English 中文(简体)
C#: 如何从其他变量中构建一个变量
原标题:C#: How to construct a variable from another variables

如何在C#中构建新的变量。

顺便说一句。

public void updateXXX(string endingOfVariable, int newValue)
{
   this.textBox_{endingOfVariable} = newValue;
}

在博士中实施:

$a =  var ; $b =  iable ;
$variable =  var ;
echo ${$a.$b};

但也许可以在C#中。

The problem is that - I created ~500 textBoxes in C# Windows Form, and if I want to set a value, I need to build a switch() {case:; } statment with 500 s cases.

最佳回答

了解你改用500起案件的开关说明,你可以采用FindControl方法,将其带给文本Box。

((TextBox)FindControl("textbox_" + endingOfVariable)).Text = newValue;
问题回答

如果你向每种文本正统指定一个名字,你可以制作文字图,以控制:

var boxes = form.Controls.OfType<TextBox>().ToDictionary(t => t.Name);

public void Update(string name, int newValue)
{
    boxes[name].Text = newValue.ToString();
}

视窗表格中包含500个文字箱本身可能是一个问题(许多控制可能很缓慢)。

这样做的一个途径是,在你创建时,把控制放在一个字典上(如果你真心实意地在计划上建立这种控制),然后使用字典钥匙来撤销你们所需要的控制。

你们需要思考

  static void Main( string[] args )
  {
    Type type = typeof(MyClass);
    object o = Activator.CreateInstance(type);

    FieldInfo field = type.GetField("text", BindingFlags.NonPublic | BindingFlags.Instance);
    field.SetValue(o, "www.domain.com");
    string text = (string)field.GetValue(o);

    Console.WriteLine(text);

  }

http://www.java2s.com/Tutorial/CSharp/0400_Reflection/Getsfieldusinga FieldInfo.htm





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签