C# help

PJBC

New Member
I'm making an application on Visual Studio 2010 for university and need help


I have to make an application that when text is entered into a text box and a button is pressed, that it then displays the entered text into a label. I have done this so far but now I need to make it so that when no text is entered the default text shows up - this is what I have but I know it's not right so can anybody help?



private void Form1_Load(object sender, EventArgs e)
{
OutputLabel.Text = "Results will show here";
if (OutputLabel.Text = "")
Console.WriteLine("Results will show here");

}

Any help would be appreciated as I have never done C# before and this is only my 2nd week - with that in mind, please keep explanations clear

Pete
 
Your code as written will always display "Results will show here" since you first set OutputLabel.Text = "Results will show here" so your If statement will always be false.

Your If statement needs to look at whatever the input text value is, then set the OutputLabel.Text value.

Your code should be something like this:

Code:
private void Form1_Load(object sender, EventArgs e)
{
if (InputBox.Value = "")
   OutputLabel.Text = "Results will show here"
else
   OutputLabel.Text = InputBox.Value;

}
 
There's an event that can be attached to the text box, called "text changed"
Doubleclick that, and check if there if the text is "". change it however you want.

C# is the awesomest language ever, i'm actually surprised you have problems like this.
 
Back
Top