?
Solved

C# DropDown smart logic implementation

Posted on 2014-10-20
8
Medium Priority
?
251 Views
Last Modified: 2014-11-27
Hi Experts,

I've got a ComboBox in ComboBoxStyle.DropDownList style.

Dropdown selection
This gets dunamically created based on the number of a passed parameter. The letters is an ArrayList of letters which are added to the dropdown

for (int a = i; a < drivesLetters.Count; a++)
       {
          combobox.Items.Add(drivesLetters[a]);
       }

Open in new window


I've been trying to create a method so that when I click on a dropdown list, the letters which are already selected should not show up from the selections. Like on the image above, If I click on M, then M should not show up. If I select V, then V should be selected, not show on the list, M should be added.

I'm not sure how to actually do this. I wonder if I should have two ArrayLists (one holds the letters used in the dropdown, and another one to hold all the unused letters).

I am looking at an exact implementation like this (in javascript) but obviously in C#. I've tried to convert the javascript method to C#, but I was not successful.

Please kindly point me to a good starting point. I've tried so many methods, but no success..

Thanks!
0
Comment
Question by:John Solomon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 40395225
You can use the drawItem event.  (http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.drawitem(v=vs.110).aspx)

Something like:

  private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
        {
            if (e.Index == ComboBox1.SelectedIndex)
                e.Graphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(2,e.Bounds.Top+2, e.Bounds.Width, 0));
        }

Open in new window

0
 
LVL 1

Author Comment

by:John Solomon
ID: 40395368
Hi Kyle Abrahams, thank you for your answer. However, I am not sure how this method would achieve my goal.
If index is equal to combobox selectedIndex, then redraw a rectangle combobox?.

I might have confused you with my question, and I see that the javascript link was not working.. I was hoping to get something like this combo box selection implementation.

Where a number of comboboxes {combo1, combo2. combo3} are filled with a list of letters {A,B,C,D,E, F, G, H}. In order to prevent a user from choosing the same letter A in combo1 and combo3, if A is already selected in combo1, then it should not show up in combo3 dropdown. Given than letter A is unselected in combo1 and is not selected in any other comboboxes, then letter A should be available in a comboboxes dropdown as selectable.

Thank you very much for your help :)
0
 
LVL 1

Author Comment

by:John Solomon
ID: 40395387
Hi Kyle, or an implementation like this. Thanks
0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 1

Author Comment

by:John Solomon
ID: 40395409
Or this is a closer implementation that I am looking for javascripcode

I'll try to learn and convert this code into C#. But your help will be greatly appreciated. :) thanks!
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 40395428
Hi John,

Okay, that makes a lot more sense.

What I was proposing was let's say you chose "M" . . . you would not want to see "M" in the same combo box in the values again.  That's not usually the default but I don't question people's intentions.

This should work:

PopulateCombos(ComboBox MychangedCombo)
{
    string drive1 = combo1.Text;
    string drive2 = combo2.Text;
    string drive3 = combo3.Text;
  


// populate 1 - do not populate if that's the one that was changed)
   if (MychangedCombo != combo1)
  {
     combobox1.items.Clear();
     for (int a = 0; a < drivesLetters.Count; a++)
       {
               if (drivesLetters[a] == drive2 || drivesLetters[a]  == drive3)
                    continue;
        
                  combobox1.Items.Add(drivesLetters[a]);
                   comboBox1.Text = drive1;
       }
  
//repeat for 2 && 3  
 }
}

You can then wire all 3 combo_selectedIndexchanged to:

DrivesChanged(object sender, eventargs e)
{
   PopulateCombos((ComboBox) sender);
}

Open in new window

0
 
LVL 35

Expert Comment

by:ste5an
ID: 40395788
Without having a VS currently at hands, my approach would be using the MeasureItem event to set the height of the item to 0 and skip it in your own owner draw event.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question