7

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:\Users\xxx\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.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.

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
zimzim
  • 83
  • 1
  • 2
  • 5

3 Answers3

16

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.

Lucas Jones
  • 19,767
  • 8
  • 75
  • 88
  • @person-b i get error Cross-thread operation not valid: Control 'txtout' accessed from a thread other than the thread it was created on. i removd all tostring but ... – zimzim Aug 01 '09 at 23:50
  • Are you using a BackgroundWorker component or the Thread class? Try doing a Ctrl+F (find) for them. If not, can you post as much code as possible to http://pastebin.com please. Thanks :) – Lucas Jones Aug 02 '09 at 00:19
  • Oh, and, by the way, sorry for the slow response - I'll check up a bit more often now ;) – Lucas Jones Aug 02 '09 at 00:23
4

txtOut.Text = txtout1;

David
  • 72,686
  • 18
  • 132
  • 173
2

First of all txtout = txtout1; will not serve as txtout is a textbox and txtout1 is a string .You should use

txtout.Text = txtout1

ie .Text property of textbox says Gets or Sets the current text in System.Windows.Forms.TextBox and its type is string as your txtout1 is already a string there is no need to convert it again by using .ToString()

Raghav
  • 8,772
  • 6
  • 82
  • 106