lets say i got combobox populated with items (items are some numbers for this particular example), and i want to tell C# something like this: IF there is some item selected in combobox, get that item and multiply it by 2. Is there any way that i can do this?
Assuming it was populated by a list of ints. e.g
for(int index = 0; index < 10; index++)
{
myCombobox.Add(index);
}
if (myCombobox.SelectedItem != null)
{
int value = ((int)myCombobox.SelectedItem) * 2;
}
Winforms of course
If they are strings then it would be something ike
if (myCombobox.SelectedItem != null)
{
int value = int.Parse(myCombobox.SelectedItem.ToString()) * 2;
}
Related
I'm trying to multiply the values of 2 listboxes together and make their product appear at another list box I'm getting the results I need but the problem is when I rerun the loop using a command button the listbox removes the next instance of the first value calculated by ppc[i] * qty[i] but when I try to remove the the listBox4.Items.Remove(ppc[i] * qty[i]) it reprints the whole array again from first element to last element
string myString = textBox1.Text.ToString();
int index = listBox6.FindString(myString, -1);
int[] qty = new int[99];
int[] ppc = new int[99];
int[] gt1 = new int[99];
listBox3.Items.Add(listBox5.Items[index]);
listBox1.Items.Add(textBox2.Text.ToString());
if (index != -1)
{
listBox6.SetSelected(index, true);
listBox2.Items.Add(textBox1.Text); //name
}
listBox3.Items.Add(listBox5.Items[index]);
listBox3.Items.Remove(listBox5.Items[index]);
for (int i = 0; i != listBox2.Items.Count ; i++)
{
ppc[i] = Convert.ToInt32(listBox3.Items[i]);
qty[i] = Convert.ToInt32(listBox1.Items[i]);
listBox4.Items.Remove(ppc[i] * qty[i]);
listBox4.Items.Add((ppc[i] * qty[i]));
}
My understanding is that this loop works once, and then when it is re-run it is out of order. Are you making sure to clear listbox4 each time this loop is executed? Also since listBox2 isn't used, it is probably better not to use it for your loop bounds.
if(listBox1.Items.Count == listBox3.Items.Count)
{
int rowCount = listBox1.Items.Count;
listBox4.Items.Clear();
for (int i=0; i < rowCount; i++)
{
ppc[i] = Convert.ToInt32(listBox3.Items[i]);
qty[i] = Convert.ToInt32(listBox1.Items[i]);
listBox4.Items.Insert(i , (ppc[i] * qty[i]));
}
}
I have an array with six numbers. I'd like to perform a certain equation on each of these numbers, and then place the result in a series of textboxes, corresponding with the position within the array.
Eg. result of equation on the value of pos 0 in array goes into textbox01, result of pos1 into textbox02, etc.
I have the following code:
for (int i = 0; i <= 5; i++)
{
if ((Convert.ToInt32(statArray.GetValue(i))-10)%2 == 0)
{
//txtMod01.Text = Convert.ToString((Convert.ToInt32(statArray.GetValue(i)) - 10) / 2);
}
else
{
txtMod01.Text = Convert.ToString((Convert.ToInt32(statArray.GetValue(i)) - 11) / 2);
}
}
I'd like to automatically change the name of the textbox (eg. txtMod01) to the following textbox in the series (txtMod02).
Is their any way to do this?
You can use reflection, which allows you manipulate types at runtime:
// property name "txtMod0x"
string propertyName = "txtMod" + i.ToString().PadLeft(2, '0');
// get the property from the current type
PropertyInfo prop = this.GetType().GetProperty(propertyName);
if (prop != null)
{
// get the property value (the TextBox in this case)
var textBox = (TextBox)prop.GetValue(this, null);
string val = Convert.ToString((Convert.ToInt32(statArray.GetValue(i)) - 11) / 2);
textBox.Text = val;
}
You could put your Textboxs in an array as well, something like:
TextBox[] boxes = new TextBox[]{txtbox01, txtbox02, txtbox03, txtbox04, txtbox05, txtbox06};
int[] values = new int[]{val1, val2,val3, val4,val5, val6};
for(int i=0; i < values.Count; ++i)
{
//perform calculations
...
boxes[i].Text = values[i];
}
int i;
int [,] Prices = new int [2, 7]{{1,2,3,4,5,6,7},{700,600,500,400,300,200,100}};
string[,] City = new string [2,1]{{"A"},{"B"}};
bool found = false;
for (i = 0; i <= City.Length -1; i++)
// for (y = 0; y <= City.Length - 1; y++)
{
if (LstDestinationCity.Text == City[i]) <<-- i get error here
{
im planing to do a program that if i select A city i get first row if B city i get 2 row
I think that's because City[i] "don't contain anything" you should check City[i,0]
if (LstDestinationCity.Text == City[i,0])// this should access the first element which is the text you are looking for
I would rather do it as
if (LstDestinationCity.Text == City[i,i])
{
// ...
}
Your City variable does not need to be a two dimensional array. If you change it to a one dimensional array you can access the values with one index instead of 2.
string[] City = new string [2]{"A","B"};
How do I get the info from a certain cell in a data grid? I want an event to happen when a person clicks the button in the 7th column, but the event depends on the value in the first column. here's what i have, but nothing is happening.
if (InventoryDataGridView.CurrentCell.ColumnIndex == 7)
{
if(InventoryDataGridView[0,0].Equals("Books"))
{
Books open = new Books();
open.Show();
}
}
Nothing happens though
InventoryDataGridView[0,0]
only refers to the DataGridViewCell. That class has a Value property which contains the value of the cell.
so that line should look like this:
if(InventoryDataGridView[0,0].Value.Equals("Books"))
{
//
}
If you using WPF then you must try this:
for (int j = 0; j < dataGrid1.Columns.Count; j++)
{
for (int i = 0; i < dataGrid1.Items.Count - 1; i++)
{
string s=(dataGrid1.Items[i] as DataRowView).Row.ItemArray[j].ToString();
}
}
i,j are the co-ordinates. So you can play around it.
reference: http://subrat308.blogspot.in/2012/02/wpf-get-cell-value-from-datagrid-cellij.html
how i can select five items in the single click on the list box??
if i click any item, i just want to +2 and -2 from the selected index. so my single click need to select five items in the listview.
Am using C#(WPF).
I am not sure what you want to do exactly, but trying. =)
Have a look at the Click event of the ListBox. You can do anything in there, including selecting five items of your choice. You can do it like this (untested, but gives you an idea):
int sindex = listBox1.SelectedIndex;
listBox1.SelectedItems.Clear();
for(int i = Math.Max(sindex - 2, 0); i < Math.Min(sindex + 2, listBox1.Items.Count()), i++)
{
listBox1.SelectedItems.Add(listBox1.Items[i]);
}
Another thing would be setting the SelectionMode to Multiple or Extended. Does this result in the behaviour you are looking for?
have a look at selectionchanged event, and get the index of the selected item and make it +2 and -2
i tried it like this and it works:
void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int idx = list.SelectedIndex;
int startIdx = idx - 2;
int endIdx = idx + 2;
if (startIdx < 0)
{
startIdx = 0;
}
if (endIdx >= list.Items.Count)
{
endIdx = list.Items.Count-1;
}
for (int i = startIdx; i <= endIdx; i++)
{
if (i != idx)
{
list.SelectedItems.Add(list.Items[i]);
}
}
}
one problem with this code is you can still use ctrl to select another item so it makes the selecteditems count increased