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. Combo Box question [modified]

Combo Box question [modified]

Scheduled Pinned Locked Moved C#
winformsdata-structureshelptutorialquestion
10 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    Muntyness
    wrote on last edited by
    #1

    Alrighty then, my new question is this: I have a combo box in each user control, and I have several user controls. Now I want each combo box to start off with the same options availible to them, (e.g. Mike, 1, 2, 3, 4, 5. I know how to do this part.) but when an option other than Mike is chosen in a combo box, then none of the other combo boxes can choose that option. So if ComboBox1 has selected 1, then ComboBox2 will only be able to select Mike, 2, 3, 4, 5. But if ComboBox1 has Mike selected, ComboBox2 will still be able to select Mike, 1, 2, 3, 4, 5. Anyway, I think I can put something together using handlers and arrays to do this, however I was hoping that someone knew a better way (or better yet knew if there was an article about this). I hope I made sense. Any help will be appreciated. Edit: Here's how I would go about the code for this function. In the Master class (The one that's creating the User controls) there is an array (Array1) with the combo box values. There is a second array (Array2) with the values that haven't been used. Array2 is passed into the User controls on creation, allowing them to populate their combo boxes. In the User control there is also public method which updates the contents of the Combo box. Now in order to get the combo boxes to update on all of the other User controls, a handler is put in the User control that listens for when the combo box is changed. This handler passes the contents of the combo box from both before(Value1) and after(Value2) the change (probably useing variables in the User class) to the Master class (which has a handle listening for the User control handler). The Master class then removes Value2 from Array2. Then the Master class adds Value1 to Array2. Once this is done the Master passes Array2 into the public methods of all user controls, which forces them to update their Combo Box contents. Note that when placing Value1 into the array, a while loop would be used in order to have it placed in the appropriate position. I really hope that explanation made sense. - Munty -- modified at 5:47 Monday 11th December, 2006

    M 1 Reply Last reply
    0
    • M Muntyness

      Alrighty then, my new question is this: I have a combo box in each user control, and I have several user controls. Now I want each combo box to start off with the same options availible to them, (e.g. Mike, 1, 2, 3, 4, 5. I know how to do this part.) but when an option other than Mike is chosen in a combo box, then none of the other combo boxes can choose that option. So if ComboBox1 has selected 1, then ComboBox2 will only be able to select Mike, 2, 3, 4, 5. But if ComboBox1 has Mike selected, ComboBox2 will still be able to select Mike, 1, 2, 3, 4, 5. Anyway, I think I can put something together using handlers and arrays to do this, however I was hoping that someone knew a better way (or better yet knew if there was an article about this). I hope I made sense. Any help will be appreciated. Edit: Here's how I would go about the code for this function. In the Master class (The one that's creating the User controls) there is an array (Array1) with the combo box values. There is a second array (Array2) with the values that haven't been used. Array2 is passed into the User controls on creation, allowing them to populate their combo boxes. In the User control there is also public method which updates the contents of the Combo box. Now in order to get the combo boxes to update on all of the other User controls, a handler is put in the User control that listens for when the combo box is changed. This handler passes the contents of the combo box from both before(Value1) and after(Value2) the change (probably useing variables in the User class) to the Master class (which has a handle listening for the User control handler). The Master class then removes Value2 from Array2. Then the Master class adds Value1 to Array2. Once this is done the Master passes Array2 into the public methods of all user controls, which forces them to update their Combo Box contents. Note that when placing Value1 into the array, a while loop would be used in order to have it placed in the appropriate position. I really hope that explanation made sense. - Munty -- modified at 5:47 Monday 11th December, 2006

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, Thats what I would try to do: I would implement an "ItemManager" which holds the Array of available Items. This Manager has an event ItemsChanged which is raised if the Array is changed. The UserControls then get the Instance (Only one instance for all Controls) of the Manager. They also link to the event. If then a item is selected in a combobox it has to be removed from the Array of the manager and the olditem has to be added again. If the ItemsChanged Event of the Manager is raised, the UserControls have too rearange their available items. Hope that helps! All the best, Martin

      M 1 Reply Last reply
      0
      • M Martin 0

        Hello, Thats what I would try to do: I would implement an "ItemManager" which holds the Array of available Items. This Manager has an event ItemsChanged which is raised if the Array is changed. The UserControls then get the Instance (Only one instance for all Controls) of the Manager. They also link to the event. If then a item is selected in a combobox it has to be removed from the Array of the manager and the olditem has to be added again. If the ItemsChanged Event of the Manager is raised, the UserControls have too rearange their available items. Hope that helps! All the best, Martin

        M Offline
        M Offline
        Muntyness
        wrote on last edited by
        #3

        Just one question. I was assuming that ItemManager was a built in class, but as I can't find it, I'm now assuming that you meant I should make an Item manager myself. Other than that, I'm planning on trying that out sometime today. - Munty

        M 1 Reply Last reply
        0
        • M Muntyness

          Just one question. I was assuming that ItemManager was a built in class, but as I can't find it, I'm now assuming that you meant I should make an Item manager myself. Other than that, I'm planning on trying that out sometime today. - Munty

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          Muntyness wrote:

          I'm now assuming that you meant I should make an Item manager myself.

          Yes that's correct. I was thinking of something like that:

          public class ItemManager
          {
          private static ItemManager _instance;
          private Array _availableitems = new Array();

          public ItemManager()
          {
          }
          
          public Array AvailableItems
          {
              get
              {
                  return \_availableitems;
              }
              set
              {
                  if(value!=\_availableitems)
                  {
                      \_availableitems = value;
                      //TODO: event for the Information of the UserControls
                  }
              }
          }
          
          
          public static ItemManager GetInstance()
          {
              if(\_instance==null)
              {
                  \_instance = new ItemManager();
              }
              return \_instance;
          }
          

          }

          Hope that helps! All the best, Martin

          M 2 Replies Last reply
          0
          • M Martin 0

            Muntyness wrote:

            I'm now assuming that you meant I should make an Item manager myself.

            Yes that's correct. I was thinking of something like that:

            public class ItemManager
            {
            private static ItemManager _instance;
            private Array _availableitems = new Array();

            public ItemManager()
            {
            }
            
            public Array AvailableItems
            {
                get
                {
                    return \_availableitems;
                }
                set
                {
                    if(value!=\_availableitems)
                    {
                        \_availableitems = value;
                        //TODO: event for the Information of the UserControls
                    }
                }
            }
            
            
            public static ItemManager GetInstance()
            {
                if(\_instance==null)
                {
                    \_instance = new ItemManager();
                }
                return \_instance;
            }
            

            }

            Hope that helps! All the best, Martin

            M Offline
            M Offline
            Muntyness
            wrote on last edited by
            #5

            Aye, that helps alot. Thanks! - Munty

            1 Reply Last reply
            0
            • M Martin 0

              Muntyness wrote:

              I'm now assuming that you meant I should make an Item manager myself.

              Yes that's correct. I was thinking of something like that:

              public class ItemManager
              {
              private static ItemManager _instance;
              private Array _availableitems = new Array();

              public ItemManager()
              {
              }
              
              public Array AvailableItems
              {
                  get
                  {
                      return \_availableitems;
                  }
                  set
                  {
                      if(value!=\_availableitems)
                      {
                          \_availableitems = value;
                          //TODO: event for the Information of the UserControls
                      }
                  }
              }
              
              
              public static ItemManager GetInstance()
              {
                  if(\_instance==null)
                  {
                      \_instance = new ItemManager();
                  }
                  return \_instance;
              }
              

              }

              Hope that helps! All the best, Martin

              M Offline
              M Offline
              Muntyness
              wrote on last edited by
              #6

              Alrighty, Im here to bug you again. So I took your framework and butchered it. (What can I say? I'm stupid :sigh:)

              public class fnt_ItemManager
              {
              public event EventHandler ArrayChanged;
              private Array _availableitems;

                  public fnt\_ItemManager()
                  {
                  }
              
                  public Array AvailableItems
                  {
                      get
                      {
                          return \_availableitems;
                      }
                      set
                      {
                          if (value != \_availableitems)
                          {
                              \_availableitems = value;
                          }
                      }
                  }
              
                  public void update (String oldVal, String newVal)
                  {
                      ArrayList updating = new ArrayList();
                      int i = 1;
              
                      if (oldVal != null)
                      {
                          foreach (String st in \_availableitems)
                          {
                              if (newVal != st || newVal == "#NONE")
                              {
                                  updating.Add(st);
                              }
                          }
              
                          if (oldVal != "#NONE")
                          {
                              Boolean found = false;
                              while (i < updating.Count)
                              {
                                  if (int.Parse((String)updating\[i\]) > int.Parse(oldVal))
                                  {
                                      updating.Insert(i, oldVal);
                                      found = true;
                                      break;
                                  }
                                  i++;
                              }
                              if (oldVal != null && found == false)
                              {
                                  updating.Add(oldVal);
                              }
                          }
              
              
                          String\[\] rar = new String\[updating.Count\];
                          i = 0;
              
                          foreach (String st in updating)
                          {
                              rar\[i\] = st;
                              i++;
                          }
              
                          \_availableitems = rar;
              
                          if (ArrayChanged != null)
                          {
                              ArrayChanged(this, EventArgs.Empty);
                          }
                      }
                  }
              }
              

              }

              And in the control that calls it:

              private void cmb1_SelectedIndexChanged(object sender, EventArgs e)
              {
              OldValue = Current;
              Current = cmb1.Text;

              if (OldValue != null && OldValue != Current)
              {
                  ItemManager.update(OldValue, Current);
              }
              

              }

              void p_ItemManager_ArrayChanged(object sender, EventArgs e)
              {

              M 1 Reply Last reply
              0
              • M Muntyness

                Alrighty, Im here to bug you again. So I took your framework and butchered it. (What can I say? I'm stupid :sigh:)

                public class fnt_ItemManager
                {
                public event EventHandler ArrayChanged;
                private Array _availableitems;

                    public fnt\_ItemManager()
                    {
                    }
                
                    public Array AvailableItems
                    {
                        get
                        {
                            return \_availableitems;
                        }
                        set
                        {
                            if (value != \_availableitems)
                            {
                                \_availableitems = value;
                            }
                        }
                    }
                
                    public void update (String oldVal, String newVal)
                    {
                        ArrayList updating = new ArrayList();
                        int i = 1;
                
                        if (oldVal != null)
                        {
                            foreach (String st in \_availableitems)
                            {
                                if (newVal != st || newVal == "#NONE")
                                {
                                    updating.Add(st);
                                }
                            }
                
                            if (oldVal != "#NONE")
                            {
                                Boolean found = false;
                                while (i < updating.Count)
                                {
                                    if (int.Parse((String)updating\[i\]) > int.Parse(oldVal))
                                    {
                                        updating.Insert(i, oldVal);
                                        found = true;
                                        break;
                                    }
                                    i++;
                                }
                                if (oldVal != null && found == false)
                                {
                                    updating.Add(oldVal);
                                }
                            }
                
                
                            String\[\] rar = new String\[updating.Count\];
                            i = 0;
                
                            foreach (String st in updating)
                            {
                                rar\[i\] = st;
                                i++;
                            }
                
                            \_availableitems = rar;
                
                            if (ArrayChanged != null)
                            {
                                ArrayChanged(this, EventArgs.Empty);
                            }
                        }
                    }
                }
                

                }

                And in the control that calls it:

                private void cmb1_SelectedIndexChanged(object sender, EventArgs e)
                {
                OldValue = Current;
                Current = cmb1.Text;

                if (OldValue != null && OldValue != Current)
                {
                    ItemManager.update(OldValue, Current);
                }
                

                }

                void p_ItemManager_ArrayChanged(object sender, EventArgs e)
                {

                M Offline
                M Offline
                Martin 0
                wrote on last edited by
                #7

                Hello Munty,

                Muntyness wrote:

                should I just scrap it all and take up farming now?

                I think this would be the best solution for everyone!:laugh: My suggestions are: UserControl: Maybe the SelectedIndexChanged Event is raised to often because of the "Items.Clear" and "Items.Add". So a boolen flag in the SelectedIndexChanged Method could help. private bool updating; private void cmbl_SelectedIndexChanged(object sender, EventArgs e) { if(updating==false) { OldValue = Current; Current = cmb1.Text; . . . } } private void p_ItemManager_ArrayChanged(object sender, EventArgs e) { updating = true; . . . updating = false; } ItemManager: Have you used the GetInstance Methode? The update Method looks much too complicated for working correct. Why cant't you just add and remove directly from the Array? See you! Martin

                M 1 Reply Last reply
                0
                • M Martin 0

                  Hello Munty,

                  Muntyness wrote:

                  should I just scrap it all and take up farming now?

                  I think this would be the best solution for everyone!:laugh: My suggestions are: UserControl: Maybe the SelectedIndexChanged Event is raised to often because of the "Items.Clear" and "Items.Add". So a boolen flag in the SelectedIndexChanged Method could help. private bool updating; private void cmbl_SelectedIndexChanged(object sender, EventArgs e) { if(updating==false) { OldValue = Current; Current = cmb1.Text; . . . } } private void p_ItemManager_ArrayChanged(object sender, EventArgs e) { updating = true; . . . updating = false; } ItemManager: Have you used the GetInstance Methode? The update Method looks much too complicated for working correct. Why cant't you just add and remove directly from the Array? See you! Martin

                  M Offline
                  M Offline
                  Muntyness
                  wrote on last edited by
                  #8

                  Martin# wrote:

                  ItemManager: Have you used the GetInstance Methode?

                  Admittedly no. To be honest I wasn't sure what that method was for... :confused:

                  Martin# wrote:

                  Why cant't you just add and remove directly from the Array?

                  I can directly remove from the array (it now does that :-O), it's just that when I add to the array, the items need to be ordered. So I can either put the value into the correct place to start with, or I can sort the array after the item has been added. I'm fairly sure that sorting the array after every change would probably be worse, but if it's better to sort, feel free to tell me. the code now looks more like this: (There are 2 combo boxes now, but the methods are identical except for some names)

                  private void cmb1_SelectedIndexChanged(object sender, EventArgs e)
                  {
                  OldValue1 = Current1;
                  Current1 = cmb1.Text;

                          if (OldValue1 != null && OldValue1 != Current1)
                          {
                              selected = 1;
                              ItemManager.update(OldValue1, cmb1.SelectedIndex);
                          }
                      }
                  
                      void ItemManager\_ArrayChanged(object sender, EventArgs e)
                      {
                          if (selected != 1)
                              cmb1.Items.Clear();
                          if (selected != 2)
                              cmb2.Items.Clear();
                  
                          foreach (String st in p\_ItemManager.AvailableItems)
                          {
                              if (selected != 1)
                                  cmb1.Items.Add(st);
                              if (selected != 2)
                                  cmb2.Items.Add(st);
                          }
                  
                          selected = 0;
                      }
                  

                  int the item manager:

                  public class fnt_ItemManager
                  {
                  public event EventHandler ArrayChanged;
                  private ArrayList _availableitems = new ArrayList();
                  private String Default;

                  public fnt\_ItemManager(String defaultcont)
                  {
                      Default = defaultcont;
                  }
                  
                  public ArrayList AvailableItems
                  {
                      get
                      {
                          return \_availableitems;
                      }
                      set
                      {
                          if (value != \_availableitems)
                          {
                              \_availableitems = value;
                          }
                      }
                  }
                  
                  public void update(String oldVal, int index)
                  {
                      Boolean found = false;
                      int i = 0;
                  
                      if (index != 0)
                      {
                          AvailableItems.RemoveAt(index);
                      }
                  
                      if (oldVal != Default)
                      {
                  
                  M 1 Reply Last reply
                  0
                  • M Muntyness

                    Martin# wrote:

                    ItemManager: Have you used the GetInstance Methode?

                    Admittedly no. To be honest I wasn't sure what that method was for... :confused:

                    Martin# wrote:

                    Why cant't you just add and remove directly from the Array?

                    I can directly remove from the array (it now does that :-O), it's just that when I add to the array, the items need to be ordered. So I can either put the value into the correct place to start with, or I can sort the array after the item has been added. I'm fairly sure that sorting the array after every change would probably be worse, but if it's better to sort, feel free to tell me. the code now looks more like this: (There are 2 combo boxes now, but the methods are identical except for some names)

                    private void cmb1_SelectedIndexChanged(object sender, EventArgs e)
                    {
                    OldValue1 = Current1;
                    Current1 = cmb1.Text;

                            if (OldValue1 != null && OldValue1 != Current1)
                            {
                                selected = 1;
                                ItemManager.update(OldValue1, cmb1.SelectedIndex);
                            }
                        }
                    
                        void ItemManager\_ArrayChanged(object sender, EventArgs e)
                        {
                            if (selected != 1)
                                cmb1.Items.Clear();
                            if (selected != 2)
                                cmb2.Items.Clear();
                    
                            foreach (String st in p\_ItemManager.AvailableItems)
                            {
                                if (selected != 1)
                                    cmb1.Items.Add(st);
                                if (selected != 2)
                                    cmb2.Items.Add(st);
                            }
                    
                            selected = 0;
                        }
                    

                    int the item manager:

                    public class fnt_ItemManager
                    {
                    public event EventHandler ArrayChanged;
                    private ArrayList _availableitems = new ArrayList();
                    private String Default;

                    public fnt\_ItemManager(String defaultcont)
                    {
                        Default = defaultcont;
                    }
                    
                    public ArrayList AvailableItems
                    {
                        get
                        {
                            return \_availableitems;
                        }
                        set
                        {
                            if (value != \_availableitems)
                            {
                                \_availableitems = value;
                            }
                        }
                    }
                    
                    public void update(String oldVal, int index)
                    {
                        Boolean found = false;
                        int i = 0;
                    
                        if (index != 0)
                        {
                            AvailableItems.RemoveAt(index);
                        }
                    
                        if (oldVal != Default)
                        {
                    
                    M Offline
                    M Offline
                    Martin 0
                    wrote on last edited by
                    #9

                    Hello Munty,

                    Muntyness wrote:

                    Admittedly no. To be honest I wasn't sure what that method was for.

                    This Method generates only one instance of the fnt_ItemManager. It's also static, so you do not need an instance of the class to call it. Cause if you make a instance like this:

                    fnt_ItemManager p_ItemManager = new fnt_ItemManager();

                    Every Control has its one instance of the Manager and has no idea which item is used by an other Control. So please implement the method like i did and call it from the Controls, like that:

                    fnt_ItemManager p_ItemManager = yourenamespace.fnt_ItemManager.GetInstance();

                    If no't all controlls are linked (sharing one set of items), please let me know. We would have to make the GetInstance method more dynamic to create and return more than one instance. Hope that helps! All the best, Martin

                    M 1 Reply Last reply
                    0
                    • M Martin 0

                      Hello Munty,

                      Muntyness wrote:

                      Admittedly no. To be honest I wasn't sure what that method was for.

                      This Method generates only one instance of the fnt_ItemManager. It's also static, so you do not need an instance of the class to call it. Cause if you make a instance like this:

                      fnt_ItemManager p_ItemManager = new fnt_ItemManager();

                      Every Control has its one instance of the Manager and has no idea which item is used by an other Control. So please implement the method like i did and call it from the Controls, like that:

                      fnt_ItemManager p_ItemManager = yourenamespace.fnt_ItemManager.GetInstance();

                      If no't all controlls are linked (sharing one set of items), please let me know. We would have to make the GetInstance method more dynamic to create and return more than one instance. Hope that helps! All the best, Martin

                      M Offline
                      M Offline
                      Muntyness
                      wrote on last edited by
                      #10

                      So that's what it does! :omg: Hmm... I might try that when I next have some free time with the code (Which wont be till tommorow at the earliest) At the moment I've just created the instance in the GUI that the user controls are added too. And then whenever the user adds another control, the ItemManager instance is passed into the new user control. So it looks something like: (In the create control method)

                      UserControl something = new UserControl(ItemManager);

                      And in the item manager, something like: (In the user controls class)

                      private theItemManager itemManager;

                      public UserControl (theItemManager item)
                      {
                      itemManager = item;
                      }

                      (Incase you're wondering, I don't have the code infront of me right now) And don't worry, all the controls are using one set of items. Thanks for all the help you're giving me by the way. :) - Munty

                      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