When I set comboBoxEdit.selectedindex = some value, it never take this value. its value is always -1. I have set it in Constructor or in Form_Load.
if (oPersclientEntrp.TypPrint == 1) {
comboBoxEdit_Print.SelectedIndex = 0;
} else {
comboBoxEdit_Print.SelectedIndex = 2;
}
I have heard that The SelectedValue, SelectedIndex, SelectedItem properties can't be set until the control is added to the form. After the control is added to the form, the selectedValue, -Index and -Item properties can be set.
but I bind the value on design mode .
Try updating your code to be this:
if (oPersclientEntrp.TypPrint == 1) { comboBoxEdit_Print.SelectedIndex = 0; }
else { comboBoxEdit_Print.SelectedIndex = 1; }
If you only have 2 items, your SelectIndex should be 1, not 2.
You have 2 Items and the index of SelectedIndex starts with 0 (Because it access' an internal array, which of course starts with 0). So you have to edit your code to use index 0 instead of 1 and index 1 inseatd of 2.
Btw, this is a common behavior of the most SelectedIndex properties, i.e. of TabControl.
Related
I have got a for loop to loop through controls in a TableLayoutPanel. In the TableLayoutPanel, there is a series of questions and the user need to tick one of the four Checkboxes to answer that question. The first question is visible, upon ticking an answer, the second question and its Checkboxes become visible. and so on till the 9th question, which ends the form.
At the end there is a Save button where the back-end code has a for loop to loop through all the checkboxes in the tablelayoutpanel to check which checkboxes are checked which are not checked.
When the loop starts running, it starts checking the Checkboxes in the second column, not from the first, not from the beginning. The first column Checkboxes are checked at the end of the loop, which is messing up the order.
Here is the for loop
foreach (Control control1 in tableLayoutPanel1.Controls.OfType<CheckBox>())
{
CheckBox checkBox = (CheckBox)control1;
String s = checkBox.Name;
//boolean value for checkbox if is checked or not
bool value = checkBox.Checked;
listValue.Add(value);
}
Why is that happening, what can be done to force the loop to start at the first column Checkboxes?
You can just loop through the cells:
for (int y = 0; y < tlp.RowCount; ++y) {
for (int x = 0; x < tlp.ColumnCount; ++x) {
CheckBox cb = tlp.GetControlFromPosition(x, y) as CheckBox;
if (cb != null) {
MessageBox.Show(cb.Name);
}
}
}
When checking the debugger I saw controls added with the designer will take space [0] of the array of controls. So if I add button1, 2 and 3 and run a foreach, I will see button 3 first.
(Added 2 buttons, then 3 checkbox's so its in 'reversed' order by default)
I have found a solution for this, this way you have to name your checkbox's with numbers corresponding to the question number though.
int amountOfQuestions = 3;
int index = 0;
while ( index < amountOfQuestions )
{
index++;
var checkbox = (CheckBox) this.Controls.Find( "CheckBox" + index, false ).First();
var value = checkbox.Checked;
}
You can also add the CheckBox's in an array, that way you decide the order and can add one in between without a hassle:
CheckBox[] checkBoxArray = { checkBox1, checkBox2, checkBox3 };
//Add the checkbox's in the array in the order you want
foreach ( CheckBox checkBox in checkBoxArray )
{
var value = checkBox.Checked;
}
Here is a solution, where you can enumerate the check boxes one of 2 ways, either by the Y coordinate in the table or the row number in the table.
tableLayoutPanel1.Controls.Add(new CheckBox(), 0, 0);
tableLayoutPanel1.Controls.Add(new CheckBox(), 0, 1);
tableLayoutPanel1.Controls.Add(new CheckBox(), 0, 2);
tableLayoutPanel1.Controls.Add(new CheckBox(), 0, 3);
tableLayoutPanel1.Controls.Add(new CheckBox(), 0, 4);
// Option 1
// Enumerate controls in order of height (from first to last on the form)
foreach (var checkBox in tableLayoutPanel1.Controls.OfType<CheckBox>().OrderBy(c => c.Location.Y))
{
// ...
}
// Option 2
// Enumerate controls based on what row they are in the tabletLayoutPanel
foreach (var checkBox in tableLayoutPanel1.Controls.OfType<CheckBox>().OrderBy(c => tableLayoutPanel1.GetRow(c)))
{
// ...
}
I have several items in my ComboBox. But how to tell ComboBox to show only item number 1 from list as default? Currently at design time I have empty text.
You have to set SelectedIndex:
comboBox.SelectedIndex = 1;
You can use the SelectedIndex as show value:
comboBox1.SelectedIndex = 0;
NB: you should check the count of values in your Items array.
MS documentation: https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx
I have a certain number of elements in an array(statsname).
they are in fact as follows
x[1] A_NAME
x[2] A_CATEGORY
x[3] ANOTHER_NAME
x[4] A_CATEGORY
I want the categories in a combobox.
I did
int up =1;
foreach (string things in statsname)
{
//if the stat name doesnot contains TIME
//Only then we add it to the combobox.
if ((Convert.ToString(things[up]) == "CurrentNumber") || (Convert.ToString(things[up]) == "TotalNumber"))
{
tcomboBox1.Items.Add(things[up-1]);
}
up++;
if (up != statsname.Count())
{
tcomboBox1.Items.Add(things[up - 1]);
}
}
However I get an error saying
Array out of bound
Why is it so ?
Where Did I go wrong ?
Problem : you are comapring the character with String, it will never become true.
Solution : if you just want to get all the Categories added in array at odd positions like 1,3,5..etc.,
you can get the odd value out from array and assign the value to combobox.
EDIT : if you want you can get Even value out from array and assign the value to combobox.
Try This:
string [] statsname=new string[] {"A_NAME1","Cat1","A_NAME2","Cat2","A_NAME3","Cat3","A_NAME4","Cat4"};
for(int i=0;i<statsname.Length;i++)
{
if(i%2==0)
tcomboBox1.Items.Add(statsname[i]);
}
An IndexOutOfRangeException has occurred. This happens in C# programs that use array types. This exception will typically occur when a statement tries to access an element at an index greater than the maximum allowable index.
for (int i = 0; i < type.Length; i++)
{
form.comboBox1.Items.Add(type[i]);
}
I have a listbox control on winform and same is Single Items SelectionMode OR One Items Selection Mode. I am trying to scroll it from form_KeyDown Event as below
if ((Keys)e.KeyCode == Keys.Down)
{
if (listBox2.Items.Count >= listBox2.SelectedIndex)
{
listBox2.SelectedIndex++;
}
}
But it’s throw an error like “ArgumentOutOfRangeException was unhandled”
Invalid argument of value =23 is not valid for selection index.
How to get ridoff?
Try this:
if ((Keys)e.KeyCode == Keys.Down)
{
if ((listBox2.Items.Count-1) > listBox2.SelectedIndex)
{
listBox2.SelectedIndex++;
}
}
Remember that if you have 23 items, SelectedIndex goes from 0 to 22...
ListBox.SelectedIndex is a zero based array IE the first item will be 0 in the index whereas the Items.Count will always return a value starting at 1.
Please see the following for further information:
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindex.aspx
Kind Regards, Wayne
According to MSDN's documentation on ListBox.SelectedIndex:
A zero-based index of the currently selected item. A value of negative
one (-1) is returned if no item is selected.
So, I believe you need to change
if (listBox2.Items.Count >= listBox2.SelectedIndex)
to
if (listBox2.Items.Count-1 > listBox2.SelectedIndex)
Please vote Marco's answer as correct, as he pointed this out to me!
Because if there are 23 items in the listbox, item 23 is actually item 22, item 1 is actually item 0, etc. etc.
I have an if statement saying that if a webpage has a certain text to remove the selected item on a listBox and iterate down to the next one.
I made some code but when I try it I keep getting:
ArgumentOutOfRangeException was unhandled by usercode
This is the error in more detail:
{"InvalidArgument=Value of '1' is not valid for
'SelectedIndex'.\r\nParameter name: SelectedIndex"}
This is my code:
listBox1.Items.Remove(listBox1.SelectedItem);
listBox1.SelectedIndex = + 1;
EDIT:
Thanks for all the help guys! I removed the issue by not removing the items and just making it iterate down.
You have to test if the item which you are trying to select acutally exists.
int index = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
If (index < listBox1.Items.Count) {
listBox1.SelectedIndex = index;
}
EDIT: If you want to delete items in a loop, it is a good idea to start at the end, since removing an item changes the position of the following items. Looping upwards would make you skip an item each time you remove an item.
for (int i = listBox1.Items.Count - 1; i >= 0; i--) {
if (listBox1.Items[i].ToString() == "whatever") {
listBox1.Items.RemoveAt(i);
}
}
I'd have to see the full code sample (with the if statement) to know for sure. But it is pretty obvious you are setting the selected item to an index that isn't in the list box.
Be careful, the selectedIndex is zero based, not one based.
MSDN says of your error:
ArgumentOutOfRangeException: The assigned value is less than -1 or
greater than or equal to the item count.
Since the error indicates it is happening when you set the SelectedIndex to 1, I am assuming that you only have a single item in the listbox (index=0) when this code is called.
Since indices are 0-based, setting SelectedIndex to 1 is selecting the second value in the list. I'm guessing it's failing when you have removed all but one of the values and are trying to set SelectedIndex to the second one.
Do you want to select the first item in the listBox? If so the code would be:
if(listBox1.Items.Count > 0) listBox1.SelectedIndex = 0;