CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2010
    Posts
    6

    Add ComboBox to Form. Selection not working

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using ShoppingListLib;
    
    namespace ShoppingForms
    {
    public partial class Form2 : Form
    {
    private System.Windows.Forms.ComboBox[] comboBox = new ComboBox [9999];
    
    private System.Windows.Forms.TextBox[] textBox = new TextBox [9999];
    
    
    private string[] storeNameArray = { "Store A", "Store B", "Store C", "Store D", "Store E", "Store F", "Store G", "Store H",};
    private string[] priceArray = new string[8];
    
    public int store_num = 0;
    public int box_location = 54;
    
    public Form2()
    {
    InitializeComponent();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
    // comboBox1
    //
    this.comboBox[store_num] = new System.Windows.Forms.ComboBox();
    this.comboBox[store_num].FormattingEnabl… = true;
    this.comboBox[store_num].Location = new System.Drawing.Point(7, box_location );
    this.comboBox[store_num].Name = "comboBox" ;
    this.comboBox[store_num].Size = new System.Drawing.Size(68, 21);
    this.comboBox[store_num].TabIndex = 0;
    this.comboBox[store_num].DataSource = storeNameArray;
    
    //
    // textBox3
    //
    this.textBox[store_num] = new System.Windows.Forms.TextBox();
    this.textBox[store_num].Location = new System.Drawing.Point(106, box_location);
    this.textBox[store_num].Name = "textBox3";
    this.textBox[store_num].Size = new System.Drawing.Size(76, 20);
    this.textBox[store_num].TabIndex = 1;
    priceArray[store_num] = this.textBox[store_num].SelectedText;
    
    panel1.Controls.Add(this.comboBox[store_…
    panel1.Controls.Add(this.textBox[store_n…
    
    store_num += 1;
    box_location += 20;
    
    }
    
    private void button3_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
    Product prod = new Product();
    
    
    prod.Product_name = this.ProductBox.SelectedText ;
    prod.Product_category = this.CategoryBox.SelectedText;
    //prod.Temp_store[store_num] = storeNameArray[store_num];
    //prod.Temp_price[store_num] = priceArray[store_num];
    
    
    }
    
    private void Form2_Load(object sender, EventArgs e)
    {
    
    }
    
    
    
    }
    }
    I allow the user to add new ComboBoxes everytime they click add item. Now if there are two comboBoxes, when they select an item from the second one, it changes the first one as well. How do I stop that from happening, so each comboBox has there respective selections.
    Last edited by HanneSThEGreaT; February 17th, 2010 at 03:51 AM.

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Add ComboBox to Form. Selection not working

    Pelase use [CODE] [/CODE] Tags when posting code

  3. #3
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Add ComboBox to Form. Selection not working

    I think that is happening because you are using the same datasource for all the combo boxes. As a quick fix try cloning the 'storeNameArray' when you assign it to the data source of the combo box. Alternatively create a new array each time and you the Array.CopyTo() method to populate the new array from the 'storeNameArray'. If you study the data-binding options you might find a better solution. I'm mainly doing ASP.NET so I'm a bit rusty on data-binding in Windows forms.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured