C# 在c中的windows窗体上添加项和展开checkedlistbox集合列表的问题#
这似乎是个愚蠢的问题。我有一个文本框,可用于在运行时在windows窗体上将项目添加到checkedlistbox。我用的是c。它在运行时工作得非常好。当表单打开时,将添加项并填充。但是,当我再次关闭并打开表单时,在checkedlistbox列表中看不到添加的项。注意,我不使用数据源,也不想使用。我不想硬编码任何东西,我更愿意使用表单上的文本框输入作为变量输入到集合列表中。我想不出扩展checkedlistbox选项的方法。如果您有任何帮助,我们将不胜感激。您如何打开表格?是不是有点像:C# 在c中的windows窗体上添加项和展开checkedlistbox集合列表的问题#,c#,winforms,checkedlistbox,C#,Winforms,Checkedlistbox,这似乎是个愚蠢的问题。我有一个文本框,可用于在运行时在windows窗体上将项目添加到checkedlistbox。我用的是c。它在运行时工作得非常好。当表单打开时,将添加项并填充。但是,当我再次关闭并打开表单时,在checkedlistbox列表中看不到添加的项。注意,我不使用数据源,也不想使用。我不想硬编码任何东西,我更愿意使用表单上的文本框输入作为变量输入到集合列表中。我想不出扩展checkedlistbox选项的方法。如果您有任何帮助,我们将不胜感激。您如何打开表格?是不是有点像: Fo
FormName form = new FormName();
form.Show()
我认为发生这种情况的唯一原因是,每次显示新的表单实例时,您都在实例化它,而不是重复使用相同的表单。您是如何打开表单的?是不是有点像:
FormName form = new FormName();
form.Show()
我认为发生这种情况的唯一原因是,每次显示新表单实例时,您都在实例化它,而不是重复使用同一表单。让您的表单将
引用列表值作为参数。然后将其作为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;
}
}
类MyForm:Form{
列表值;
绑定源;
公共MyForm()
{
初始化组件();
}
公共MyForm(参考列表值):this()
{
如果(值==null)
值=新列表();
这个值=值;
checkedListBox1.DisplayMember=“Value”;
checkedListBox1.ValueMember=“Value”;
source=新的BindingSource(this.values,null);
checkedListBox1.DataSource=源;
}
私有void AddItemButton\u单击(对象发送者,事件参数e)
{
this.source.Add(textBox1.Text);
textBox1.Text=string.Empty;
}
}
让表单将参考列表值
作为参数。然后将其作为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;
}
}
类MyForm:Form{
列表值;
绑定源;
公共MyForm()
{
初始化组件();
}
公共MyForm(参考列表值):this()
{
如果(值==null)
值=新列表();
这个值=值;
checkedListBox1.DisplayMember=“Value”;
checkedListBox1.ValueMember=“Value”;
source=新的BindingSource(this.values,null);
checkedListBox1.DataSource=源;
}
私有void AddItemButton\u单击(对象发送者,事件参数e)
{
this.source.Add(textBox1.Text);
textBox1.Text=string.Empty;
}
}
私有无效frmMain\u加载(对象发送方,事件参数e)
{
如果(!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
{
string[]checkedDictions=Properties.Settings.Default.CheckedItems.Split(新字符[]{',},StringSplitOptions.RemoveEmptyEntries);
for(int i1=0;i1=(idx+1)))
{
checkedListBox1.SetItemChecked(idx,true);
}
}
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
如果(textBox1.Text!=“”)
{
textBox1.MaxLength=15;
//将输入的所有文本更改为小写。
textBox1.CharacterCasing=CharacterCasing.Lower;
if(checkedListBox1.Items.Contains(textBox1.Text)==false)
{
checkedListBox1.Items.Add(textBox1.Text,CheckState.Checked);
textBox1.Text=“”;
MessageBox.Show(“添加!单击移动查看列表框”);
}
其他的
{
MessageBox.Show(“已经存在了!”);
textBox1.Text=“”;
}
}
}
私有无效frmMain_FormClosing(对象发送方,FormClosingEventArgs e)
{
string idx=string.Empty;
for(int i1=0;i1
私有无效frmMain\u加载(对象发送方,事件参数e)
{
如果(!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
{
string[]checkedDictions=Properties.Settings.Default.CheckedItems.Split(新字符[]{',},StringSplitOptions.RemoveEmptyEntries);
for(int i1=0;i1=(idx+1)))
{
checkedListBox1.SetItemChecked(idx,true);
}
}
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
如果(textBox1.Text!=“”)
{
textBox1.MaxLength=15;
//将输入的所有文本更改为小写。
textBox1.CharacterCasing=CharacterCasing.Lower;
if(checkedListBox1.Items.Contains(textBox1.Text)==false)
{
checkedListBox1.Items.Add(textBox1.Text,CheckState.Checked);
textBox1.Text=“”;
MessageBox.Show(“添加!单击移动查看列表框”);
}
其他的
{
MessageBox.Show(“已经存在了!”);
textBox1.Text=“”;
}
}
}
私有无效frmMain_FormClosing(对象发送方,FormClosingEventArgs e)
{
string idx=string.Empty;
for(int i1=0;i1