永久添加项目到组合框? [英] Add items to combobox permanently ?

查看:57
本文介绍了永久添加项目到组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,有时我需要在组合框中添加一个新项目。我用这个简单的代码做了这个,但问题是,每次我重新启动程序时,它都不存在了。我怎么能添加一些东西,所以下次我打开程序时它会在那里?没有数据库。



I have a program, where I need to add a new item to a combobox sometimes. I did this with this simple code but the problem is, everytime I restart the program, its not there anymore.How can I add somethings so it will be there the next times I open the program too? without a database .

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    Random r = new Random();
    int aa=r.Next(comboBox1.Items.Count);
    comboBox1.Text = comboBox1.Items[aa].ToString();
}

private void button2_Click(object sender, EventArgs e)
{
    if (!(textBox1.Text==""))
    {
        comboBox1.Items.Add(textBox1.Text);

    }
    if (textBox1.Text == "")
    {
        MessageBox.Show("gsdgsdgs");
    }
}

推荐答案

启动程序时,组合框中填充了您在设计器中指定的项目。如果你想要其他项目(比如保存在程序出口并在启动时再次加载),那么你必须将它们保存在某处并在代码中自行恢复。



最简单的方法可能是将它们放入设置文件中:

1)在Visual Studio中打开项目,然后查看解决方案资源管理器窗格。

2)打开Properties下拉菜单,双击Settings.Settings分支。

3)在结果页面中,添加新项目:给它一个合理的名称,设置类型到字符串,并将范围留在用户。将值设置为Option1 | Option2

4)关闭页面。

5)在Load事件中,读取值:

When you start your program, the combo box is filled with the items you specify in the designer. If you want to have other items (say that are saved on program exit and loaded again on start up) then you have to save them somewhere and restore them yourself in the code.

The easiest way to do that may be to put them into the Settings file:
1) Open your project in Visual Studio, and look at the Solution Explorer pane.
2) Open the Properties dropdown, and double click the Settings.Settings branch.
3) In the page that results, add your new item: Give it a sensible name, set the "Type" to "String", and leave the "Scope" at "User". Set the Value to "Option1|Option2"
4) Close the page.
5) In your Load event, read the values:
string setting = Properties.Settings.Default.MySettingString;
string[] comboRows = setting.Split('|');

现在可以使用数组加载组合框。

6)您可以通过构建单个字符串来保存当前值它们(由''|''字符分隔)然后:

You can now use the array to load your combobox.
6) You can save the current values by building a single string from them (separated by ''|'' characters) and then:

Properties.Settings.Default.MySettingString = setting;
Properties.Settings.Default.Save();


取决于您对数据库的定义。



您必须使用某种持久存储来存储要添加到组合框的标志或项列表。当应用程序启动时,它需要从持久存储中读取并填充组合框。当添加新项目时,需要更新持久存储。



持久存储可以是应用程序关闭时持续存在的任何内容(并且关闭电脑) )。



您可以发送电子邮件给石匠,当您向他发送写信息并将其重新输入时,将数据凿成花岗岩块响应读取消息的电子邮件 - 但是延迟和费用使得它有问题。



最典型的选择是:



(a)你维护的文件应用程序数据目录或程序目录。它可以是纯文本或xml或二进制文件,具体取决于您如何存储和检索数据,但是您自己完成。



(b)注册表设置



(c)传统数据库(此应用程序的过度杀伤)。
Depends on your definition of "database".

You have to use some sort of persistent storage to store a flag or list of items to add to your combo box. When the application starts up it needs to read from the persistent storage and populate the combo box. When a new item is added the persistent storage needs to be updated.

The "persistent storage" can be anything that persists when the application closes (and the pc shuts down).

You could send an email to stone mason who chisels the data into a block of granite when you send him a write a message and who types it back in to an email in response to a read message -- but the latency and expense of that makes it questionable.

The most typical choices for this are:

(a) a file that you maintain in the application data directory or program directory. It could be plain text or xml or binary depending on how you wan to store and retrieve the data, but you do it yourself.

(b) a registry setting

(c) a traditional database (overkill for this application).


这篇关于永久添加项目到组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆