0

我在这里做的是;制作 UI 以直观地更新值,也会增加对其他类型的支持。可能所有类型。updateIcons 每次加载控制器时都会调用此函数,并且每次都有新的值和名称。countControls 来跟踪控制器,所以如果可以更新点击值。myP 是保存在运行时获取的值的对象,用户通过在另一个屏幕上按 Tab 来打乱值创建单选按钮组框以允许管理单选按钮组。属性都属于一个对象。每个属性都有一些可能的值,例如我的示例中的枚举。

现在我有点迷路了,不知道如何最好地做到这一点,因为现在我的 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);
  }


}
4

1 回答 1

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);
            }
        }
    }
}
于 2013-05-23T12:37:39.307 回答