2013-04-29 51 views
0

我遇到以下代碼的問題...我想要在我的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)。

感謝您的幫助。

+0

請不要將您的整個程序嘔吐到您的文章中。只發布相關代碼。 – tnw 2013-04-29 20:59:09

+0

我已經削減了很多。我想知道如果問題是在其中的一件,所以我包括了我認爲是必要的... – Jim 2013-04-29 21:01:47

回答

1

爲了得到一個水平線上,你必須保持箱內的y點相同,然後增加基於前面框的寬度和用於分離的箱子

設置間隔值的點的x值該位置是正確的,但您可能還需要設置寬度。

而且,反而讓1000個複選框數組,其中只使用40,只是使將被添加到您的控件

這個工作對我來說

for (int i = 0; i < 40; i++) 
    { 
     CheckBox c = new CheckBox(); 
     c.Location = new Point(20 * i, 20); 
     c.Width = 20; 
     c.Text = i.ToString(); 
     c.Click += c_Click; 
     this.Controls.Add(c); 
    } 

編輯一個臨時變量 共享點擊事件

void c_Click(object sender, EventArgs e) 
{ 
    CheckBox c = sender as CheckBox; 
    if (c.Checked) 
    { 
     //dostuff 
    } 
} 
+0

這就是我在這裏做的:'checkBoxes [i] .Left = i * 30; _checkBoxes [i] .Top = 500; ' – Jim 2013-04-29 21:03:53

+0

是的,我現在正在讀這個...試圖找出爲什麼這沒有爲你工作.. – Sayse 2013-04-29 21:05:10

+0

我試過用Left和Top方法'_checkBoxes [i] .Left = i * 30; _checkBoxes [i] .Top = 500;'以及新的Point'_checkBoxes [i] .Location = new Point(20 * i,200);'我認爲他們都以類似的方式工作。他們都產生了同樣的問題。 – Jim 2013-04-29 21:14:01