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

    Dynamic ComboBox IndexChanged Event, WinForms, C#

    Hello All:

    I am making a dynamic form that I have linked to a SQL database. On my form I create a combobox dynamically. I would like to have an something like a selectedindexchanged event fire when an item in the combobox is selected. If possible, once this event fires I would like to create other dynamic controls based on the selection. I know how to do this on a web form, but not in win forms. I also know that there is an easier way around all the dynamic controls that I am making but I am experimenting with the dynamic controls. Any suggestions would be greatly appreciated. Here is my code for the form:

    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 System.Runtime.InteropServices;
    using System.Web;

    namespace Real_Estate_Manager
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
    btnUpdateProps.Visible = true;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    // TODO: This line of code loads data into the 'propertyManagerDataSet.Property' table. You can move, or remove it, as needed.
    this.propertyTableAdapter.Fill(this.propertyManagerDataSet.Property);

    }

    public void btnUpdateProps_Click(object sender, EventArgs e)
    {
    Label lblStreet = new Label();
    lblStreet.Text = "Street:";
    lblStreet.Location = new Point(20, 155);
    lblStreet.Width = 40;
    this.Controls.Add(lblStreet);

    TextBox txtStreet = new TextBox();
    txtStreet.Location = new Point(60, 150);
    txtStreet.Width = 150;
    this.Controls.Add(txtStreet);

    Label lblCity = new Label();
    lblCity.Text = "City:";
    lblCity.Location = new Point(20, 185);
    lblCity.Width = 40;
    this.Controls.Add(lblCity);

    TextBox txtCity = new TextBox();
    txtCity.Location = new Point(60, 180);
    txtCity.Width = 150;
    this.Controls.Add(txtCity);

    Label lblState = new Label();
    lblState.Text = "State:";
    lblState.Location = new Point(20, 215);
    lblState.Width = 40;
    this.Controls.Add(lblState);

    String[] strStates = new string[] {"AL","AK","AS","AZ","AR","CA","CO","CT",
    "DE","DC","FM","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA",
    "ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM",
    "NY","NC","ND","MP","OH","OK","OR","PW","PA","PR","RI","SC","SD","TN",
    "TX","UT","VT","VI","VA","WA","WV","WI","WY"};

    ComboBox ddlState = new ComboBox();
    ddlState.Location = new Point(60, 210);
    ddlState.Width = 150;
    ddlState.Text = "Please Select a State";
    // ddlState.Items.Insert(0, "Please Select a State");
    ddlState.Items.AddRange(strStates);
    this.Controls.Add(ddlState);

    Label lblZip = new Label();
    lblZip.Text = "Zip Code:";
    lblZip.Location = new Point(5, 245);
    lblZip.Width = 60;
    this.Controls.Add(lblZip);

    TextBox txtZip = new TextBox();
    txtZip.Location = new Point(60, 240);
    txtZip.Width = 150;
    this.Controls.Add(txtZip);

    Label lblType = new Label();
    lblType.Location = new Point(20, 275);
    lblType.Text = "Type";
    lblType.Width = 40;
    this.Controls.Add(lblType);

    string[] strTypes = new string[] {"Military", "Educational", "Government",
    "Industrial", "Commercial", "Residential"};

    ComboBox ddlType = new ComboBox();
    ddlType.Location = new Point(60, 270);
    ddlType.Width = 150;
    ddlType.Text = "Please Select the Type";
    ddlType.Items.AddRange(strTypes);
    this.Controls.Add(ddlType);
    ddlType.SelectedIndexChanged = ddlType_SelectedIndexChanged();


    }

    public void ddlType_SelectedIndexChanged()
    {
    if (ddlType.SelectedIndexChanged = 0)

    }
    }
    }



    Here is my code that I am trying to do the event on:

    CODE

    string[] strTypes = new string[] {"Military", "Educational", "Government",
    "Industrial", "Commercial", "Residential"};

    ComboBox ddlType = new ComboBox();
    ddlType.Location = new Point(60, 270);
    ddlType.Width = 150;
    ddlType.Text = "Please Select the Type";
    ddlType.Items.AddRange(strTypes);
    this.Controls.Add(ddlType);
    ddlType.SelectedIndexChanged = ddlType_SelectedIndexChanged();


    }

    public void ddlType_SelectedIndexChanged()
    {
    if (ddlType.SelectedIndexChanged = 0)

    }


    Any suggestions would be greatly appreciated. I have been struggling with this for a couple days.

    Thanks,
    Roaster

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

    Re: Dynamic ComboBox IndexChanged Event, WinForms, C#

    Hi there. An interesting topic. Please edit your original post and use the [code] tags to format your code. You open the tag at the beginning of the code and you close it at the end. This is a general rule on the forum. It would make your code alot easier to read without having to copy it to a C# editor. Thanks.

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Dynamic ComboBox IndexChanged Event, WinForms, C#

    you were close. This is how you would do it...

    Code:
    string[] strTypes = new string[] {"Military", "Educational", "Government",
    "Industrial", "Commercial", "Residential"};
    
    ComboBox ddlType = new ComboBox();
    ddlType.Location = new Point(60, 270);
    ddlType.Width = 150;
    ddlType.Text = "Please Select the Type";
    ddlType.Items.AddRange(strTypes);
    ddlType.SelectedIndexChanged += new EventHandler(ddlType_SelectedIndexChanged);
    this.Controls.Add(ddlType);
    and your event would look like this...

    Code:
    public void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
          if (ddlType.SelectedIndexChanged == 0)
          {
                  // do whatever when the user selects the first item in the combobox.
          }
    }
    ===============================
    My Blog

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