Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

C# Put string into TextBox

I want to show the results of this code in my TextBox:

       string txtout1 = txtOrgText.Text.Replace(parm, txtTo.Text).ToString();
       txtout = txtout1;

I have a textbox, txtOrgtext, into which the user inputs text. I want to put some text into txtout now. I have set txtout to ReadOnly and MultiLine.

When I try running my program, I get the following error:

Error   1   Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'   C:UsersxxxAppDataLocalTemporary ProjectsWindowsFormsApplication1Form1.cs 45  25  WindowsFormsApplication1

I tried txtout1.ToString(), but nothing changes.

I also tried txtout.Text = txtout1 and get this error :

Cross-thread operation not valid: 
Control 'txtout' accessed from a thread other than the thread it was created on.

I got an error because I used Threading, without Threading it works fine.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you need to do is:

 txtout.Text = txtout1;

This is because txtout1 is just a string of characters, while txtout is a full TextBox, with all the drawing and colouring and stuff like that.

I see that you were on the right lines with your first line of code - txtOrgText.Text - the .Text is used both ways - for reading and writing. (Or "looking" and "changing" is another way of putting it.)

You do this with a lot of other controls - a ComboBox, a Form (to set the caption), a DomainUpDown (the thing with the arrows on the right) to name a few.

The reason that "ToString()" doesn't work is that ToString() is making your string of text into a string of text! It doesn't turn it into a TextBox for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...