C#将字符串放入TextBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1217772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:47:45  来源:igfitidea点击:

C# Put string into TextBox

c#

提问by zimzim

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.

我有一个文本框,txtOrgtext用户可以在其中输入文本。我现在想将一些文本放入 txtout 中。我已将 txtout 设置为 ReadOnly 和 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.

我试过了txtout1.ToString(),但没有任何改变。

I also tried txtout.Text = txtout1and get this error :

我也试过txtout.Text = txtout1并得到这个错误:

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.

我收到一个错误,因为我使用了线程,没有线程它工作正常。

采纳答案by Lucas Jones

What you need to do is:

你需要做的是:

 txtout.Text = txtout1;

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

这是因为txtout1它只是一串字符,txtout而是一个完整的 TextBox,包含所有绘图和着色之类的东西。

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

我看到你的第一行代码是正确的 - txtOrgText.Text -.Text两种方式都使用 - 用于阅读和写作。(或者“看”和“改变”是另一种说法。)

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.

您可以使用许多其他控件来执行此操作 - 一个 ComboBox、一个 Form(用于设置标题)、一个 DomainUpDown(带有右侧箭头的东西)等等。

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.

“ToString()”不起作用的原因是 ToString() 正在将您的文本字符串转换为文本字符串!它不会把它变成一个 TextBox 给你。

回答by David

txtOut.Text = txtout1;

txtOut.Text = txtout1;

回答by Raghav

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

首先 txtout = txtout1;不会作为 txtout 是一个文本框而 txtout1 是一个字符串。你应该使用

txtout.Text = txtout1

txtout.Text = txtout1

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

即文本框的 .Text 属性表示 获取或设置 System.Windows.Forms.TextBox 中的当前文本,其类型为字符串,因为您的 txtout1 已经是字符串,因此无需使用 .ToString() 再次转换它