我在使用以下代码时遇到问题……我想在我的 C# 程序中创建一个新表单。我以编程方式创建了一个新表单,该表单具有一个组合框、一个复选框列表和一个按钮,该按钮将允许表单将所需信息返回给调用例程。一切都按需要工作,除了我无法让复选框以我想要的方式排列。我想要在表单底部附近有一条水平的复选框。我以编程方式创建了复选框,但我无法获得它们的水平线。我可以得到一条对角线(左上角到右下角)或只有第一个复选框。
我知道其他复选框在那里,因为我打印出表明所有复选框都在那里的信息,但是我没有看到它们。正如您从下面的 for 循环中看到的(带有索引 i 的循环),我尝试使用 BringtoFront()、Update() 和 Refresh() 方法。该循环是在表单上放置复选框的主循环。您可以看到我尝试过的代码行以及发生的结果。未注释的是我想要的,但只有第一个复选框出现在屏幕上。
internal System.Windows.Forms.ComboBox ComboBox12;
string theitem;
Form prompt2;
private Button getSelectedRB2;
private Button thebutton;
private CheckBox[] _checkBoxes;
CheckBox checkBox15;
int numberOcheckboxes = 40;
// initialize the combo box
private void InitializeComboBox()
{
this.ComboBox12 = new System.Windows.Forms.ComboBox();
string[] employees = new string[]{"Choose One", "Normal",
"Setup", "Special-Skip 1", "Special-Skip 2", "Special-All Blades" };
ComboBox12.Items.AddRange(employees);
this.ComboBox12.Location = new System.Drawing.Point(136, 32);
this.ComboBox12.IntegralHeight = false;
this.ComboBox12.MaxDropDownItems = 20;
this.ComboBox12.DropDownStyle = ComboBoxStyle.DropDownList;
this.ComboBox12.Name = "ComboBox12";
//size of combobox
this.ComboBox12.Size = new System.Drawing.Size(136, 81);
this.ComboBox12.TabIndex = 0;
this.Controls.Add(this.ComboBox12);
// Associate the event-handling method with the
// SelectedIndexChanged event.
this.ComboBox12.SelectedIndexChanged +=
new System.EventHandler(ComboBox12_SelectedIndexChanged);
//Create button
Button confirmation1 = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70 };
confirmation1.Click += (sender, e) => { prompt2.Close(); };
confirmation1.Click += new EventHandler(okClicked);
this.prompt2.Controls.Add(confirmation1);
prompt2.Controls.Add(this.ComboBox12);
_checkBoxes = new CheckBox[1000];
for (int i = 0; i < numberOcheckboxes; i++)
{
_checkBoxes[i] = new CheckBox();
//diagonal – works correctly
//_checkBoxes[i].Location = new Point(20 * i, 20 * i);
//left to right? - only first one appeared
//_checkBoxes[i].Location = new Point(20 * i, 200);
//left to right? - get just checkbox 0 even though they are all there
_checkBoxes[i].Left = i * 30;
_checkBoxes[i].Top = 500;
//top to bottom? Worked correctly
//_checkBoxes[i].Left = 50;
//_checkBoxes[i].Top = i * 20;
_checkBoxes[i].Text = i.ToString();
_checkBoxes[i].Enabled = true;
_checkBoxes[i].Checked = true;
_checkBoxes[i].BringToFront();
_checkBoxes[i].Update();
this.prompt2.Refresh();
_checkBoxes[i].CheckedChanged += new EventHandler(ShowCheckedCheckboxes);
this.prompt2.Controls.Add(_checkBoxes[i]);
}
}
//gets values when combobox changes...
void ComboBox12_SelectedIndexChanged(object sender, EventArgs e)
{
//works except that it has an error at the end
System.Windows.Forms.ComboBox theobject;
theobject = (System.Windows.Forms.ComboBox)sender;
theitem = (string)theobject.SelectedItem;
MessageBox.Show(theitem);
MessageBox.Show("Making everything Disabled and Unchecked so everything is set to default");
for (int i = 0; i < numberOcheckboxes; i++)
{
_checkBoxes[i].Enabled = false;
_checkBoxes[i].Checked = false;
}
}
private ComboBox combobox10;
//Combobox on a form?
public string Select_Blades_InitializeCombobox()
{
prompt2 = new Form();
prompt2.Width = 1000;
prompt2.Height = 600;
InitializeComboBox();
prompt2.ShowDialog();
return (theitem);
}
void ShowCheckedCheckboxes(object sender, EventArgs e)
{
}
private void CheckBoxCheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("CheckBoxCheckedChanged");
}
private void okClicked(object sender, EventArgs e)
{
MessageBox.Show("okClicked");
}
正如我之前指出的,这是用 C# 编写的。我有一台 Windows XP 机器,我正在使用 Visual Studio 2008。我正在创建一个 Windows 应用程序。
总而言之,除了似乎不喜欢在水平线上的复选框之外,所有内容都出现在我的表单上(上面称为 prompt2)。
谢谢你的帮助。