我遇到以下代碼的問題...我想要在我的C#程序中創建一個新窗體。我用編程方式創建了一個新的表單,它具有一個組合框,一個複選框列表和一個允許表單將所需信息返回給調用例程的按鈕。除了我不能讓複選框以我想要的方式排列外,所有內容都可以根據需要運行。我想要一個水平線的複選框靠近窗體的底部。我用編程方式創建了複選框,但是我無法獲得它們的水平線。我可以得到一條對角線(從左上角到右下角)或者只有第一個複選框。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)。
感謝您的幫助。
請不要將您的整個程序嘔吐到您的文章中。只發布相關代碼。 – tnw 2013-04-29 20:59:09
我已經削減了很多。我想知道如果問題是在其中的一件,所以我包括了我認爲是必要的... – Jim 2013-04-29 21:01:47