Textbox displays more than I hoped for...

  • Thread starter Thread starter pauldr
  • Start date Start date
P

pauldr

I want to thank Morten Wennevik for his help on my last hurdle, but now
I have another one.

I have a string value going to a label. The label displays the
following:

System.Windows.Forms.TextBox, Text: Paul

I just want 'Paul' to be displayed. Any suggestions as to why this is
happening?

Paul
 
Probably you have something like:

this.label1.Text=this.textBox1.ToString();

but you should write:

this.label1.Text=this.textBox1.Text.ToString(); (.ToString(); here is
optional since TextBox.Text return a string)

Cheers,

Romano Fabrizio
(e-mail address removed)
 
Fabrizio Romano said:
Probably you have something like:

this.label1.Text=this.textBox1.ToString();

but you should write:

this.label1.Text=this.textBox1.Text.ToString(); (.ToString(); here is
optional since TextBox.Text return a string)

'ToString' is completely redundant here and IMO should not be used.
 
I appreciate all this help.

Just want to clarify some things.

All values inside a textbox are strings and values going to a label
need to be strings as well. If I have a method that uses doubles to
perform calculations based upon the string values of the textboxes how
do I convert the strings into doubles without throwing exceptions?Paul
 
Well, there are a couple of methods (or maybe more). I would do one of
these:

double b=double.Parse(myTextBox.Text);
double c=Convert.ToDouble(myTextBox.Text);

If you want to convert them avoiding the exception to be thrown in any case,
yust put them into a try block like this:

try {
double b=double.Parse(myTextBox.Text);
} catch {
// do nothing
}

or maybe you could parse the string with regular expressions to see if it is
a good number.

Cheers,

Romano Fabrizio
(e-mail address removed)
 
pauldr said:
All values inside a textbox are strings and values going to a label
need to be strings as well. If I have a method that uses doubles to
perform calculations based upon the string values of the textboxes how
do I convert the strings into doubles without throwing exceptions?

'Double.Parse'/'Double.TryParse'/'CDbl' (VB.NET).
 
Back
Top