在 C# 中的 Windows 窗体上添加项目和扩展 checklistbox 的集合列表时出现问题

发布于 2024-07-26 00:46:03 字数 247 浏览 6 评论 0原文

这可能看起来是一个愚蠢的问题。 我有一个文本框,可用于在 Windows 窗体上运行时将项目添加到选中列表框中。 我正在使用c#。 它在运行时工作得非常好。 当表单打开时,该项目将被添加。 但是,当我关闭并再次打开表单时,我在 checklistbox 列表中看不到添加的项目。 请注意,我不使用数据源,也不想使用。 我不想对任何内容进行硬编码,并且更愿意使用表单上的文本框输入作为变量来输入集合列表。 我无法找到扩展我的复选框选项的方法。 任何援助将不胜感激。

This may seem like a dumb question. I have a textbox that can be used to add items to a checkedlistbox at runtime on a windows form. I'm using c#. It works perfectly fine at runtime. The item gets added and stuff, when the form is open. But, when I close and open the form again, I don't see the added item in the checkedlistbox list. Note, I don't use a datasource and don't want to. I wouldn't want to hardcode anything and would prefer to use a textbox input on the form as a variable to feed into the collections list. I couldn't figure out a way to expand my checkedlistbox options. Any assistance would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

滥情哥ㄟ 2024-08-02 00:46:03

你如何打开表格? 是这样的吗:

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

我认为发生这种情况的唯一原因是您每次显示它时都实例化一个新的表单实例,而不是重用相同的表单。

How are you opening the form? Is it something like:

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

The only reason I can think that's happening is that you're instantiating a new form instance every time you show it, instead of reusing the same form.

玩物 2024-08-02 00:46:03

让您的表单采用ref List; 值作为参数。 然后将其设置为 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;
        }
}

Have your Form take a ref List<string> values as parameter. Then make this as BindingSource for the CheckedListBox.

Here is the code:

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;
        }
}
和我恋爱吧 2024-08-02 00:46:03
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();
}
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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文