簡體   English   中英

在c#中添加項目並在windows窗體上展開checkedlistbox的集合列表時出現問題

[英]Issues adding items and expanding the collections list of checkedlistbox on a windows form in c#

這似乎是一個愚蠢的問題。 我有一個文本框,可用於在Windows窗體上運行時將項目添加到checkedlistbox。 我正在使用c#。 它在運行時完美運行。 當表單打開時,項目會被添加和填充。 但是,當我再次關閉並打開表單時,我沒有在checkedlistbox列表中看到添加的項目。 注意,我不使用數據源而不想。 我不想硬編碼任何東西,並且更喜歡使用表單上的文本框輸入作為變量來輸入集合列表。 我無法找到擴展我的checkedlistbox選項的方法。 任何援助將不勝感激。

你是如何打開表格的? 是這樣的:

FormName form = new FormName();
form.Show()

我認為發生這種情況的唯一原因是,每次顯示時都會實例化一個新的表單實例,而不是重復使用相同的表單。

讓您的表單將ref List<string> values作為參數。 然后將其作為CheckedListBox的BindingSource。

這是代碼:

class MyForm : Form {
        List<string> values;
        BindingSource source;

        public MyForm()
        {
            InitializeComponent();
        }

        public MyForm(ref List<string> values):this()
        {
            if (values == null)
                values = new List<string>();

            this.values = values;

            checkedListBox1.DisplayMember = "Value";
            checkedListBox1.ValueMember = "Value";
            source = new BindingSource(this.values, null);
            checkedListBox1.DataSource = source;
        }

        private void AddItemButton_Click(object sender, EventArgs e)
        {
            this.source.Add(textBox1.Text);
            textBox1.Text = string.Empty;
        }
}
private void frmMain_Load(object sender, EventArgs e)
{
  if (!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
  {
    string[] checkedIndicies = Properties.Settings.Default.CheckedItems.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    for (int i1 = 0; i1 < checkedIndicies.Length; i1++)
    {
      int idx;
      if ((int.TryParse(checkedIndicies[i1], out idx)) && (checkedListBox1.Items.Count >= (idx+1)))
      {
        checkedListBox1.SetItemChecked(idx, true);
      }
    }
  }
}

private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
        {
            textBox1.MaxLength = 15;
            // Change all text entered to be lowercase.
            textBox1.CharacterCasing = CharacterCasing.Lower;

            if (checkedListBox1.Items.Contains(textBox1.Text) == false)
            {
                checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked);

                textBox1.Text = "";
                MessageBox.Show("Added! Click Move to see List Box");
            }

            else
            {
                MessageBox.Show("Already There!");
                textBox1.Text = "";
            }
        }
    }



private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
  string idx = string.Empty;
  for (int i1 = 0; i1 < checkedListBox1.CheckedIndices.Count; i1++)
    idx += (string.IsNullOrEmpty(idx) ? string.Empty : ",") + Convert.ToString(checkedListBox1.CheckedIndices[i1]);
  Properties.Settings.Default.CheckedItems = idx;
  Properties.Settings.Default.Save();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM