从字典中填充控制值?

时间:2011-09-01 15:39:54

标签: c# winforms dictionary controls

我是C#的新手,所以请为我的新手提问。

我从一个名为dictControls的Windows窗体中创建了一个控件字典。然后我用所有文本框和组合框控件以及表格中的值填充它:

Dictionary<Control, string> dictFormControls = new Dictionary<Control, string>(); 
        foreach (Control c in Controls)
        {
            if (c is ComboBox)
            {
               dictFormControls.Add(c, ((ComboBox)c).SelectedValue.ToString()); 
            }
            if (c is TextBox)
            {
               dictFormControls.Add(c, ((TextBox)c).Text); 
            }
            if (c is MaskedTextBox)
            {
               dictFormControls.Add(c, ((MaskedTextBox)c).Text);
            }
        }

        if (discNumber <= Convert.ToInt32(numDiscs))
        {
         frmAddVideo frm = new frmAddVideo(numDiscs, discNumber, videoID, sequenceID, dictFormControls);
         frm.Show();
         this.Close();
        }

我希望字典看起来像这样:

键------------值

“txtName”-----“测试”

“txtYear”------“1980”

我将其传回相同的形式(frmAddVideo):

public frmAddVideo(string numDiscs, int discNumber, string videoID, string sequenceID, Dictionary<Control, string> dict)

    {
        this.numDiscs = numDiscs;
        this.discNumber = discNumber;
        this.videoID = videoID;
        this.sequenceID = sequenceID;
        InitializeComponent();

        //This is where I want to parse out the Dictionary and populate the form values
        foreach (KeyValuePair<Control, string> item in dict)
        {

            **Basically, I am looking for a way to take **            
            **item(Key)**
            **and do something like item(Key).Value = item(Value);**
            **so it would be the same as writing**
            **txtName.Text= "1980";**
            **cbxLocID.Value = 1;**
        }
    }

我正在寻找一种方法来获取密钥并将其转换为控件名称,然后向其添加“.Text”或“.Value”,然后将值设置为item(value),如上面的代码中所述。

这可能吗?我试过研究这个,但我还没有把2和2放在一起。

2 个答案:

答案 0 :(得分:1)

您可以将您使用的控件集存储在字典中:

class ControlBoundValueDescription
{
  private Control _control;

  public ControlBoundValueDescription(Control control)
  {
    _control = control;
  }

  public string Value
  {
    get
    {
      if(_control is ...) return ...
      ...
    }

    set
    {
      if(_control is ...) ((Xxx)_control).Yyy = value;
      ...
    }
  }
}
...    
Dictionary<string, ControlBoundValueDescription> dictControls = 
  new Dictionary<string, ControlBoundValueDescription>(); 
...
// defining mappings (you may also want to populate it automatically,
// by iterating over all the controls you have on your form)
dictControls["UserName"] = new ControlBoundValueDescription(tbUserName);
dictControls["Group"] = new ControlBoundValueDescription(cbxGroup);
...
// working with controls using previously defined mappings
dictControls["UserName"].Value = "guest"; // probably, text box
dictControls["Group"].Value = "Guest Users"; // probably, combo

但整个想法似乎都是糟糕的设计。你应该澄清你想要解决的问题。

答案 1 :(得分:0)

如果我理解你的问题,你可以使用Find()

 ((TextBox)myForm.Controls.Find(Key, true)).Text = Value;
 ((CheckBox)myForm.Controls.Find(Key, true)).Checked = Boolean.Parse(Value);