9 Replies - 9213 Views - Last Post: 15 January 2010 - 09:00 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 374
  • Joined: 14-December 08

Label to display message

Posted 12 January 2010 - 05:31 PM

Right now, we are doing or working with window form, rather than console. So it is a bit tough to create a robust WF program. Still we are just at the very beginning of doing window form, so what we do is just drag and drop the controls from the already-provided control palettes.

Three questions I might need your help with.
1) When I read the book as a reference book, it just mentions about components and controls. The question is what is control and what is component?

2) Well...I tried to use TryParse but the result is 0 if the user enters some junk, what I want to display is the null value , not 0 within the label, which is what I use to display the result in text format.

(I know how TryParse works because it cannot parse the value, it return false, and the value 0 is the default value, which is created by the constructor).

3) In console, if you do something like the codes below, if the users enter junk, the invalid message is displayed. But in window form, I cannot do the same. I have to use ShowMessage method to display the invalid input message. I know it is possible to use Label to display the message, but HOW?

int i;
bool result;
String x;

Console.Write("Please enter a number: ");
x = Console.ReadLine();
result = int.TryParse(x, out i);

if(result == false) Console.Write("\nThis is invalid input);



Here is the code for Window Form

// text is the properties of form, button, label, and textbox.

//lblOutput is the label text property
// txtInteger and txtInteger2 are the name
// of the textboxes.
bool result_x, result_i;
int i, x, result;

result_i = int.TryParse(txtInteger.text, out i);
result_x = int.TryParse(txtInteger2.text, out x);

if(!result_i || !result_x)   lblOutput.text = "This is invalid input.";

result = (i + x) / 2;
lblOutput.text = result.ToString();



4) Question is why do we have to use text property following the name of textbox and label?
- The last lblOutput.text works and display the proper result. However, the statement if(!result_i || !result_x) lblOutput.text = "This is invalid input."; makes the program crash. I am just wondering why the bottom lblOutput works but the if(!result_i || !result_x) lblOutput.text = "This is invalid input."; won't work?

Is This A Good Question/Topic? 0
  • +

Replies To: Label to display message

#2 finaiized  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 29-November 09

Re: Label to display message

Posted 12 January 2010 - 05:39 PM

1) Control is something that the user can directly interact with basically. A component is something like a pie chart that displays information and can still be accessed by drag-and-drop.
2)I guess you could check if the output/string is 0; if so, change it to null or whatever.
3)What do you mean by message? If you want a label to show text, use label.Text="text here". You can also warn users it didn't work by using MessageBox.Show("It didn't work");
4)I can't be 100% sure, but try if it works using the full writing of an if statement, like this:
if(!result_i || !result_x)  { lblOutput.text = "This is invalid input.";}

Was This Post Helpful? 0
  • +
  • -

#3 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Label to display message

Posted 12 January 2010 - 05:48 PM

Here's a simple solution to posting exceptions.

Say for isntance you want an error message to pop-up when a user types in a letter instead of a number, you'd do something like this.

try
{
    int a = textBox1.Text;
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}



That will display what went wrong very easily.

A useful tip: Just type in "try" and press the TAB key twice, the IDE will generate the trycatch statements for you. :)
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Label to display message

Posted 12 January 2010 - 05:56 PM

You have made the jump from working in console, where you are in control of how the program excutes, to event driven programming where you respond to what the person using your software decides to do. It can be a big leap for a beginner so don't worry too much if you have a problem right of the start. I'm sure we will all be more than happy to help you through this. :)

3) - You can use the MessageBox class to display the message. Your if statement is a little more complicated than it needs to be as well. You can try the following.

if !(result_i && result_x)
{
	result = (i + x) / 2;
	lblOutput.text = result.ToString();
}
else
{
	MessageBox.Show("You have entered an invalid number.\nPlease enter numbers only.");
	lblOutput.Text = "";
}



The reason this works is that !(result_i && result_x) will only evalute to false if both of the conversions worked property. That might sound a little complicated to you, I know you have struggled with things like this in the past. If either of result_i or result_x are false result_i && result_x evaluates to false and by using ! the result is true and the code will be executed.

Hopefully this helps you out. :)

View Postpapuccino1, on 12 Jan, 2010 - 07:48 PM, said:

Here's a simple solution to posting exceptions.

Say for isntance you want an error message to pop-up when a user types in a letter instead of a number, you'd do something like this.

try
{
    int a = textBox1.Text;
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}



That will display what went wrong very easily.

A useful tip: Just type in "try" and press the TAB key twice, the IDE will generate the trycatch statements for you. :)


Yes, you could try that. However, relying on try-catch will cause a performance hit when you can easily use the TryParse method when converting from strings to numbers which will execute better. If you can avoid using a try-catch block using methods supported by C# you should.
Was This Post Helpful? 0
  • +
  • -

#5 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 374
  • Joined: 14-December 08

Re: Label to display message

Posted 12 January 2010 - 06:28 PM

Thanks so much for three of your helps. I really appreciate it. For 6of11, I will try that out tomorrow for the new assignment the instructor give us. Thanks so much, and that would be a great help.

BTW, is it ok not to use MessageBox.Show() Function or method? Instead, using if !(result_i && result_x)
lblOutput.text = "Invalid Input"?


-Why do we have to use text after the name of the texbox, label and so on? For instance, integer.text...I know text is the property, without which the program won't work but what bugs me is why we need to use text property right after the object such as label name, texbox name...? One of my fellow classmates told me, if we don't use text, the output would be displayed as a whole thing. I just don't get it.
Was This Post Helpful? 0
  • +
  • -

#6 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Label to display message

Posted 12 January 2010 - 06:52 PM

View Postkenryuakuma, on 12 Jan, 2010 - 05:28 PM, said:

Thanks so much for three of your helps. I really appreciate it. For 6of11, I will try that out tomorrow for the new assignment the instructor give us. Thanks so much, and that would be a great help.

BTW, is it ok not to use MessageBox.Show() Function or method? Instead, using if !(result_i && result_x)
lblOutput.text = "Invalid Input"?


-Why do we have to use text after the name of the texbox, label and so on? For instance, integer.text...I know text is the property, without which the program won't work but what bugs me is why we need to use text property right after the object such as label name, texbox name...? One of my fellow classmates told me, if we don't use text, the output would be displayed as a whole thing. I just don't get it.



Regarding your first question, it's only a matter of aesthetics. Generally speaking you only want to display a MessageBox for crucial parts.
  • Do you want to save your file?
  • Do you want to undo the changes?
  • Are you sure you want to delete your local cache?
Things like that. Remember not to bother your end user too much with random boxes that require him to stop cold on what he is doing just to click 'OK'. :)

The Textbox control, label control etc. are all just Classes. That means that they have methods to display information, methods to resize, methods to select text, all of this is managed internally. Meaning, ideally, you never have to know about them.

When you use TextBox.Text, you are using that class's property to GET the string saved to that classes instance.

Here's an image to illustrate:

Posted Image

Don't worry if you don't get this, just read it once. Read it again tomorrow and you should understand it a bit. :D If it's too confusing just ask here and I'll help ya out bro. :)

This post has been edited by papuccino1: 12 January 2010 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Label to display message

Posted 13 January 2010 - 09:34 AM

That drawing cracks me up. Nicely done.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Label to display message

Posted 13 January 2010 - 09:42 AM

Brings to mind a multicultural T4 bacteriophage invasion.
Was This Post Helpful? 0
  • +
  • -

#9 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 433
  • View blog
  • Posts: 3,006
  • Joined: 12-January 10

Re: Label to display message

Posted 13 January 2010 - 02:25 PM

since you are using a try catch and it is numbers you want use the
try
{
}
catch(FormatException)
{
}

that will make sure you are using what is declared
Was This Post Helpful? 0
  • +
  • -

#10 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 374
  • Joined: 14-December 08

Re: Label to display message

Posted 15 January 2010 - 09:00 PM

Thanks a lot for helping out with the graphical stuff or drawing in short.

Now I think I probably get it and it seems to me that the property, text, is just like the members or instance variables in console C#.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1