Identify the Listbox Item through specific item - c#

private void Form1_Load(object sender, EventArgs e){
for (int i = 0; i < listBox1.Items.Count; i++)
{
lst.Add(listBox1.Items[i].ToString());
}
foreach (var item in lst)
{
lst1.Add(item[2].ToString());
}
}
private void button1_Click(object sender, EventArgs e) {
if (lst1.Contains(textBox1.Text))
{
// *Need to find that particular item from listbox and clear rest of them*\\
}
}
my input is
1-2-3-4-5
6-7-8-9-10
1-9-4-2-3
7-8-1-4-9
so when textbox has value 7
then my listbox must show 6-7-8-9-10 as output and clear rest all items in listbox

Using what you have posted, I do not understanding what exactly you are trying to achieve. Using the two (2) List’s lst and lst1 looks very odd. Without having more information as to what your ultimate goal is I question why you would do this the way you are.
The code below removes the items in the ListBox where the second character does not match the character in the text box. Hope this helps.
private void button1_Click(object sender, EventArgs e) {
if (lst1.Contains(textBox1.Text)) {
int index = lst1.IndexOf(textBox1.Text);
string temp = listBox1.Items[index].ToString();
MessageBox.Show("Character: " + textBox1.Text + " Found at index: " + index + " the string is: " + temp);
listBox1.Items.Clear();
listBox1.Items.Add(temp);
// *Need to find that particular item from listbox and clear rest of them*\\
} else {
MessageBox.Show("Not Found");
}
}

Related

How to make a listbox item not duplicate itself to the next line of the listbox in C#

so I am adding an item in a listbox by clicking an image.
If the item gets clicked or added multiple times,it duplicates itself to the next line of the listbox. What I want is the item to have a counter that will count the number of instance it was clicked.
here's my code so far:
int ctr = 1;
private void item_img1_Click(object sender, EventArgs e)
{
if (!orderList.Items.Contains(item1.Text))
{
orderList.Items.Add(item1.Text + ctr);
ctr++;
}
}
Note that you're not actually adding item1.Text; you're adding item1.Text + ctr. That's why your if clause isn't keeping you from adding duplicates.
Use this code:
class ItemWrapper
{
public object item;
public string text;
public int ctr = 1;
public override string ToString()
{
return text + " (" + ctr + ")";
}
}
private void item_img1_Click(object sender, EventArgs e)
{
bool found = false;
foreach (var itm in orderList.Items)
if ((itm as ItemWrapper).text == item1.Text)
{
(itm as ItemWrapper).ctr++;
found = true;
break;
}
if (!found)
orderList.Items.Add(new ItemWrapper() { item = item1, text = item1.Text, ctr = 1 });
}
Where ItemWrapper is wrapper of your item object and overriding ToString() method in it allows listBox to display object as your defined format.

Calculate value automatically when selecting a decimal in combobox

Im trying to get the selected value of my 4 comboboxes and add them together automatically in a windows form.
The comboboxes items are decimals, 0,75 , 0,8 etc.
How do i add all the values selected from the comboboxes together into a textbox?
I have tried for 5 hours now and really cant figure it out.
FYI im really a beginner.
Thanks!
You can handle TextChanged event on all combo boxes, calculate the sum and assign the result to the text box.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var comboBox in this.Controls.OfType<ComboBox>())
{
comboBox.TextChanged += ComboBox_TextChanged;
InitializeComboBox(comboBox);
}
}
private void ComboBox_TextChanged(object sender, EventArgs e)
{
double result = 0;
foreach (var comboBox in this.Controls.OfType<ComboBox>())
{
if (!string.IsNullOrEmpty(comboBox.Text))
{
result += Convert.ToDouble(comboBox.Text);
}
}
textBox1.Text = result.ToString();
}
private void InitializeComboBox(ComboBox comboBox)
{
for (int index = 0; index < 10; index++)
{
comboBox.Items.Add(index + 0.5);
}
}

transfer items from one listbox to another

so far i have accomplished transfer a single select item from one lb1 to lb2 and vice versa. Now the problem is transfering the whole list of data from lb1 to lb2 and vice versa. if anyone could help please.
using for loop will be much better.
i am using the following code:
private void add_Click(object sender, EventArgs e)
{
if (lb1.SelectedItem != null)
{
lb2.Items.Add(lb1.SelectedItem);
lb1.Items.Remove(lb1.SelectedItem);
}
else
{
MessageBox.Show("No item selected");
}
}
private void remove_Click(object sender, EventArgs e)
{
if (lb2.SelectedItem != null)
{
lb1.Items.Add(lb2.SelectedItem);
lb2.Items.Remove(lb2.SelectedItem);
}
else
{
MessageBox.Show("No item selected");
}
}
private void addall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb1 to lb2
{
for (int i = 0; i < lb1.SelectedItems.Count; i++)
{
lB2.Items.Add(lb1.SelectedItems[i].ToString());
}
}
private void removeall_Click(object sender, EventArgs e) //problem is here. adding all the items from lb2 to lb1
{
}
Because you want to transfer all of the items from one list box to another not only the selected items you must loop for every item in the list box and not only the selected items.
So change your usage of ListBox.SelectedItems to ListBox.Items and your code should work as expected assuming you remember to remove the items as well.
for (int i = 0; i < lb1.Items.Count; i++)
{
lB2.Items.Add(lb1.Items[i].ToString());
}
lb1.Items.Clear();
You could do something like this:
private void addall_Click(object sender, EventArgs e)
{
foreach (var item in lb1.Items)
{
lB2.Items.Add(item);
}
}
This would loop through your list and add them all. You would do the reverse for lb2->lb1.
Simply iterate over all Items of the list and add each element to the next list. At the end simply remove all Items.
foreach (var item in listBox1.Items)
{
listBox2.Items.Add(item);
}
listBox1.Items.Clear();

C# form - Checkboxes

I'm wondering how to restrict my checkbox from adding to my listbox. At the moment when the user checks the checkbox it will add "Anchovies" to the listbox. What I don't want to happen is when the user deselects the checkbox and re selects it again, "Anchovies" is added to the listbox again (showing two lots of "Anchovies").
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
}
The key is to check if Anchovies already exists on the listBox1 items.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
//If the item is already there, we don't do anything.
if (!listBox1.Items.Contains("Anchovies")) {
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
}
}
Do it this way
if (checkBox1.Checked)
{
if(!listBox1.Items.Contains("Anchovies"))
listBox1.Items.Add("Anchovies");
double total = Convert.ToDouble(textBox2.Text);
total = total + .5;
textBox2.Text = Convert.ToString(total);
}
To fix this issue, you need to check your list box(for this value, either it is already there or not) before inserting any value in it.
e.g
foreach (var item in listBox1.Items )
{
if(item.ToString() != "Anchovies")
{
listBox1.Items.Add("Anchovies");
}
// other code here.
}

Search items in the listbox using index

I am doing a project that uses listbox, I can add items, delete items, update items but I can't search,
this is my code for search
string search = Person.listperson[listBox1.SelectedIndex].lastname;
foreach (String s in search)
{
if (s.Equals(textBox6.Text))
{
//show searched items
MessageBox.Show("Success!");
}
}
can you help me with this?
thanks :)
I have here a code for search,
But it does not apply in the button, how can I apply this on the button?
private void textBox6_TextChanged(object sender, EventArgs e)
{
int index = listBox1.FindString(this.textBox6.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}
}
Try something like this, add a click event to your button and put your code in it. This works for me.
private void button1_Click(object sender, EventArgs e)
{
int index = listBox1.FindString(textBox6.Text);
if (index > -1)
{
listBox1.SelectedIndex = index;
}
}
Not sure what you're trying to do exactly, but here's some samples.
Also, start giving variables useful names. If you come back to this code in a month you'll have no idea what's going on there or what textBox6 is.
To find a string (textBox6) in the entire listperson collection:
var list = Person.listperson.Where(p => p.lastname.Contains(textBox6.Text));
To check if a specific item in listperson has a partial textBox6 value:
var search = Person.listperson[listBox1.SelectedIndex].lastname;
bool success = search.Contains(textBox6.Text);
Or if you'd rather compare the values:
bool success = (search == textBox6.Text);
you can foreach char in a string
string s = "Blippy you";
foreach (char item in s)
{
}
but anywho. try Text.RegularExpressions for string matching.
private void button1_Click(object sender, System.EventArgs e)
{
if (listBox1.FindString("Your String in Textbox 6") != -1)
{
MessageBox.Show("Success!");
}
}

Categories

Resources