从字典中填充控件的值?

3

我是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)。这是否可行?我已经进行了研究,但是还没有想通。

你只是想要持久化数据吗?如果是这样,你可以尝试序列化表单而不是手动保存每个东西。 - Dustin Davis
如果这是新的,请使用WPF。 - Jodrell
@Jodrell,转换到WPF会如何帮助他? - Dustin Davis
@Titan2782,Mark将学习推荐的客户端GUI开发方式,而不是为了向后兼容性而提供的传统选项。 - Jodrell
2
谁说只因为它是新的就是推荐的方式?如果他只是在学习C#,那么做WPF只会让他的学习变得更加复杂,因为他必须浏览所有WPF的废话。你的评论毫无建设性。 - Dustin Davis
2
首先,感谢大家的回复。我是C#的新手,基本上我有一个Windows表单,在保存时调用自身。我正在尝试使用表单中的信息预填写一些文本框和组合框。我认为我可以通过使用控件(键)和值(值)的字典将其传回构造函数,并循环遍历并解析出键(即表单上控件的名称)和值来实现这一点。我只是为了学习而这样做。我从Windows表单开始,这样我就可以学会爬行,然后再学会走路,这么说吧。 :) - Mark Hackenberg
2个回答

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 ;) - Dustin Davis
@Titan2782:特别为您修复;-) - Andrey Agibalov
我有一个带有文本框和组合框的Windows表单,当点击保存时会调用自身。我正在尝试使用来自该表单的信息预填充一些文本框和组合框。我不太明白,这里有多少个表单?预填充数据从哪里来? - Andrey Agibalov
它来自表单本身(frmAddVideo)。Save_OnClick再次调用该表单:frmAddVideo frm = new frmAddVideo(numDiscs, discNumber, videoID, sequenceID, dictFormControls);我在这里尝试的是再次打开表单,而不是空白的表单,它将预填一些之前点击保存的表单值。
  1. 填写表格。
  2. 点击保存。
  3. 再次打开表单,并从步骤1中预填某些字段。
这就是我想要的。感谢您的时间。
- Mark Hackenberg
在某种程度上是的,它是一个小项目,用于记录我的 DVD 收藏。一些 DVD 套装有多个光盘。第一件事就是我要指定套装中有多少张光盘(例如,3张)。然后我在表单上输入关于第一张DVD的信息(名称、运行时间、光盘号等),然后点击保存。有一个计数器变量来检查我正在输入哪张DVD的信息。这些信息进入数据库并且计数器增加。在保存方法中,我会检查计数器是否<=总的光盘数量。如果还有更多要输入的,则关闭该表单,然后出现一个新的表单,其中字段预填充自前一个表单。 - Mark Hackenberg
显示剩余9条评论

0

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

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

你忘了使用 ListControls 来获取 SelectedValue。 - Conrad Frix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接