blah blah blah is here! blah blah » Close

up0down
link

I'm trying to get the text from a TextBox but the following is always returned:
"System.Windows.Forms.TextBox, Text: "
and then the actual desired text in the control is shown after "Text:".
How can I get just the text in the control without having to use String operations to cut that useless part off?

last answered 2 years ago

1 answers

up0down
link

It sounds like you're doing something like:
textBox1.ToString()
What you want is:
textBox1.Text

up0down
link

I'm using the Text property. Here's my line of code:
String myConnStr = (txtEdt_connection.Text);
and just for fun, I tried .ToString() as well. It also returned something similar: "System.Windows.FOrms.TextBox, Text: ..."

up0down
link

How did you set the Text property in the first place, programatically or by typing it into the textbox?
If the former, what code did you use?

up0down
link

First, I tried setting it in the Properties pane that's available within Visual C#'s Design view.
Then, I tried setting it in the actual ProjectName.Designer.cs file where everything is initialized, using this line:
this.txtEdt_connection.Text = "-no connection specified-";

up0down
link

And if you then do:
MessageBox.Show(txtEdt_connection.Text);
it displays:
System.Windows.Forms.TextBox, Text: -no connection specified-
If that's the case, I'm completely baffled!
Time I packed in for the day, I think :)

up0down
link

Actually, I then do:
txtEdt_connection.Text = txtEdt_connection + "Rawrawrawr";
so that the control displays the old string and my (obviously elegant) distinction at the end.
I'm baffled too! Thanks for trying, Vulpes. :)

up0down
link

Ah, I think I've spotted now where the problem lies!
When you do this:
txtEdt_connection.Text = txtEdt_connection + "Rawrawrawr";
the ToString() method is called implicitly on txtEditConnection and the result is then concatenated with "Rawrawrawr".
If you do it like this instead then the problem should go away:
txtEdt_connection.Text += "Rawrawrawr";

up0down
link

Good to know! Thanks, Vulpes!

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback