存储多个控件并更新单选按钮值?

时间:2013-05-23 11:34:15

标签: c# .net user-interface

这里做的是什么;使UI可视化更新值,也将为其他类型添加更多支持。可能是所有类型。 updateIcons每次加载控制器时都会调用此函数,并且每次都有新值,名称。 countControls用于跟踪控制器,因此可以更新点击的值。 myP是保存在运行时获取的值的对象,用户通过从另一个屏幕按Tab键来重排值 创建了radiobuttons groupboxes以允许管理radiobutton组。 属性都属于一个对象。每个属性都有几个可能的值,如我的例子,枚举。

现在有点迷失,不知道如何最好地做到这一点,因为现在我的rb_CheckedChanged正在返回某种混乱。 我该怎么做正确的方法?总而言之,我觉得它至少是一种正确的方法。 我想写一本字典?在已检查的事件中使用它。不确定如何

 private void updateIcons(List<Props> prop) {
  countControls++;
  locationY = 10;
  int gbHeight;
 foreach (var p in prop) {
radioButtonY = 10;
IType pType = p.Type;
if (pType is Enum) {
  var myP = new MyProp(p, this);
  GroupBox gb = new GroupBox();
  gb.Location = new Point(nextLocationX,locationY);
  nextLocationX += rbWidth+10;
  gb.Name = "groupBox" + countControls;
  gb.Text = "smthn";
  var TypesArray = set here;

gbHeight = TypesArray.Length;
foreach (var type in TypesArray) {
  getimagesPath(TypesArray);
  RadioButton rb = new RadioButton();
  rb.Appearance = Appearance.Button;
  rb.Width = rbWidth;
  rb.Height = rbHeight;
  rb.Name = type.Name + countControls;
  rb.Text = type.Name;
  string path = imagePaths[type.Name];
  Bitmap rbImage = new Bitmap(path);
  rb.BackgroundImage = rbImage;
  countControls++;
  rb.Location = new Point(radioButtonX, radioButtonY);

  if (myP.Value != null && type.Name.SafeEquals(myP.Value.ToString())) {
    rb.Checked = true;

  }
  radioButtonY += rbHeight;
  gb.Controls.Add(rb);
  rb.CheckedChanged += rb_CheckedChanged;

}
gb.Height = rbHeight * gbHeight + 20;
gb.Width = rbWidth + 10;

Controls.Add(gb);
}
}
}

void rb_CheckedChanged(object sender, EventArgs e) {
  RadioButton rb = (RadioButton)sender;
  Control control = (Control)sender;

  if (rb.Checked) {
    MessageBox.Show("You have just checked: " + rb.Text);
    MessageBox.Show("You have just called Controller: " + control.Name);
    var t = PropSeq;
  }
  else {
    MessageBox.Show("you have just unchecked: " + rb.Text);
    MessageBox.Show("You have just called Controller: " + control.Name);
  }


}

1 个答案:

答案 0 :(得分:0)

我认为您的代码可能有点乱,而不是最容易阅读。它看起来很简单,并非所有关闭括号都存在?尝试下面的代码,它将创建两个组框,每个组有五个单选按钮。这应该可以帮助您实现您想要做的事情(基本表格的完整列表):

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CreateButton();
        }

        private void CreateButton() 
        {
            // Add two group boxes
            for (int groupCount = 1; groupCount < 3; groupCount++)
            {
                var groupBox = new GroupBox();
                groupBox.Location = new Point(220 * (groupCount - 1), 10);
                groupBox.Name = string.Format("groupBox{0}", groupCount);
                groupBox.Text = string.Format("Group Box {0}", groupCount);

                // Add some radio buttons to each
                for (int buttonCount = 1; buttonCount < 6; buttonCount++)
                {
                    var radioButton = new RadioButton();
                    radioButton.Width = 150;
                    radioButton.Location = new Point(10, 30 * buttonCount);
                    radioButton.Appearance = Appearance.Button;
                    radioButton.Name = string.Format("radioButton{0}", buttonCount);
                    radioButton.Text = string.Format("Dynamic Radio Button {0} - {1}", groupCount, buttonCount);
                    radioButton.CheckedChanged += radioButton_CheckedChanged;

                    // Add radio button to the group box
                    groupBox.Controls.Add(radioButton);
                    groupBox.Height += 20;
                }

                // Add group box to form
                Controls.Add(groupBox);
            }               
        }

        private void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            // Get button and only show the selected (not now de-selected item)
            var radioButton = (RadioButton)sender;
            if (radioButton.Checked)
            {
                MessageBox.Show("You have just checked: " + radioButton.Text);
            }
        }
    }
}