editing items in a listbox
G'day all
I have a listbox with 16 lines (strings 32 char max). These lines populate a dropdown menu according to a maximum number of items selected eg if the number of items is 5 then a for next loop fills up only 5 items of the dropdown menu. This part works well, what I would like to do is for the user to be able to edit the lines of text in the listbox and then the application should save the new data and restore it at the next restart. Is there a simple way of doing this without having to create a database? I guess my old .ini file is a mini data base but there may be a way of say creating an array and then saving the data somehow...don't know how
Thanks for the reply. My total experience with VB totals to about 3 weeks now. I have read the above and still confused.
I do populate the combobox (using item.add) from the listbox values but can't seem to be able to modify (edit) the values from the listbox so that it can be done at run time by the user, it seems to be read only. The example seems to show that the values are "hard wired" into the code (city, state) pretty much like what I have now.
Also how do I tell the application to save the modified settings on close and reload them at startup? I can do it with other settings like a combobox text or a checked state of a checkbox, by making it a binding property but can't seem to find the equivalent for the listbox (eg make items or lines binding).
Anyway I'll do a bit more reading to see if I can figure it out.
>Are you already familiar with serialization in .NET..
see above 
Ok...
The idea with the DataSource is that you separate the data from the view. So you have a data model that is viewed by the combobox. You need to alter the DataSource in order to update the view. I believe you will need to specifically synchronize the contents using a specific call which I don't know by heart. So indeed, you shouldn't try to add items manually into the combobox because that would get your data source out of sync. Does that make sense?
I can't give you more detailed info here, haven't got VS installed, I hope I can give you more specifics tomorrow if still needed.
As promised, a complete sample on how to add items to a datasource, how to serialize and deserialize an object. This sample should do it all. I've put buttons on the form for ease of use.
Hmm I see that you're a VB guy... <DOH>. Use this translator to translate from C# to VB.
| |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Collections; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace ComboBoxDataSource { public partial class Form1 : Form { private SomeModel m_SomeModel = new SomeModel(); public Form1() { InitializeComponent(); this.comboBox1.DataSource = m_SomeModel.List; } private void m_AddItemButton_Click( object sender, EventArgs e ) { // I wonder whether there are better ways to accomplish the updating of the // DataSource, but this works for now. m_SomeModel.List.Add( "New ITEM!" ); this.comboBox1.DataSource = null; this.comboBox1.DataSource = m_SomeModel.List; } private void m_LoadButton_Click( object sender, EventArgs e ) { if( openFileDialog1.ShowDialog( this ) == DialogResult.OK ) { FileStream fileStream = null; try { fileStream = new FileStream( openFileDialog1.FileName, FileMode.Open, FileAccess.Read ); BinaryFormatter formatter = new BinaryFormatter(); m_SomeModel = (SomeModel)formatter.Deserialize( fileStream ); comboBox1.DataSource = m_SomeModel.List; } catch( UnauthorizedAccessException ex ) { MessageBox.Show( this, ex.Message, "Cannot open file" ); } catch( Exception ex ) { MessageBox.Show( this, ex.Message, "Error loading file" ); } finally { if( fileStream != null ) { fileStream.Close(); } } } } private void m_SaveButton_Click( object sender, EventArgs e ) { if( saveFileDialog1.ShowDialog( this ) == DialogResult.OK ) { FileStream fileStream = null; try { fileStream = new FileStream( saveFileDialog1.FileName, FileMode.Create ); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize( fileStream, m_SomeModel ); } catch( UnauthorizedAccessException ex ) { MessageBox.Show( this, ex.Message, "Cannot write file" ); } catch( Exception ex ) { MessageBox.Show( this, ex.Message, "Error writing file" ); } finally { if( fileStream != null ) { fileStream.Close(); } } } } /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose( bool disposing ) { if( disposing && ( components != null ) ) { components.Dispose(); } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.comboBox1 = new System.Windows.Forms.ComboBox(); this.m_AddItemButton = new System.Windows.Forms.Button(); this.m_LoadButton = new System.Windows.Forms.Button(); this.m_SaveButton = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); this.SuspendLayout(); // // comboBox1 // this.comboBox1.FormattingEnabled = true; this.comboBox1.Location = new System.Drawing.Point( 42, 60 ); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size( 150, 21 ); this.comboBox1.TabIndex = 0; // // m_AddItemButton // this.m_AddItemButton.Location = new System.Drawing.Point( 42, 111 ); this.m_AddItemButton.Name = "m_AddItemButton"; this.m_AddItemButton.Size = new System.Drawing.Size( 75, 23 ); this.m_AddItemButton.TabIndex = 1; this.m_AddItemButton.Text = "Add Item"; this.m_AddItemButton.UseVisualStyleBackColor = true; this.m_AddItemButton.Click += new System.EventHandler( this.m_AddItemButton_Click ); // // m_LoadButton // this.m_LoadButton.Location = new System.Drawing.Point( 42, 151 ); this.m_LoadButton.Name = "m_LoadButton"; this.m_LoadButton.Size = new System.Drawing.Size( 75, 23 ); this.m_LoadButton.TabIndex = 2; this.m_LoadButton.Text = "Load"; this.m_LoadButton.UseVisualStyleBackColor = true; this.m_LoadButton.Click += new System.EventHandler( this.m_LoadButton_Click ); // // m_SaveButton // this.m_SaveButton.Location = new System.Drawing.Point( 123, 151 ); this.m_SaveButton.Name = "m_SaveButton"; this.m_SaveButton.Size = new System.Drawing.Size( 75, 23 ); this.m_SaveButton.TabIndex = 3; this.m_SaveButton.Text = "Save"; this.m_SaveButton.UseVisualStyleBackColor = true; this.m_SaveButton.Click += new System.EventHandler( this.m_SaveButton_Click ); // // openFileDialog1 // this.openFileDialog1.FileName = "openFileDialog1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F ); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size( 319, 267 ); this.Controls.Add( this.m_SaveButton ); this.Controls.Add( this.m_LoadButton ); this.Controls.Add( this.m_AddItemButton ); this.Controls.Add( this.comboBox1 ); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout( false ); } #endregion private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.Button m_AddItemButton; private System.Windows.Forms.Button m_LoadButton; private System.Windows.Forms.Button m_SaveButton; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.SaveFileDialog saveFileDialog1; } [Serializable] public class SomeModel { private ArrayList m_List = new ArrayList(); public ArrayList List { get { return m_List; } set { m_List = value; } } public SomeModel() { m_List.Add( "Value1" ); m_List.Add( "Value2" ); m_List.Add( "Value3" ); } } }
|
Does this answer your question?
Hello Jelle
Thanks for going to all the trouble. After some messing around, (translation problems?) I manage to get it all working. All I need to do is digest it and apply in my project 
The translation added
Namespace ComboBoxDataSource at the top of the form which didn't agree with VB, removing that and also removing End Namespace got it working. I changed the line:
m_SomeModel.List.Add("New ITEM!")
to
m_SomeModel.List.Add(comboBox1.Text)
And now I can save whatever gets typed in the combobox.