Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不要在组合框中保存项目_C# - Fatal编程技术网

C# 不要在组合框中保存项目

C# 不要在组合框中保存项目,c#,C#,我有一个文本框、一个按钮和一个组合框 单击按钮时,我希望文本框中的文本添加到组合框项目中 这是我的密码: private void button1_Click(object sender, EventArgs e) { comboBox1.Items.Add(textBox1.Text); } 我的表格直到打开。此文本显示在组合框中,但当我关闭表单并再次打开它时,文本不再显示在组合框中 我想将文本保存到组合框的集合项中。我不想使用数据库。正如其他

我有一个文本框、一个按钮和一个组合框
单击按钮时,我希望文本框中的文本添加到组合框项目中

这是我的密码:

        private void button1_Click(object sender, EventArgs e)
    {
        comboBox1.Items.Add(textBox1.Text);
    }
我的表格直到打开。此文本显示在组合框中,但当我关闭表单并再次打开它时,文本不再显示在组合框中


我想将文本保存到组合框的集合项中。我不想使用数据库。

正如其他人在评论中提到的,您需要了解并决定将值存储在何处

在我的示例中,我创建了一个简单的文本文件来存储这些值。代码读取文件并将每一行作为一项添加到组合框中

        private void Form1_Load(object sender, EventArgs e)
        {
            // Read items from file into a string array
            string[] items = System.IO.File.ReadAllLines(@"D:\ComboBoxValues.txt");

            // Add items to the comobobox when opening the form
            comboBox1.Items.AddRange(items);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Add your new value to the combobox
            comboBox1.Items.Add(textBox1.Text);

            // Put all existing comobo box items into a string array
            string[] items = comboBox1.Items.OfType<string>().ToArray();

            // Save the array of items to a text file (this will not append, it will re-write the file)
            System.IO.File.WriteAllLines(@"D:\ComboBoxValues.txt", items);
        }
private void Form1\u加载(对象发送方,事件参数e)
{
//将项目从文件读入字符串数组
string[]items=System.IO.File.ReadAllLines(@“D:\ComboBoxValues.txt”);
//打开表单时将项目添加到ComboBox
comboBox1.Items.AddRange(Items);
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
//将新值添加到组合框中
comboBox1.Items.Add(textBox1.Text);
//将所有现有的comobo框项放入字符串数组中
string[]items=comboBox1.items.OfType().ToArray();
//将项目数组保存到文本文件(这不会追加,它将重新写入文件)
System.IO.File.WriteAllines(@“D:\ComboBoxValues.txt”,items);
}

这可能不是最优雅的方式,但从让您理解的角度来看,这应该足够了。

如果您不喜欢使用文件系统,您可以使用首选项(但不建议使用首选项来记忆大值),请查看此链接以了解如何使用

private void Form1\u加载(对象发送方,事件参数e)
{
string[]strItems=Properties.Settings.Default.items.Split(“,”);
for(int i=0;i
您想将它们存储在哪里?您必须使用某种存储,可能是数据库(表)、磁盘上的文件,甚至可能是在云中存储数据的东西。这需要努力,你需要决定你愿意付出什么努力。话虽如此,在我看来,预先排除数据库似乎不是一个非常明智的立场。因为你似乎只是在处理一些文本,你也可以研究一个简单的文本文件或用户设置。你真的需要多研究一点,因为你似乎认为填充列表会以某种方式自动保存。我有消息告诉你,这在C#不会发生。要将其输入数据库,还需要做很多工作。试着复习一下数据库,如何连接以及如何创建、读取、更新和删除(CRUD)数据。