Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. two comboBoxs

two comboBoxs

Scheduled Pinned Locked Moved C#
csharphelp
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    picasso2
    wrote on last edited by
    #1

    I have a C# and Silverlight project with two combo boxes (CB1, CB2)
    -CB1 is initialized and populated with few items.
    (e.g. car, plane, motorcycle)
    -User selects an item (e.g. car) and CB2 gets automatically populated
    (e.g. model, year, color) via the cb1_SelectionChanged()
    -user selects the item from CB2 (e.g. model) and a datagid is populated accordingly.

    Problem:
    Once I selected item from CB2 and datagrid is populated correctly, attempting to select a different item from CB1 generates a “Object reference not set to an instance of an object”
    To go around just select a different item then back again to the item and that woks but is really annoying. :)
    Any ideas will be greatly appreciated

    S 1 Reply Last reply
    0
    • P picasso2

      I have a C# and Silverlight project with two combo boxes (CB1, CB2)
      -CB1 is initialized and populated with few items.
      (e.g. car, plane, motorcycle)
      -User selects an item (e.g. car) and CB2 gets automatically populated
      (e.g. model, year, color) via the cb1_SelectionChanged()
      -user selects the item from CB2 (e.g. model) and a datagid is populated accordingly.

      Problem:
      Once I selected item from CB2 and datagrid is populated correctly, attempting to select a different item from CB1 generates a “Object reference not set to an instance of an object”
      To go around just select a different item then back again to the item and that woks but is really annoying. :)
      Any ideas will be greatly appreciated

      S Offline
      S Offline
      Snehasish_Nandy
      wrote on last edited by
      #2

      Post the piece of code for better understanding...

      P 1 Reply Last reply
      0
      • S Snehasish_Nandy

        Post the piece of code for better understanding...

        P Offline
        P Offline
        picasso2
        wrote on last edited by
        #3

        Yes, I should have done fist. Thank you public Home() { InitializeComponent(); //initilize the combobox comboBox1.Items.Add("CAR"); comboBox1.Items.Add("PLANE"); comboBox1.Items.Add("MOTORCYCLE"); for CB1 private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { string selec = comboBox1.SelectedItem.ToString(); switch (selec) { case "CAR": { comboBox2.Items.Clear(); label3.Content = "Select cars"; comboBox2.Items.Add("Model"); comboBox2.Items.Add("Color"); } break; case "PLANE": { comboBox2.Items.Clear(); label3.Content = "Select Site"; comboBox2.Items.Add("model"); comboBox2.Items.Add("EngineType"); comboBox2.Items.Add("choice 1"); comboBox2.Items.Add("choice 2"); } break; }//end of switch } FOR CB2 private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e) { EclipseMTXDomainContext context1 = new EclipseMTXDomainContext(); //load datagrid from database string selec = comboBox2.SelectedItem.ToString(); switch (selec) { case "model": { //GET ALL MOdels dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; case "EngineType": { //GET ALL Engine Types dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; }//end of switch }

        B 1 Reply Last reply
        0
        • P picasso2

          Yes, I should have done fist. Thank you public Home() { InitializeComponent(); //initilize the combobox comboBox1.Items.Add("CAR"); comboBox1.Items.Add("PLANE"); comboBox1.Items.Add("MOTORCYCLE"); for CB1 private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) { string selec = comboBox1.SelectedItem.ToString(); switch (selec) { case "CAR": { comboBox2.Items.Clear(); label3.Content = "Select cars"; comboBox2.Items.Add("Model"); comboBox2.Items.Add("Color"); } break; case "PLANE": { comboBox2.Items.Clear(); label3.Content = "Select Site"; comboBox2.Items.Add("model"); comboBox2.Items.Add("EngineType"); comboBox2.Items.Add("choice 1"); comboBox2.Items.Add("choice 2"); } break; }//end of switch } FOR CB2 private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e) { EclipseMTXDomainContext context1 = new EclipseMTXDomainContext(); //load datagrid from database string selec = comboBox2.SelectedItem.ToString(); switch (selec) { case "model": { //GET ALL MOdels dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; case "EngineType": { //GET ALL Engine Types dataGrid1.ItemsSource = context1.Protons; context1.Load(context1.GetProtonsFilteredQuery(1)); } break; }//end of switch }

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          picasso2 wrote:

          string selec = comboBox1.SelectedItem.ToString();

          Comboboxes use to send two SelectionChanged events when the user changes the selection: in the first event, the previously selected item is un-selected, at this moment no item is selected, comboBox1.SelectedItem is null, and consequently comboBox1.SelectedItem.ToString(); causes a NullReferenceException. Then the new item gets selected and SelectedItem has a value again. Solution: check SelectedItem for null.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups