having problems with checklistbox click events - c#

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.

Related

How to disable formatting equals column on other datagrid?

I am developing a Windows Forms Application for lending. When I click on the datagrid1 add (green) button the equipment is added on datagrid2, but I would like that to only happen once for each item.
I am using this code to pass the line data:
private void DataGridEquipamento_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
{
return;
}
if (e.ColumnIndex == 2)
{
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
}
There's a couple of ways to achieve what I believe you're asking for.
The first way would be to actually remove the item from the first list once it's been added to the second. This prevents the user from even attempting to add more than one, and ensures what the interface is presenting makes contextual sense.
With that said, to answer the question exactly, because you're effectively cloning the Row, you can perform a comparison to determine if it's already been added:
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
if (!this.dataGridEmprestimo.Rows.Contains(rowData))
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}
In terms of optimisation, you may only need to check for a single value from the Row - such as the Id - to determine if it already exists. You could write a simple method to return a count of Rows with that Id. If there are none, or if the number is less than the maximum allowed, then you're free to add that Row.
public int CountObjectsInRows(string idValue)
{
int count = 0;
if (string.IsNullOrEmpty(idValue))
{
return count;
}
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
if (row.Cells[0].Value.ToString() == idValue)
{
count++;
}
}
return count;
}
You could use this method in place of the above "this.dataGridEmprestimo.Rows.Contains(rowData)" condition.
foreach (DataGridViewRow row in this.dataGridEquipamento.SelectedRows)
{
object[] rowData = new object[row.Cells.Count];
for (int i = 0; i < rowData.Length; ++i)
{
rowData[i] = row.Cells[i].Value;
}
if (CountObjectsInRows(rowData.Cells[0].ToString()) == 0)
{
this.dataGridEmprestimo.Rows.Add(rowData);
}
}

InvalidArgument=Value of '5' is not valid for 'SelectedIndex'

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;
}
}
}

Want to check whether a gridview contain duplicate value

I want to check a wether a gridview contain duplicate value on button click event
This will find the duplicate values in dataGridView and add them to a alreadySeen list. Then show message box.
private void button2_Click_1(object sender, EventArgs e)
{
int i = 0,j=0;
string check1 = "",check2="";
List<string> alreadySeen = new List<string>();
for (i=0;i<dataGridView1.Rows.Count-1;i++)
{
check1 = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
j = i;
for (j=j+1; j < dataGridView1.Rows.Count - 1; j++)
{
check2 = this.dataGridView1.Rows[j].Cells[0].Value.ToString();
if (check1==check2)
{
if (!alreadySeen.Contains(check1))
alreadySeen.Add(check1);
}
}
}
//Show duplicate value
foreach (var x in alreadySeen)
{
MessageBox.Show(x);
}
}

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;
}
}

select items in a checkbox list based on their text

i have a check box list in my asp.net page ...i need to select the check box based on their text...am getting these string values form the database and storing it in a array.....the below code works fine for a single text ..What should i do in case of array..how should i pass the array values in the if loop
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (chkbx.Text == "Dress" )
{
chkbx.Items[i].Selected = true;
}
}
Below code should work for you -
string[] array = { "Dress", "Pen", "Table"};
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (array.Contains(chkbx.Items[i].Text))
{
chkbx.Items[i].Selected = true;
}
}
You probably want to use the Contains method of the array that contains your text values:
for (int i = 0; i < chkbx.Items.Count; i++)
{
if (myArray.Contains(chkbx.Items[i].Text))
{
chkbx.Items[i].Selected = true;
}
}
where myArray is the array of values you're populating from the database.
The following is a nice way of doing it
foreach (string item in myarr)
{
checkboxlist1.Items[checkboxlist1.Items.IndexOf(checkboxlist1.Items.FindByText(item))].Selected = true;
}

Categories