InvalidArgument=Value of '5' is not valid for 'SelectedIndex' - c#

I read saved data from tbl in a list, and i want to edit the object, so when i start the program, combobox first to show saved value for that object, and others also to be in the combobox. Please help !
if (lstP.Count > 0)
{
for (int i = 0; i < lstP.Count; i++)
{
if (Stav.IDP == lstP[i].SP)
{
Prim.SelectedIndex = lstP[i].SP;
//ERROR
break;
}
}
}

SelectedIndex requires a number to be passed. What you need is to assign an i to it:
if (lstP.Count > 0)
{
for (int i = 0; i < lstP.Count; i++)
{
if (Stav.IDP == lstP[i].SP)
{
Prim.SelectedIndex = i;
break;
}
}
}

Related

Referencing components of objects from an array in C#

I'm trying to make a "thing" that checks/unchecks a specific object's interactable checkbox from an array by using player pref integers. The problem I'm having is I can't seem to reference specific objects from an array, please help.
Here's some script:
//This part is from the Start function.
for (int i = 0; i < buttons.Length; i++) {
if (PlayerPrefs.GetInt("button" + i) == null) {
PlayerPrefs.SetInt("button" + i, 1);
}
if (PlayerPrefs.GetInt("button" + i) == 1) {
button.interactable = true;
} else {
button.interactable = false;
}
}
void Update () {
for (int i = 0; i < buttons.Length; i++) {
if (PlayerPrefs.GetInt("button" + i) == 0) {
button.interactable = false;
}
}
}
The areas where you can see button.interactable = true/false are where I'm having problems.
If you don't have button defined somewhere else; I assume you are missing the concept of array index accessors; you might want to use buttons[i] instead of button.

How to change exact text value become consist or contains?

Is there a way to convert it, from exact value text become consist of? So I don't need to type Ballet instead of Bal.
Here's the code:
private void button6_Click_1(object sender, EventArgs e)
{
ColumnView View = gridControl1.MainView as ColumnView;
View.BeginUpdate();
try
{
int rowHandle = 0;
DevExpress.XtraGrid.Columns.GridColumn col = View.Columns["genre"];
while (true)
{
// // Locate the next row
rowHandle = View.LocateByValue(rowHandle, col, textBox6.Text);
// // Exit the loop if no row is found
if (rowHandle == DevExpress.XtraGrid.GridControl.InvalidRowHandle)
break;
//// Perform specific operations on the found row
gridView1.FocusedRowHandle = rowHandle;
rowHandle++;
}
}
finally { View.EndUpdate(); }
}
for (int i = 0; i < gridView1.VisibleRowCount; i++)
{
var row = gridView1.GetDataRow(i);
var genre = row["ColumnName"].ToString(); //ColumnName is your genre Column name
if(genre.StartsWith(textBox6.text)){
//here you can set row sellected
}
}
I dont have experience with devexpress but you can try it like this.
i dont if this what u seek, but it solved my own problem
for (int i = 0; i < gridView1.RowCount; i++)
{
var rosw = gridView1.GetDataRow(i);
var genre = rosw["genre"].ToString();
int tmpg = 0;
// //tmpg = genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase);
if (genre.IndexOf(textBox8.Text, StringComparison.OrdinalIgnoreCase) >= 0)
{
//if (tmpg >= 1)
// MessageBox.Show(genre);
gridView1.FocusedRowHandle = i;
break;
}
}

How can I use greater than on Gridview text?

I have a Gridview like this:
I need to change backcolor when Zero line greater than %10.
I tried like this (for just 15%):
if (e.Row.Cells[0].Text == "Zero")
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text == "15%")
{
e.Row.Cells[i].BackColor = System.Drawing.Color.LightCoral;
}
}
}
I can't convert Row.Cells[i].Text to Int because of %. How can I use greater than 10 for backcolor?
just replace
if (e.Row.Cells[i].Text == "15%")
with
if (int.Parse(e.Row.Cells[i].Text.Trim('%'))>10)

having problems with checklistbox click events

This code works but the results show up when I check and then uncheck again in the first checkbox. I want to check only once.Does anyone have an opinion? Thanks!
private void clb_grup_MouseClick(object sender, MouseEventArgs e)
{
Liste.Items.Clear();
string s = "";
foreach (var item in clb_kisiler.CheckedItems)
{
s = s + item.ToString() + " ";
}
string[] secilenler = s.Split(' ');
if (clb_grup.GetItemChecked(0) == true)
{
for (int x = 0; x < clb_kisiler.Items.Count; x++)
{
clb_kisiler.SetItemChecked(x, true);
}
for (int i = 0; i < clb_kisiler.Items.Count; i++)
{
Liste.Items.Add(clb_kisiler.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(1) == true)
{
for (int x = 0; x < clb_idari.Items.Count; x++)
{
clb_idari.SetItemChecked(x, true);
}
for (int i = 0; i < clb_idari.Items.Count; i++)
{
Liste.Items.Add(clb_idari.Items[i].ToString());
}
}
else if (clb_grup.GetItemChecked(2) == true)
{
for (int x = 0; x < clb_teknik.Items.Count; x++)
{
clb_teknik.SetItemChecked(x, true);
}
for (int i = 0; i < clb_teknik.Items.Count; i++)
{
Liste.Items.Add(clb_teknik.Items[i].ToString());
}
}
foreach (object i in clb_kisiler.CheckedItems)
{
Liste.Items.Add(i.ToString());
}
}
NEW CODE
My friend used string arrays instead of the combo boxes.She is also having the same problem as me,and she also tried to write a code that would prevent dublicate items being showed up in the list box.She has one checked list box the same as me,her second checked list box is empty,and one list box.Heres the code.
private void chklstbx_bolum_ItemCheck(object sender, ItemCheckEventArgs e)
{
//event for the first check list box
string[] tumu = { "Jane", "Tammy", "John", "Emily", "Susan", "Julie", "Amelia", "Katherine" };
string[] idari = { "Julie", "Amelia", "Katherine" };
string[] teknik = { "Jane", "Tammy", "John", "Emily", "Susan" };
if (chklstbx_bolum.GetItemChecked(0) == true)
{
//if the first box in the first check list box is checked then do this
//this part of the code works fine but it doesnt check if there are //duplicates of the same name in the list box
chklstbx_sonuc.Items.Clear();
for (int i = 0; i < 5;i++ )
{
chklstbx_sonuc.Items.Add(teknik[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(teknik[i]);
}
}
else if (chklstbx_bolum.GetItemChecked(1) == true){
//do this if the second box in the first check box list is checked
//Here the program checks to see if there are duplicates when adding from the second check list box but the program just freezes.
chklstbx_sonuc.Items.Clear();
int x=0;
do
{
for (int i = 0; i < 3; i++)
{
chklstbx_sonuc.Items.Add(idari[i]);
chklstbx_sonuc.SetItemChecked(i, true);
lstbx_sonuc.Items.Add(idari[i]);
}
} while ( x== 0);
x++;
for (int t = 0; t < lstbx_sonuc.Items.Count; t++)
{
for (int s = 0; s < chklstbx_sonuc.Items.Count; s++)
{
if (chklstbx_sonuc.Items[s].ToString() != lstbx_sonuc.Items[t].ToString())
lstbx_sonuc.Items.Add(chklstbx_sonuc.Items[s]);
}
}
}
}
So now the second question is why does the second else if statement create a problem and makes the program not respond? And we still want to know how are we able to transfer the items in the string array without having to check and uncheck but directly transfer when its checked?
The click event is not the right one. you should use the check event and then verify in the event arguments if the checkbox has been checked or unchecked.
EDIT
I've read your other problem. There is an easier solution than manually checking which has been checked.
You need to assign a value to each checkbox with the ID of the list you want to transfer. You can then check in your code which value has been checked (likely through the sender) and use FindControl(checkboxvalue) to find the list that you want to transfer. Then you first transfer all the items, and then you check them. As I mentioned above, you also need to verify in your checkedeventargs (or whatever the eventargs parameter of the checked event is) if the checkbox is currently checked or unchecked.

Getting position in a list and comparasion

I am trying to get positions in a list of some values to compare them with another list.
for (int i = 0; i <= commands.ToArray().Length; i++)
{
levensheteinvalues_commands.Add(commands.ToArray()[i].ToString());
levensheteinvalues_numbers.Add(
Program.ComputeLevenshteinDistance(args[0],
commands.ToArray()[i].ToString()));
}
for (int i = 0; i <= commands.ToArray().Length; i++)
{
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
Why doesn't
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
work? And when I get the value how can I compare it to another list?
The code you have posted have some serious problems. This May solve your problem since your code as well as your approach is very unclear and ambiguous. I have blindly edited the code to fix the serious problems.
for (int i = 0; i < commands.Count(); i++) {
levensheteinvalues_commands.Add(commands.ElementAt(i).ToString());
Program.ComputeLevenshteinDistance(args[0], commands.ElementAt(i).ToString()));
}
for (int i = 0; i < commands.Count(); i++) {
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
int index = levensheteinvalues_numbers.IndexOf(levensheteinvalues_numbers.Min());

Categories