Set index number in comboBox to display selected item - 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

Related

How do I limit the rows/indexes of a Listbox to a specified number?

How do I restrict the amount of rows/indexes in a list box from a dynamic amount to a constant amount, like 5, programmatically? For example, a user inputs data from a text box to a list box up to the fifth row. If they attempt again, the program would reject the data the user entered which would prevent the list box from increasing the dynamic row size.
I tried using Selected Index and Selected Items properties such as:
if (ListBox1.SelectedItems.Count != 0)
{
while (ListBox1.SelectedIndex == 5)
{
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
}
but it seems that the properties require a list box index to be selected.
if(ListBox1.Items.Count < 5){
ListBox1.Items.Add("asd");
}
Instead of removing, while inserting, you can check if the number of items in ListBox1 are less than 5 and only add an item if it holds true.

How to update the value in cells in a grid

i have a Grid and i have 4 rows and I need to update the 4th row based on the inputs in the 1st,2nd and 3rd row values .
Example :
1st when you enter 1 the total in the 4th row should be 1
when you enter 1 in 1st row and 2 in 2nd row the total shoud be 3 .
Should we use java script as these rows are in a grid when we hit a pop up on the page .
let me know if I am missing anything
This question has been already discussed in the IG forum at: http://www.infragistics.com/community/forums/t/77638.aspx
Here's what I wrote there:
You could handle the AfterCellUpdateHandler. It will fire after a
cell’s value has been changed so in it you could look through the cell
values and calculate the sum. After that you can change the last
cell’s value. For example:
function UltraWebGrid1_AfterCellUpdateHandler(gridName, cellId){
var cell = igtbl_getCellById(cellId);
var totalCell = cell.Row.getCellFromKey("Total");
var sum = 0;
for (var i = 1; i < cell.Row.cells.length-1; i++) {
sum += cell.Row.getCell(i).getValue();
}
cell.Row.getCellFromKey("Total").setValue(sum);
}
Please refer to the attached sample. In it if you change the value in
any of the cells the value of the related cell from the column “Total”
will be calculated based on the new values.
Let me know if you have any questions.
You can do something like
dataGridView1.Rows[0].Cells[0].Value = Convert.ToInt32(dataGridView1.Rows[1].Cells[0].Value) + Convert.ToInt32(dataGridView1.Rows[2].Cells[0].Value) ;
In the [ ], you will have to enter desired index of row or column.
You can use Unbound field
see this example Unbound field
here you can see unbound field of subtotal,SalesTax and Total and code done at cs side in InitializeRecord event.

Listbox Error On Selection Index

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.

C#: ArgumentOutOfRangeException was unhandled by usercode

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;

c# add a list of items to combobox ordered by string not natural

i use this code to add a numbers to combobox
for (int i = 15; i < 250; i++)
{
cbSumFrom.Items.Add(i);
}
the problem is that i get something like
100
101
......
but i want like
15
16
17
......
how to fix it ?
The problem is is that it appears the combo box is sorting the item and it's doing an ASCII comparison on each character to do it, so 100 comes before 15 because 10 is before 15. Take the sorting off the combo box and it should list them in the order you;ve added them
Take a look at your ComboBox.Sorted property. If it is True then you get your unwanted behavior (default, string-based sort.) Since you are populating the combo box from what looks like a presorted list, make sure that ComboBox.Sorted is set to False.
Try this...didn't tested it but try this...
cbSumFrom.Items.Clear();
for (int i = 15; i < 250;)
{
cbSumFrom.Items.Add(Convert.toString(i));
}

Categories

Resources