我的表单上有“listBox1”和“button1”。我有两个功能。第二个函数向listbox1添加复选框,第一个函数显示消息框。但我不知道如何编写第一个函数。
在这里,我想检查选中的复选框并写一条消息:
private void button1_click(object sender, EventArgs e)
{
MessageBox.Show("radiobutton: " + rb[i].Text);
}
在这里,我创建了一个复选框://它正在工作
internal void loadSurveys()
{
WebClient client2 = new WebClient();
var json = client2.DownloadString("http://www.test.net/api/surveys/?api_key=123");
JObject data = JObject.Parse(json);
var example = JsonConvert.DeserializeObject<Example>(json);
int y = 5;
int i = 0;
RadioButton[] rb = new RadioButton[example.surveys.Length];
String chkBox_name = "";
String chkBox_text = "";
foreach (var survey in data["surveys"].Children())
{
rb[i] = new RadioButton();
rb[i].Location = new Point(5, y);
rb[i].Name = chkBox_name + survey["id"];
rb[i].Text = chkBox_text + survey["title"];
rb[i].AutoSize = true;
this.listBox1.Controls.Add(rb[i]);
y += 20;
i++;
}
}
答案 0 :(得分:1)
第一步是使radiobutton数组在表单级别上变量:
RadioButton[] rb
在loadSurveys
中分配rb = new RadioButton[example.surveys.Length];
然后点击按钮
即可访问该数组var rb = rb.FirstOrDefault(r=>r.Checked);
if(rb==null)
MessageBox.Show("No radiobutton was selected");
else
MessageBox.Show("radiobutton: " + rb[i].Text);
编辑刚刚注意到您将radiobuttons添加到列表框中。 listbox1变量是否是实际的列表框?以上仍然有效,但如果目标是显示radiobuttons的列表框,您可以自定义绘制列表框,否则使用普通面板而不是列表框。 无论哪种方式,您还可以对listbox1变量的控件(使用OfType)执行firstordefault,但是如果您使用列表框并填充其项目,则只需使用SelectedIndexChanged
编辑2 由于我已经拥有它,想要展示一种方法,使您的列表框成为单选框。您可以使用以下类将任何现有列表框设为radiobutton框:
public class RadioButtonBoxPainter:IDisposable
{
public readonly ListBox ListBox;
public RadioButtonBoxPainter(ListBox ListBox)
{
this.ListBox = ListBox;
ListBox.DrawMode = DrawMode.OwnerDrawFixed;
ListBox.DrawItem += ListBox_DrawItem;
}
void ListBox_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1) return;
Rectangle r = e.Bounds;
r.Width=r.Height;
bool selected= (e.State & DrawItemState.Selected) > 0;
e.DrawBackground();
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
ControlPaint.DrawRadioButton(e.Graphics, r, selected ? ButtonState.Checked : ButtonState.Normal);
r.X = r.Right + 2;
r.Width = e.Bounds.Width - r.X;
string txt;
if (ListBox.Site != null && ListBox.Site.DesignMode && e.Index >= ListBox.Items.Count)
txt = ListBox.Name;
else
txt = ListBox.GetItemText(ListBox.Items[e.Index]);
using (var b = new SolidBrush(e.ForeColor))
e.Graphics.DrawString(txt, e.Font, b, r);
if (selected)
{
r = e.Bounds;
r.Width--; r.Height--;
e.Graphics.DrawRectangle(Pens.DarkBlue, r);
}
}
public void Dispose()
{
ListBox.DrawItem -= ListBox_DrawItem;
}
}
标准实施的例子:
public class RadioButtonBox:ListBox
{
public readonly RadioButtonBoxPainter Painter;
public RadioButtonBox()
{
Painter = new RadioButtonBoxPainter(this);
}
[DefaultValue(DrawMode.OwnerDrawFixed)]
public override DrawMode DrawMode
{
get{return base.DrawMode;}
set{base.DrawMode = value;}
}
}
RadioButtonBox是我实际使用很多的控件。就个人而言,我发现在实施一堆单独的无线电按钮时要快得多。
如果您想使用它,并想要一个示例如何在当前代码中实现它,请留下评论,我将添加一个。
答案 1 :(得分:1)
您可以浏览listBox1.Controls
并选择已选中的RadioButton
private void button1_click(object sender, EventArgs e)
{
var rb = this.listBox1.Controls.OfType<RadioButton>().SingleOrDefault(n => n.Checked);
if (rb != null)
MessageBox.Show("radiobutton: " + rb.Text);
}
因为这是RadioButton
,所以不应该多检查一次