字符串到文本框的转换

本文关键字:转换 文本 字符串 | 更新日期: 2023-09-27 18:02:53

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

字符串到文本框的转换

使用TextBox。Text = "bla"代替cast

假设您使用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,
  };
String data = "test";
TextBox tb = new TextBox();
tb.text = data;

你可以将string直接赋值给文本框的using text属性。