Hi
Suppose I have three DropDownLists each with the items: A, B,C,D and E. If I selected item D from the DropDownList 2, then DropDownList1 and 3 will show the items A, B, C and E only, since my code works to remove / disable the selected item from the other DropDownLists. Any idea? Please help.
Thanks
VulpesPosted Aug 12, 2013, 4:05 PM
Shrinath GuravPosted Mar 31, 2016, 6:15 AM
Rano AHPosted Aug 26, 2013, 1:23 AM
Hi Vulpes.
This is what finally I got, but still I cannot proceed :-(
public partial class Form1 : Form
{
string[] mySelectedItems = new string[4];// this will store the selected items from all the comboBoxs.. Here I have the order of the selected items, since, in each compboBox the final statement will be:
mySelectedItems = exclusions;
In myFunction which reads the lines from a text file:
var list = new List();
using (var reader = new StreamReader(file))
{
for (int i = 1; i <= 10; i++)
{
list.Add(reader.ReadLine());
}
}
string[] MatchesValues = new string[4];
if((mySelectedItems[0]=="Oman" ) || (mySelectedItems[1]=="Oman" ) || (mySelectedItems[2]=="Oman") || (mySelectedItems[3]=="Oman"))
{
MatchesValues[0] = list[2]; // this will set the value of list[2] to store it int matchesValues[0]
}
if ((mySelectedItems[0] == "USA") || (mySelectedItems[1] == "USA") || (mySelectedItems[2] == "USA") || (mySelectedItems[3] == "USA"))
{
MatchesValues[1] = list[5];// this will set the value of list[5] to store it int matchesValues[1]
}
if ((mySelectedItems[0] == "Saudi Arabia") || (mySelectedItems[1] == "Saudi Arabia") || (mySelectedItems[2] == "Saudi Arabia") ||
(mySelectedItems[3] == "Saudi Arabia"))
{
MatchesValues[2] = list[8];// this will set the value of list[8] to store it int matchesValues[2]
}
if ((mySelectedItems[0] == "uAE") || (mySelectedItems[1] == "UAE") || (mySelectedItems[2] == "UAE") || (mySelectedItems[3] == "UAE"))
{
MatchesValues[3] = list[9];// this will set the value of list[9] to store it int matchesValues[3]
}
But I stop here each time :-(
I have the order of selected items and I set the value of the correspondence lines in my MatchesValues list. Can you help me on this, plz?
Rano AHPosted Aug 25, 2013, 8:11 AM
How to get the order of selected items from different comboBoxes I couldn't do that :-(
Below I manged to assign the value of lines to each item in comboBox and save them into MatchesValuesList
var list = new List
using (var reader = new StreamReader(file))
{
for (int i = 1; i <= 10; i++)
{
list.Add(reader.ReadLine());
}
}
string[] MatchesValues = new string[4];
if((mySelectedItems[0]=="Oman" ) || (mySelectedItems[1]=="Oman" ) || (mySelectedItems[2]=="Oman") || (mySelectedItems[3]=="Oman"))
{
MatchesValues[0] = list[2];
}
if ((mySelectedItems[0] == "USA") || (mySelectedItems[1] == "USA") || (mySelectedItems[2] == "USA") || (mySelectedItems[3] == "USA"))
{
MatchesValues[1] = list[5];
}
if ((mySelectedItems[0] == "Saudi Arabia") || (mySelectedItems[1] == "Saudi Arabia") || (mySelectedItems[2] == "Saudi Arabia") || (mySelectedItems[3] == "Saudi Arabia"))
{
MatchesValues[2] = list[8];
}
if ((mySelectedItems[0] == "uAE") || (mySelectedItems[1] == "UAE") || (mySelectedItems[2] == "UAE") || (mySelectedItems[3] == "UAE"))
{
MatchesValues[3] = list[9];
}
But, how to get the order of the selected items from each comboBox so that I can give a name to my file? and maybe I don't want to select all comboboxes to give a name. Maybe I just need to select only one item to give a name or maybe two to give a name. etc..
Rano AHPosted Aug 25, 2013, 1:00 AM
Yes, It is important to give the sequence of the file name.
If Oman is selected fist from ComboBox1 and UAE for example selected from ComboBox2 and USA selected from comboBox3 then the name should be the values of line 2, line 4, and line 7 for example. in which lines I can read using stream reader.
So,
Some specific lines in my text file have a value for each item in the comboBox.
All comboboxes have the same items option, but whenever an item is selected from any combobox my code will assign the related value of the line that it reads from my file and use it to give the file name for example. I have the right to select how many values I need to add in my file name. If I select 2 items from any two comboboxes, then the file name should contain two values based on the items that I selected which are being assigned from the value of the line.
VulpesPosted Aug 24, 2013, 7:09 PM
Are you saying that the order of the selected countries is important. So you need to know in which combobox each one is selected?
Rano AHPosted Aug 24, 2013, 3:31 PM
Hi Vulpes
I'm trying to do the following and not sure if this is ok.
public partial class Form1 : Form
{
string[] countries = { "UAE", "Saudi Arabia", "Qatar", "Oman" , "USA", "Canada"};
string [] mySelectedItems = new string[4];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.AddRange(countries);
comboBox2.Items.AddRange(countries);
comboBox3.Items.AddRange(countries);
comboBox4.Items.AddRange(countries);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedItem = comboBox1.SelectedItem.ToString();
string[] exclusions = {selectedItem};
Fill(comboBox2, exclusions);
Fill(comboBox3, exclusions);
Fill(comboBox4, exclusions);
mySelectedItems = exclusions;
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1) comboBox2.SelectedIndex = -1;
if (comboBox2.SelectedIndex == -1) return;
string selectedItem1 = comboBox1.SelectedItem.ToString();
string selectedItem2 = comboBox2.SelectedItem.ToString();
string[] exclusions = {selectedItem1, selectedItem2};
Fill(comboBox3, exclusions);
Fill(comboBox4, exclusions);
mySelectedItems = exclusions;
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1) comboBox3.SelectedIndex = -1;
if (comboBox3.SelectedIndex == -1) return;
string selectedItem1 = comboBox1.SelectedItem.ToString();
string selectedItem2 = comboBox2.SelectedItem.ToString();
string selectedItem3 = comboBox3.SelectedItem.ToString();
string[] exclusions = { selectedItem1, selectedItem2, selectedItem3};
Fill(comboBox4, exclusions);
Fill(comboBox4, exclusions);
comboBox4.SelectedIndex = 0;
mySelectedItems = exclusions;
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1 || comboBox2.SelectedIndex == -1 || comboBox3.SelectedIndex == -1)
comboBox4.SelectedIndex = -1;
string selectedItem1 = comboBox1.SelectedItem.ToString();
string selectedItem2 = comboBox2.SelectedItem.ToString();
string selectedItem3 = comboBox3.SelectedItem.ToString();
string selectedItem4 = comboBox4.SelectedItem.ToString();
string[] exclusions = { selectedItem1, selectedItem2, selectedItem3,selectedItem4 };
mySelectedItems = exclusions;
}
private void Fill(ComboBox cb, string[] exclusions)
{
cb.Items.Clear();
foreach(string country in countries)
{
bool exclude = false;
foreach(string exclusion in exclusions)
{
if (country == exclusion)
{
exclude = true;
break;
}
}
if(!exclude) cb.Items.Add(country);
}
cb.SelectedIndex = -1;
}
private void NameFiles (string oldPath, string newPath, string[] Files, string [] mySelectedItems)
{
foreach (string file in files)();
{
try
{
var list = new List
using (var reader = new StreamReader(file))
{
for (int i = 1; i <= 10; i++)
{
list.Add(reader.ReadLine());
}
}
//set the condition here.
So instead of saying:
string newFileName = string.Concat(Path.GetFileNameWithoutExtension(file), "-", list[2], "-", list[4], "-", list[6], "-",list[8], "txt");
I would assign the rows to my selected items first then proceed.,
Example:
string[] MatchesValues = new string[4];
if((mySelectedItems[0]=="Oman" )||(mySelectedItems[1]=="Oman")||(mySelectedItems[2]=="Oman")||(mySelectedItems[3]=="Oman"))
{
MatchesValues[0] = list[2]; // then the value of line 2 will be assigned whenever Oman is selected.
}
if((mySelectedItems[0]=="UAE" )||(mySelectedItems[1]=="UAE")||(mySelectedItems[2]=="UAE")||(mySelectedItems[3]=="UAE"))
{
MatchesValues[1] = list[4];// value of line 4 will be assigned whenever UAE is selected
}
// and so on..
I just need to set the format of the name. I need to decide which value will be set before another as per my selected items. (The order is important)
VulpesPosted Aug 24, 2013, 1:42 PM
Rano AHPosted Aug 24, 2013, 9:17 AM
I'm Posting this question here since it's related to the comboBoxes question.
I'm trying to assign a value of the line number that I read from my file when I select a specific item from any comboBox that I have.
Example:
If I select Oman from any comboBox, then line #9 will be the related value for Oman in which I can give in my file name.
So, instead of saying:
//string newFileName = string.Concat(Path.GetFileNameWithoutExtension(file), "-", list[2],"-"line[6], "txt"); I need to go to the correspondence line that matches with the selected Item condition.
I think I need to use the exclusions list which contains the selected items and then write the conditions to check before passing the values of myLines.
Rano AHPosted Aug 21, 2013, 8:16 AM
Yes, I copied correctly and old comboBox can still show same item. But it doesn't mean that its being selected, and that's good :-). Also, the rest of comboBoxes won't have same item again,
Thank you Vulpes.
VulpesPosted Aug 21, 2013, 5:57 AM
When I run it, if I select Saudi Arabia from comboBox1 and Oman from comboBox2 and then go back and select Oman from comboBox1, the selected item in comboBox2 disappears and the drop down shows: UAE, Saudi Arabia and Qatar.
Rano AHPosted Aug 21, 2013, 2:05 AM
Yes, It works :-) but noticed the following:
If one item is selected from one combobox, then yes, it is being removed from other comboBoxes, however, if you need to go to the previous comboBox to change the selected item, then the other second comboBox will still show the old selected item. (more than comboBox will show same item)
for example
UAE UAE UAE UAE
Saudi Arabia Saudi Arabia Saudi Arabia Saudi Arabia
Qatar Qatar Qatar Qatar
Oman Oman Oman Oman
so I selected Saudi Arabia from ComboBox1 and Oman from ComboBox2, if I need to go again to select other item than Saudi Arabia (Oman for example) from comboBox1 then comboBox2 will still Show Oman and ComboBox1 also show Oman item.
I think it doesn't work for backwards options.
Rano AHPosted Aug 12, 2013, 2:38 PM
Thanks for the reply, the code works fine but still I'm not getting the expected result.
Maybe I didn't clearly set my requirements:
Suppose that I have 4 comboBoxes, each should populate same items at the beginning (before the first selection). Let's agree that all the comboBoxes have the following items by default.
UAE UAE UAE UAE
Saudi Arabia Saudi Arabia Saudi Arabia Saudi Arabia
Qatar Qatar Qatar Qatar
Oman Oman Oman Oman
If I selected Saudi Arabia from the first combobox, then ComboBox No2, No3, No4 should populate automatically only UAE, Qatar and Oman. For the next selection for comboBox 2 if I selected Oman, then the third comboBox will populate UAE and Qatar. So for each selection I make other comboBoxes will be updated automatically. Note that user can select from one or two or all comboBoxes.
VulpesPosted Aug 12, 2013, 1:38 PM
For Windows Forms the nearest equivalent is a ComboBox with its DropDownStyle set to ComboBoxStyle.DropDownList. The equivalent code for that is as follows:
Rano AHPosted Aug 12, 2013, 12:33 PM
I'm working on Windows application form and I'm not sure if the code that you implemented is for web applications.
It says that the name ("IsPostBack) does not exist in the current context, and same for DropDownList. The type or namespace "DropDownList couldn't be found.
am I missing a namespace? or the code works only for web applications? What I know that System.Web.UI.WebControls.DropDownList represents a control that allows the user to select a single item from a drop-down list.
VulpesPosted Aug 11, 2013, 4:57 PM
if (!IsPostback) {... }
ensures that the three DropDownLists are only loaded once when the page first loads and not on subsequent postbacks.
I don't understand why you can't implement the code as it's working fine on my machine.
What errors are you getting?
Rano AHPosted Aug 10, 2013, 3:09 PM
VulpesPosted Aug 10, 2013, 6:14 AM
Rano AHPosted Aug 10, 2013, 2:33 AM
Thank you so much for the solution. It worked, but I still have a concern:
If I selected an item from DropDownList1 Then it's true that the other dropDownLists wil show only the remaining items. However, if I selected another one item one more time from the same DropDownList1 then this will remove also that item in addition to the old selection from the other dropDownLists. As a result number of items is being decreased each time I change the selected item from the same DropDownList. and this will lead for a dropDownList with Zero items left. How to make my code displays the updated values for the DropDownLists based on the current selection of the items in all the dropDownLists.
Thanks again.
VulpesPosted Aug 9, 2013, 6:28 PM
Note that I've iterated backwards to remove items from the DropDownLists just in case in practice the item to be deleted appears more than once in any particular DropDownList. This avoids messing up the indexing of the remaining elements.
Also AutoPostBack should be set to True for the DropDownLists.