Loop thru the combobox and compare with the data in the database - c#

i had try several method to loop through the combobox, but it won't work...
for (Int16 i = 0; cbxrecipe.Items.Count - 2 >= i; i++)
{
if (cbxrecipe(i).Items.ToString() != Reader_recipe1.GetValue(0).ToString())`
{
//update the combobox;
}
}
means i need to loop through the combobox, to check whether the items inside the combobox is same with the data in the database, if same, it will not update, otherwise, the combobox will appear new item immediately...
thanks for the help =)

you have an error in your code.
try this :
for (Int16 i = 0; cbxrecipe.Items.Count - 2 >= i; i++)
{
if (cbxrecipe.Items[i].ToString() != Reader_recipe1.GetValue(0).ToString())
{
//update the combobox;
}
}

Related

Set Combobox selected item based on its ItemsValue

I have a Combobox, in which let's say that an item's Display Text is "School" and it's Item Value is 19. So i have stored this 19 into a DataGrid.
Then, i retrieve Combobox Value from DataGrid, then what i want to do is simply that based on value retrieved from DataGrid, combobox should set it's display Item or SelectedItem which have Value 19. In above scenario Combobox should display its selected item "School" if its value was 19.
So far i have wrote code upto this point. But it always giving me First Item of a Combobx.
DataGrid gd = (DataGrid)sender;
DataRowView rowSelected = gd.SelectedItem as DataRowView;
if(rowSelected!=null)
{
for (int i = 0; i < comboBox1.Items.Count;i++ )
{
if (Convert.ToString(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = comboBox1.Items.IndexOf(comboBox1.SelectedValue);
}
comboBox1.SelectedItem= comboBox1.Items[index];
}
textBox9.Text=rowSelected[14].ToString();
}
Now i am able to retrieve the Combobox Item, Based on its value which i am retrieving from the WPF DataGrid.
for (int i = 0;i <comboBox1.Items.Count; i++)
{
comboBox1.SelectedIndex = i;
if ((string)(comboBox1.SelectedValue) == Convert.ToString(rowSelected[14]))
{
index = i;
}
}
comboBox1.SelectedIndex = index;
Change your code to
if(rowSelected!=null)
{
int index = comboBox1.Items.IndexOf(rowSelected[14]);
comboBox1.SelectedItem = comboBox1.Items[index];
}
or
Use FindStringExact() method of combobox
int i = comboBox1.FindStringExact("Combo");
if(i >= 0)
{
}

Feeding a override value from OnSelectionChangeCommitted DataGridViewComboBoxEditingControl to column object

So I have this and I know it is wrong:
protected override void OnSelectionChangeCommitted(EventArgs e)
{
if (SelectedIndex == 0)
{
GENIO_Viewer.FullColourPaletteForm dlgColour = new GENIO_Viewer.FullColourPaletteForm();
if(dlgColour.ShowDialog() == DialogResult.OK)
{
bool bFound = false;
for(int i = 1; i < Items.Count; i++)
{
ComboboxColourItem ocbItem = (ComboboxColourItem)Items[i];
if(ocbItem.Index == dlgColour.iSelectedColour)
{
SelectedIndex = i;
bFound = true;
break;
// We can just select this one
}
}
if(!bFound)
{
// Add it
ComboboxColourItem ocbItem = ComboboxColourItem.Create((ushort)dlgColour.iSelectedColour);
Items.Add(ocbItem);
SelectedIndex = Items.Count - 1;
}
}
}
base.OnSelectionChangeCommitted(e);
}
This handler is part of my DataGridViewComboBoxEditingControl. But it is the wrong place to add new Items.
I can't workout how to get access to the owning Column as that is where I need to add the Item, otherwise I get exceptions.
I have looked here: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol(v=vs.110).aspx
But i can't see a property I can use to get the column object.
How do we do this from the editing control?
Further explaination:
The list combo items are added by the "column" object. Thus we have a set of 15 colours to choose from. Now I have added a genric colour tot he top of the list.
So, the user invokes the edit, droplist displays, and they pick item 0. We intercept this with the aforementioned handler. Since they picked item 0, we show a popup dialogue to let them choose a different colour.
When they have chosen, we must now either find it or add it to the the mandatory list of items for the column. Make sense now?
I tried to use the DataGridView Notify object but for some reason it is not showing in the list of available functions.
I don't use a DataSource. I populate like this in the columns constructor:
private void InitialiseComboItems()
{
List<ushort> listColours = new List<ushort>();
listColours.Add(0);
listColours.Add(1);
listColours.Add(2);
listColours.Add(3);
listColours.Add(4);
listColours.Add(5);
listColours.Add(6);
listColours.Add(7);
listColours.Add(8);
listColours.Add(9);
listColours.Add(250);
listColours.Add(251);
listColours.Add(252);
listColours.Add(253);
listColours.Add(254);
listColours.Add(255);
this.Items.Clear();
foreach (ushort iColourIndex in listColours)
this.Items.Add(ComboboxColourItem.Create(iColourIndex));
}
I also have a helper method:
public ComboboxColourItem InsertColour(ushort iColourIndex)
{
ComboboxColourItem ocbItem = ComboboxColourItem.Create(iColourIndex);
bool bAppend = true;
if (Items.Count > 16)
{
// There are other colours, need to find right index
for(int i = 16; i < Items.Count; i++)
{
if(ocbItem.Index < ((ComboboxColourItem)Items[i]).Index)
{
bAppend = false;
Items.Insert(i, ocbItem);
break;
}
}
}
if (bAppend)
Items.Add(ocbItem);
return ocbItem;
}
You can use EditingControlDataGridView to find the DataGridView which owns the editing control. Then you can use CurrentCell property of grid to find the current cell and using ColumnIndex you will find the column index. Then using Columns collection, you can get the column at that index:
var c = this.EditingControlDataGridView
.Columns[this.EditingControlDataGridView.CurrentCell.ColumnIndex]
as DataGridViewComboBoxColumn;
if (c != null)
c.Items.Add("Something");

C# display components based on name

Is it possible to set a components visible attribute based on its name?
I have 12 "master" components (comboboxes) if you want to call them that and based on the selection in these I want to display anywhere from 1 to 16 textboxes. These are named in numeric order such as combobox1_textbox_0, combobox1_textbox_1 and so on. What I would like to do ideally is take the index of the combobox and pass it as a parameter to a method that sets the textboxes visible attribute to visible/hidden depending on the index passed into the method.
Is this possible? in pseudocode or what you call it I would like it to work something like this:
private void methodToSetVisibleAttribute(int indexFromMainComboBox)
{
for(int i = 0; i < 15; i++)
{
if(i < index)
{
combobox1_textbox_+i.Visible = true;
}
else
{
combobox1_textbox_+i.Visible = false;
}
}
}
I could do panels or something for the choices but seeing as all the selections from the combobox will use the same textboxes but in different amounts it seems like alot of work to make a panel for every possible selection not to mention difficult to expand the program later on.
Assuming you are using Windows Forms and not WPF, you can use ControlCollection.Find() to find controls by name:
var textBox = this.Controls.Find(string.Format("combobox1_textbox_{0}", i), true).OfType<ComboBox>().FirstOrDefault();
if (textBox != null)
textBox.Visible = (i < index);
else
Debug.Assert(false, "textbox not found"); // Or throw an exception if you prefer.
I'll suggest an alternative to your approach, maybe not quite what you're looking for:
Place your combo boxes in a List<ComboBox> and you can access them by an index number.
List<ComboBox> myCombos = new List<ComboBox>();
for (int i = 0; i < 16; i++)
{
ComboBox cb = new ComboBox();
//do what ever you need to do here. Set its location, add items, etc.
Form1.Controls.Add(cb); //Alternatively add it to another container.
myCombos.Add(cb); //Now it's in a list.
}
Modify them like this:
for(int i = 0; i < 15; i++)
{
if(i < index)
{
myCombos[i].Visible = true;
}
else
{
myCombos[i].Visible = false;
}
}
Or even more succintly:
for(int i = 0; i < 15; i++)
{
myCombos[i].Visible = i < index;
}

Index was outside the bounds of the array in CheckedListBox

I am getting an index error on my else if statement but I'm unable to find the reason for it.
What I am doing is going through a CheckedListBox, if no values are checked print an error else show the selected values in a MessageBox.
Can anybody help me? Thank you!
for (int i = 0; i < checkedListBox1.Items.Count; i++)
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
return;
}
else if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.CheckedItems[i].ToString());
}
Move the Count-ckeck before the loop:
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
return;
}
But the important part is that you are looping all items. Then you check for every item if it is checked with GetItemChecked. That's fine, but then you use checkedListBox1.CheckedItems[i] which doesn't contain all items but only the checked items. That's why you get the Index was outside the bounds error.
Instead you just need to use that collection instead of looping all:
for(int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
{
MessageBox.Show(checkedListBox1.CheckedItems[i].ToString());
}
You should change
i < checkedListBox1.Items.Count;
To:
i < checkedListBox1.CheckedItems.Count;
checkedListBox1.CheckedItems[i] is the problem. You loop through all the items, but indexing CheckedItems. So when you have 10 items and checked 2nd item and 8th item, CheckedItems will have only two items but you'll be accessing CheckedItems[7] that's why you get the exception.
Use CheckedItems collection to access checked items directly.
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
return;
}
foreach (var checkedItem in checkedListBox1.CheckedItems)
{
MessageBox.Show(checkedItem.ToString());
}
Why are you checking for CheckedItems.Count inside the for cycle?
Take the first part of the If clause outside of the For cycle.
At the end your code can look like that:
if (checkedListBox1.CheckedItems.Count == 0)
{
Empty.SetError(checkedListBox1, "Please select at Least One");
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
if (checkedListBox1.GetItemChecked(i))
{
MessageBox.Show(checkedListBox1.Items[i].ToString());
}

Listbox For statement not looping through listboxs

4th time asking this question after trying to apply so fixes. My problem is I have 2 listboxs one holds IDs and the other holds Cell ids. The ID listbox has 10 items (for testing) and Cell boxes have 5 ID. I'm trying to process listId and listCell at the same time creating update links...
example MakeReq will create txtWebUpdate.Text listID listCell &ire=1, in which is
store.domain.com/101/sec01&ire=1
store.domain.com/102/sec02&ire=1
store.domain.com/103/sec03&ire=1
store.domain.com/104/sec04&ire=1
store.domain.com/105/sec05&ire=1
store.domain.com/106/sec01&ire=1 <- notice how listCell starts over it
continues to loop applying sections until the ListID is complete.
here's the code i've been working with. what's happening with my code is that it's only selecting the first item. It's not going to the next item after its done.
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() +
"&ire=1", listCell.Items[b].ToString());
//System.Threading.Thread.Sleep(5);
}
}
}
Please only comment if you can help. I'm almost positive that the problem is the for statement and how it's selecting list items, but I may be wrong.
Edit More Info: after testing an example/suggestion for #duffp below the problem is definitely with my For statements. What's happening is its counting that I have 5 entries in ListCell and then output one (the same) ListID 5times but with a different ListCell. Can someone help me write it following what I wrong above?
It may have something to do within the MakeReq method call. I just tried the following code:
private void Form1_Load(object sender, EventArgs e)
{
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(listId.Items[a].ToString(), listCell.Items[b].ToString());
}
}
}
}
public void MakeReq(string listId, string cellId)
{
Console.WriteLine("store.domain.com/" + listId + "sec01&ire=" + cellId);
}
And it gave the output:
store.domain.com/ID_10sec01&ire=Cell_1
store.domain.com/ID_10sec01&ire=Cell_2
store.domain.com/ID_10sec01&ire=Cell_3
store.domain.com/ID_10sec01&ire=Cell_4
store.domain.com/ID_10sec01&ire=Cell_5
store.domain.com/ID_10sec01&ire=Cell_6
store.domain.com/ID_10sec01&ire=Cell_7
store.domain.com/ID_10sec01&ire=Cell_8
store.domain.com/ID_10sec01&ire=Cell_9
store.domain.com/ID_10sec01&ire=Cell_10
Isn't that what you wanted?
I think there might be some side-effects in MakeReq that you are not showing us. How about adding some debug statements to your code, like this:
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
int listCellCount = listCell.Items.Count;
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() +
"&ire=1", listCell.Items[b].ToString());
//System.Threading.Thread.Sleep(5);
Debug.Assert(listCellCount == listCell.Items.Count);
}
}
}
The assertion (listCellCount == listCell.Items.Count) should always be true, unless something is changing listCell.Items.

Categories

Resources