Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
private void btnok_Click(object sender, EventArgs e)
{
    string name = tbxname.Text;
    Form2 ff = new Form2();
    ff.comboBox1.Items.Add(name);
    ff.ShowDialog();
}
 
The above given code is present in form1. btnok.
I want to add some item to form2.combobox1 through this code(combobox in form2 is declared as public).it updated in the combobox items.But when we close the solution and again run it,again there is only one item,the old item is gone.how do i achieve it.
Your help is needed.
Posted 30-Nov-10 6:15am
Edited 30-Nov-10 15:16pm
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are aware that you actually have to set the selected item in the combobox before it shows up in the edit field, right?
 
ff.comboBox1.SelectedIndex = 0;
or
 
ff.comboBox1.SelectedItem = name;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

In addition to John's answer, the ComboBox on Form 2 needs to have its accessor changed to public.
 
It might help you to get a more specific answer if you tell us what 'But its not working.' actually means. In what way is it not working?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If I understand correctly, you're using a button on form1 to dynamically add an additional item to form2.combobox at runtime. However, when you "close the solution and run again" the combobox on form2 only displays a single item (that you presumably added at design time or application load)?
 
If you want to dynamically add an item to the second drop down and have the data persist across an application restart you'll need to store the data in a file or database, then re-load or bind the comboboxes to said file/database each time the appplication loads.
 
Please clarify your question...
  Permalink  
Comments
johnsathish at 1-Dec-10 1:26am
   
What you understood is exactly correct,But i dont get your solution clearly.If possible give me example with code.Thank you!
ROGDEV at 1-Dec-10 1:32am
   
Hello, you need to decide how you want to persist the new item you are adding to the combo box. Every time you start/stop your application those dynamically added items will be erased from memory unless you store it to disk or database. Decide if you need to store it in a database (Access, SQL Server) or on disk (XML, Dataset, flat text, CSV, etc). This is up to you and how you decide to build your application...
 
Here are a few links to point you in the right direction..
 
http://dotnet.itags.org/dotnet-c-sharp/200224/
http://www.vbdotnetforums.com/windows-forms/27422-load-combo-box-text-file.html
 
http://stackoverflow.com/questions/376622/c-how-do-you-save-a-list-of-items-like-a-combobox-to-the-net-settings-file
 
http://msdn.itags.org/visual-csharp/52804/
 
http://bytes.com/topic/c-sharp/answers/252379-combo-box-load-table
 
Good luck
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

As in previously stated answers, dynamically inserted elements will disappear if the program restarts. This is simply because they are not recorded to a physical drive and will be removed as soon as the garbage collector realizes their work is done.
Simplest way to avoid this is to write contents of a combobox to a text file (if you do not want to meddle with databases) and save that file (as comboboxName.txt, for example). The second time program opens and shows form2, first add the items in the text document to the combobox.
/*write contents of a combobox to a file in a directory. the file location can be a predefined one or you may prompt the user for location by savefiledialog */
//this one saves to MyDocuments
//use on OnClosing event of the form - first delete existing file by system.IO [File.Delete - not in this code]
StringBuilder sb = new StringBuilder();
foreach(var item in ComboboxName.Items)
{
    sb.AppendLine(item.ToString());
}
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
using(StreamWriter _sw = new StreamWriter(folderPath+@"\ComboboxName.txt"))
{
    _sw.Write(sb.ToString());
}
 
//this one reads it from my documents and fills the combobox
//use on loading event of the form
string filePath=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\ComboboxName.txt";
using (StreamReader sr = new StreamReader(filePath)
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    ComboboxName.Items.Add(line);
                }
            }
 
One point is, if you try to save the file into sensitive folders, your program might need to be executed as elevated (i.e. run as administrator)
Of course these codes are almost pseudocodes. For better performance compare existing lines in the text file and combobox and write/delete only if necessary.
  Permalink  
Comments
Dalek Dave at 2-Dec-10 17:13pm
   
Good Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 365
1 OriginalGriff 275
2 Sergey Alexandrovich Kryukov 247
3 George Jonsson 130
4 ChauhanAjay 104
0 OriginalGriff 6,464
1 Sergey Alexandrovich Kryukov 5,915
2 CPallini 5,175
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 2 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100