我想知道即使在页面加载后仍然保留组合框中保存的值的代码 [英] i want to know the code to make remain the saved values in the combobox even after the page load

查看:43
本文介绍了我想知道即使在页面加载后仍然保留组合框中保存的值的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 尝试 
{
SyS_comboBox1.DataSource = ABLL.BindAC_Name();
SyS_comboBox1.DisplayMember = AC_NAME;
SyS_comboBox1.ValueMember = AC_NAME;
}
catch (例外ee){}



绑定后我必须保存组合框的值,以便在应用程序重新打开后值仍然保持

解决方案

使用sql server或xml在加载时保存并加载它


有多种技术可用于将会话(打开 - 关闭 - 打开应用程序)中的应用程序/表单设置/数据从轻量级持久保存到重量级:我建议你研究这个帖子:[ ^ ]获取技术的一般概念。



注意,如果你是想知道:WinForm原生控件,如ComboBox,不能直接序列化。



然后,决定哪种技术最适合你使用:应用程序设置(仅限用户?申请范围广?)?以二进制,XML或JSON格式序列化自定义类等等



无论您使用哪种策略,在您的情况下您都需要签入代码对于持久设置的存在,还要确定何时需要刷新当前设置...以及ComboBox内容...再次从数据库中读取。



如果我只需要保留一组与某些特定控件相关的值(比如ComboBox的项集合),并且我认为这些设置不需要经常从数据库中刷新,我可能会使用应用程序设置[ ^ ]。



请注意,Application.Settings只允许一种类型的对象来保存Collection:ArrayList。要使用ArrayList作为应用程序设置,



请注意,在以下示例中,我假设可以在运行时修改ComboBoxItems集合;因此,在这种情况下,您没有绑定到数据库。



1.在解决方案资源管理器窗口的C#项目标题节点中:双击设置。设置:打开设置设计器。



2.单击空白设置的类型字段的下拉列表,然后在树视图中选择浏览:库选择'mscorlib:select System.Collection:select'ArleList



3.在名称字段中为您的物业命名。



4.定义您的Property是用户定义的还是应用程序范围的。 请注意,如果您在应用程序范围内进行操作,则无法使用=直接分配给属性!它没有'setter。



这里有一个例子,说明如何使用名为'ComboBoxXItems'定义为'范围内的用户的ArrayList Applicaton设置:< pre lang =cs> private void Form1_Load( object sender,System.EventArgs e)
{
if (Settings.Default.ComboBoxXItems == null
{
// 从数据库填充ComboBox !
}
else
{
// 清除theComboBox并从保存的设置中重新填充
comboBox1.Items.Clear();
comboBox1.Items.AddRange(Settings.Default.ComboBoxXItems.ToArray());
}
}

// 仅用于测试:代码到模拟在运行时添加ComboBoxItem
// 以确保运行时添加的项目保存在设置
private int count = 1 ;

private void button1_Click( object sender,System.EventArgs e)
{
comboBox1.Items.Add( 新项目 + count.ToString());
count ++;
}

// 表单结束事件是当前ComboBox项目的位置
// 保存到设置
private void Form1_FormClosing( object sender,FormClosingEventArgs e)
{
// 捕获当前的ComboBox项
ArrayList CBxItemsToSave = < span class =code-keyword> new
ArrayList( this .comboBox1.Items);

if (Settings.Default.ComboBoxXItems == null
{
// 如果我们用适当的项目初始化ComboBox
// 那么这里有一个错误

// 仅供测试
Settings.Default.ComboBoxXItems = new 数组列表();
}
其他
{
// < span class =code-comment>仅当设置为非null时才清除该设置!
Settings.Default.ComboBoxXItems.Clear();
}

// 刷新设置
Settings.Default.ComboBoxXItems.AddRange(CBxItemsToSave);

// 保存设置
Settings.Default.Save ();
}

开始使用一种技术,或者全部探索(最好),然后回答具体问题和代码示例。


try
           {
               SyS_comboBox1.DataSource = ABLL.BindAC_Name();
               SyS_comboBox1.DisplayMember = "AC_NAME";
               SyS_comboBox1.ValueMember = "AC_NAME";
           }
           catch (Exception ee) { }


after binding i have to save the value of combobox so that the value shoul remain even after the app was re opened

解决方案

use sql server or xml to save and load it on the time of loading


There are a variety of techniques that can be used to persist Application/Form settings/data across "sessions" (opening-closing-opening the Application) from "light-weight" to "heavy-weight:" I suggest you study this thread: [^] to get general ideas of the techniques.

Note, in case you were wondering: WinForm native Controls, like ComboBox, are not directly serializable.

Then, decide which technique is best for you to use: Application Settings (user only ? application wide ?) ? Serialize a Custom Class in Binary, XML, or JSON format ?, etc.

Whichever strategy you use, in your case you are going to need to check in your code for the existence of persisted settings, and also decide when you need to refresh the current settings ... and the ComboBox content ... by reading from the Database again.

If I only needed to persist one set of values related to some specific Control (like a ComboBox's Items Collection), and I believed those settings would not need to be refreshed form the Database often, I might use Application Settings [^].

Note that Application.Settings allows only one Type of object that will hold a Collection: the ArrayList. To use an ArrayList as an Application Setting,

Note that in the following example I've made the assumption that the ComboBoxItems Collection may be modified at run-time; so, in this case, you are not binding to the Database.

1. in the C# project title node in the Solution Explorer Window: double-click on Settings.settings: that opens the Settings Designer.

2. click on the dropdown of the Type field of a "blank" setting and select "Browse:" in the treeview of libraries select 'mscorlib: select System.Collection: select 'ArrayList

3. give your Property a Name in the 'Name field.

4. define whether your Property is user-defined or application-wide. Note that if you make it application-wide then you cannot assign to the Property directly with = ! It will have no 'setter.

Here's an example of how an ArrayList Applicaton Setting named 'ComboBoxXItems defined as 'user in scope might be used:

private void Form1_Load(object sender, System.EventArgs e)
{
    if (Settings.Default.ComboBoxXItems == null)
    {
        // fill the ComboBox from the Database !
    }
    else
    {
        // clear theComboBox and refill it from the saved Setting
        comboBox1.Items.Clear();
        comboBox1.Items.AddRange(Settings.Default.ComboBoxXItems.ToArray());
    }
}

// for testing only: code to simulate adding a ComboBoxItem at run-time
// to make sure that the run-time added Items are saved in the Setting
private int count = 1;

private void button1_Click(object sender, System.EventArgs e)
{
    comboBox1.Items.Add("new item " + count.ToString());
    count++;
}

// the Form Closing Event is where the current ComboBox Items
// are saved to the Setting
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // capture the current ComboBox Items
    ArrayList CBxItemsToSave = new ArrayList(this.comboBox1.Items);
      
    if (Settings.Default.ComboBoxXItems == null)
    {
        // if we initialized the ComboBox with Items properlyt
        // then there is an error here

        // for testing only
        Settings.Default.ComboBoxXItems = new ArrayList();
    }
    else
    {
        // clear the setting only if it is non-null !
        Settings.Default.ComboBoxXItems.Clear();
    }

    // refresh the Setting
    Settings.Default.ComboBoxXItems.AddRange(CBxItemsToSave);

    // save the Setting
    Settings.Default.Save();
}

Get started using one technique, or explore them all (best), and come back with specific questions, and code examples.


这篇关于我想知道即使在页面加载后仍然保留组合框中保存的值的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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