我有一個checkedListBox在的TabControl創建標籤動態地根據CheckedListBox C#的WinForms的
我要的是創造一個標籤和的NumericUpDown動態,當用戶選中的項目checkedListBox它將顯示新標籤和NumericUpDown
然後,當它取消選中此項時,numericUpDown將被清除(空)。
結論:由於許多檢查的項目,因爲許多w've創建標籤和NumericUpDowns。
請問我該怎麼做?
我有一個checkedListBox在的TabControl創建標籤動態地根據CheckedListBox C#的WinForms的
我要的是創造一個標籤和的NumericUpDown動態,當用戶選中的項目checkedListBox它將顯示新標籤和NumericUpDown
然後,當它取消選中此項時,numericUpDown將被清除(空)。
結論:由於許多檢查的項目,因爲許多w've創建標籤和NumericUpDowns。
請問我該怎麼做?
對於checkedListBox屬性中的每個複選框項目,切換到事件併爲事件CheckStateChanged創建用戶checkBoxName_CheckStateChanged。 在sucriber的代碼可以是這樣的:
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
var source = sender as CheckBox;
if (source.Checked == true)
{
this.numericUpDown1.Text = "TextWhenChecked";
this.labelAtTheNumericUpDown.Text = "TextWhenChecked";
}
else
{
this.numericUpDown1.Text = "TextWhenUnchecked";
this.label1.Text = "TextWhenUnchecked";
}
}
,只要你想你填寫的字符串。這些只是例子。 要一次只檢查一次checkBox,請看這裏:https://stackoverflow.com/a/24693858/6650581。
您需要做的是手動創建Label和NumericUpDown並通過添加到Controls集合來顯示它。 TableLayoutPanel可以幫助您安排控件而無需手動設置大小和計算位置。 這裏是一個例子:
public class MainForm : Form
{
private CheckedListBox checkedListBox;
private TableLayoutPanel tableLayoutPanel;
public MainForm()
{
InitializeComponent();
//Fill checkedListBox and create controls
for(int i = 0; i <= 5; i++)
{
checkedListBox.Items.Add(i.ToString());
Label lbl = new Label()
{
Name = "lbl" + i,
Text = "Label " + i,
Visible = false
};
NumericUpDown num = new NumericUpDown()
{
Name = "num" + i,
Value = i,
Visible = false
};
tableLayoutPanel.Controls.Add(lbl, 0, i);
tableLayoutPanel.Controls.Add(num, 1, i);
}
}
private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
if(e.NewValue == CheckState.Checked)
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = true;
tableLayoutPanel.Controls["num" + e.Index].Visible = true;
}
else
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = false;
((NumericUpDown)tableLayoutPanel.Controls["num" + e.Index]).Value = 0M;
}
}
private void InitializeComponent()
{
this.checkedListBox = new System.Windows.Forms.CheckedListBox();
this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// checkedListBox
//
this.checkedListBox.Location = new System.Drawing.Point(8, 8);
this.checkedListBox.Name = "checkedListBox";
this.checkedListBox.Size = new System.Drawing.Size(200, 100);
this.checkedListBox.TabIndex = 1;
this.checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox_ItemCheck);
//
// tableLayoutPanel
//
this.tableLayoutPanel.AutoScroll = true;
this.tableLayoutPanel.ColumnCount = 2;
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.Location = new System.Drawing.Point(8, 112);
this.tableLayoutPanel.Name = "tableLayoutPanel";
this.tableLayoutPanel.Size = new System.Drawing.Size(200, 100);
this.tableLayoutPanel.TabIndex = 2;
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(223, 227);
this.Controls.Add(this.tableLayoutPanel);
this.Controls.Add(this.checkedListBox);
this.Name = "MainForm";
this.ResumeLayout(false);
}
}
是的,但你到目前爲止嘗試過什麼?你目前的代碼在哪裏? –
您可以使用「TableLayoutPanel」或「FlowLayoutPanel」創建此類UI,並將控件動態添加到面板。例如,看看[這篇文章](https://stackoverflow.com/questions/34426888/dynamic-button-creation-placing-them-in-a-predefined-order-using-c-sharp)。但是如果你繼續這種方法,下一步將是如何從數字上下文中讀取值,以及如何知道哪個值與哪個項目等有關。我建議使用數據綁定和'DataGridView'來代替。 –