开发者

Populating Control values from a Dictionary?

开发者 https://www.devze.com 2023-04-01 16:44 出处:网络
I am new to C# so please fogive my newbie question. I created a dictionary of controls from a Windows form called dictControls.I then populated it with all text box and combobox controls and开发者_JS

I am new to C# so please fogive my newbie question.

I created a dictionary of controls from a Windows form called dictControls. I then populated it with all text box and combobox controls and开发者_JS百科 values from the form:

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();
        }

I want the dictionary basically look something like this:

Key ------------ Value

"txtName" ----- "Test"

"txtYear" ------ "1980"

I am passing this back into the same form (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;**
        }
    }

I am looking for a way to take key and turn it into the control name, then add ".Text" or ".Value" to it and then set the value to item(value) as I explained in the code above.

Is this possible? I tried researching this, but I have yet to put 2 and 2 together.


You may just store the set of controls you work with in your dictionary:

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

But the whole idea seems to be bad design. You should probably clarify the problem you're trying to solve.


If I understand your question, you can use Find()

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

精彩评论

暂无评论...
验证码 换一张
取 消