字符串到文本框转换

时间:2015-07-16 07:25:06

标签: c# string casting textbox

string data = "test";
TextBox tb = (TextBox)data;  // i want something like that in order to 
tb.Backcolor = color.black;  // do this line 

3 个答案:

答案 0 :(得分:0)

使用TextBox.Text =“bla”而不是强制转换

答案 1 :(得分:0)

假设您使用Form的方法调用代码,您需要类似的内容:

  String data = "test";

  // creation of a new TextBox with Text assigned to data... 
  TextBox tb = new TextBox() {
    Parent = this, // <- text box is on the Form...
    Location = new Point(10, 10),
    Size = new Size(100, 20),
    Text = data, // <- TEXT of the TextBox is data
    BackColor = Color.Black,
    ForeColor = Color.White,
  };

答案 2 :(得分:0)

String data = "test";
TextBox tb = new TextBox();
tb.text = data;

您可以使用text属性将字符串直接分配给文本框。 希望这会有所帮助。