Add combobox item from another form won't work

how to set value in combobox in c# windows application
how to add items in combobox in c# windows application
how to get selected value of combobox in c# windows application
how to get selected text of combobox in c# windows application
vb.net combobox add item and value
combobox c# selecteditem
c# combobox selectedindexchanged
visual basic combobox selected item

I want to add an item to my combobox in form1 from form2. But if I press the button, form2 closes but the item is not added in form1. I hope you can help me! I found no solution for this problem in the internet.

Form 2:

public void button5_Click(object sender, EventArgs e)
{
    Form1 main = new Form1();

    main.AddItem("Item");
    this.Close();
}

Form 1:

public void AddItem(object item)
{
    comboBox1.Items.Add(item);
}

First of all: You have to change comboBox access modifier to public. Then:

Form 2:

public void button5_Click(object sender, EventArgs e)
{
     Form1 main = new Form1();
     main.AddItem("Item");

     this.Hide(); // This will hide Form2 ("this." is redundant)
     main.ShowDialog(); // This will show Form1
}

How to add combobox item from one form to another form combobox , Here is 2 solutions for your problem. -1-Sol 1 : Add a parentForm refernce for all non main form. => when you create a new form, you init your� I want to add an item to my combobox in form1 from form2. But if I press the button, form2 closes but the item is not added in form1. I hope you can help me! I found no solution for this problem in the internet. Form 2: public void button5_Click(object sender, EventArgs e) { Form1 main = new Form1(); main.AddItem("Item"); this.Close(); }

Reference the name property of your 'Form1' don't create a new instance. Then reference that forms combobox control.

Your original code creates a NEW instance. Meaning its creating a new version of Form1 and not accessing the original!

The code below should help

  public void button5_Click(object sender, EventArgs e)
        {

          Form1 myForm = whateverYourFormsNamePropertyIs;
          myForm.Combobox.AddItem("Item");
          this.Close();

        }

Combobox value depends on the selection of another combobox in , show first combobox selected item to another combobox.Duration: 5:34 Posted: Aug 29, 2018 Here, the ComboBox.ObjectCollection indicates the elements present in the ComboBox. Following steps are used to add the elements in the ComboBox: Step 1: Create a combobox using the ComboBox() constructor is provided by the ComboBox class. // Creating ComboBox using ComboBox class ComboBox mybox = new ComboBox();

In form2...

    Form1 f;
    public Form2(Form1 parent)
    {
        InitializeComponent();
        f = parent;
    }
    private void Add_Click(object sender, EventArgs e)
    {
        f.comboBox1.Items.Add("item");
    }

In form1

public void AddItem(object item)
{
  comboBox1.Items.Add(item);
  Form2 f = new Form2(this);
  f.Show();
}

Add and Remove Items from ComboBox, ListBox, or , Learn how to add and remove a Windows Forms ComboBox, ListBox, and CheckedListBox controls simply and with no data binding. ComboBox.ObjectCollection. A ComboBox.ObjectCollection representing the items in the ComboBox. Examples. The following code example demonstrates how to add a small number of ComboBox items. If you need to add a large number of items, see the example at BeginUpdate. Both examples are part of a larger example available in the ComboBox class overview.

orginal form

private void FormPeople_Load(object sender, EventArgs e)
    {
        populateComboBoxTitles();
}
public void populateComboBoxTitles()
        {
            comboBox2.Items.Clear();
            comboBox2.Items.Add("mr");
             comboBox2.Items.Add("miss");

}
private void button5_Click(object sender, EventArgs e)
    {
FormAddTitle formAddTitle = new FormAddTitle(this);
        formAddTitle.Show();
}

secondary form

 FormPeople formPeople;
    public FormAddTitle(FormPeople formPeople)
    {
        InitializeComponent();
        this.formPeople = formPeople;
    }
private void button1_Click(object sender, EventArgs e)
    {
if (formPeople != null)
            formPeople.populateComboBoxTitles();
}

ComboBox Class (System.Windows.Forms), Represents a Windows combo box control. how you can use the Add method to add items to a ComboBox, the FindString method to find items Form { private System. are not empty, they become read-only when you derive another form. How to Add Item not in the List of Combo box A combo box is mostly a list of items linked to table or query. It also can be a list of item that created inside the combo box as a value list. In Access, mostly the item in the combo box is linked to the ID or primary key field of that item from its table. However, the ID field is usually hidden and will display only the second field of the data

Limit the Items in One Combo Box Based on the Selected Item in , Limit the Items in One Combo Box Based on the Selected Item in Another When you place two combo boxes on a form, Access by default doesn't link You could use a macro to accomplish this, but adding a VBA procedure is just as easy. On one of the forms I have a combobox that won't let me select a value from it. To clarify, the combobox is displaying the first value, when I hit the dropdown the other values are properly displayed. When I click on another value from the list, the original value remains and nothing changes.

ComboBox In C#, Only one list item is displayed at one time in a ComboBox and other set its properties and add ComboBox instance to the Form controls. Another option is to let users add items to the control's list on the fly. This choice is easy for users -- they simply enter the item into the control's text box component. Your part takes a

Using ComboBox In Windows Forms, Let's add a ComboBox control to the form by dragging it from Toolbox and enables the user to select items from the list or enter a new value. How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control. 03/30/2017; 2 minutes to read +8; In this article. Items can be added to a Windows Forms combo box, list box, or checked list box in a variety of ways, because these controls can be bound to a variety of data sources.

Comments
  • It's because you create new form and then destroy it. You must have reference to form1 instance, instead of creating new one.
  • stackoverflow.com/questions/1736444/…
  • Thank You! This works! But if I click the button, form1 is opened 2 times. How can I close the first form1 if I click the where and the second form1 opens?
  • You can remove "main.ShowDialog()".
  • I tried it but I get this Error: (translated from german) Error 7 "Launcher.Form1" is a "type", but its used a a variable. I used: Form1 myForm = Form1;
  • stackoverflow.com/questions/7273862/… Here is a good example of how to reference other forms within your application. You'll need to pass the reference into Form2 on construction
  • Fix your answer; don't post yet another link.