简体   繁体   中英

How to change text in textbox when I press the button?

Frist of all, I'm a beginner in C# and I want to become a game dev one day. So here I am, Asking about this simple question for you. but not for me.

The question is, I wanted my code to change text when I press the button. Imagine you're playing text-based adventure game which I'm trying to make here, and when you press the button it'll change the text each time when you press it.

But my code doesn't, here is my code enter image description here

The code is not wrong, but it doesn't work as I expected. It'll show only the last one but not from the first to last.

If you can help me develop my knowledge about this I'd be thankful so much.

You can cycle through an array or a List .

Here is a very simple exemple:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace TextLineByLine
{
    public partial class Form1 : Form
    {
        List<string> Messages = new List<string>();
        int currentLine = -1;

        public Form1()
        {
            InitializeComponent();

            // Add some messages
            Messages.Add("You open your eyes");
            Messages.Add("The bright blue sky is up there");
            Messages.Add("And some blood covers the ground");
            Messages.Add("You're not sure what happened...");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            currentLine++; // increment the line index
            if (currentLine < Messages.Count)
            {
                Title.Text = Messages[currentLine];
            }
            else
            {
                Title.Text = "-= Nothing to display =-";
            }
        }
    }
}

if I understand your question well you want a text change each time it is pressed.

You can do it easily by using an Integer for example.

public partial class MainWindow : Window 
{
    int dialogue = 0;
    public MainWindow() { InitializeComponent(); }
    private void btnAction_Clickl(object sender, RoutedEventArgs e) 
    {
        switch ( dialogue ) 
        {
            case 0:
                Title.Text = "Some text";
                dialogue++;
                break;
            case 1:
                Title.Text = "Another text";
                dialogue++;
                break;
            default: //just in case you want to reset the text
                Title.Text = "First text";
                dialogue = 0;
                break;
         }
    }
}

If you want to set it automatic, like just one click and then the text changes every x seconds you can use:

public partial class MainWindow : Window 
{
    public MainWindow() { InitializeComponent(); }
    private void btnAction_Clickl(object sender, RoutedEventArgs e) 
    {
        Dialogue();
    }
    
    private async void Dialogue()
    {
        Title.Text = "Some text";
        await Task.Delay(2000); // Where 2000 means 2 seconds in milliseconds
        Title.Text = "More text"; // And so on...
    }
}

Have a nice day.

Well you can create a string array for your sentences like this and do the following.

int counter = 0;

private void btnAction_Click(object sender, RoutedEventArgs e)
{
    string[] messages = new string[4]
    messages[0] = "Your first message";
    messages[1] = "Your second message";
    messages[2] = "Your third message";
    messages[3] = "Your fourth message";
    
    title.Text = messages[0];
    counter++;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM