I have a timer in every tick it adds an item to listbox (or tuple which has two double integer).
For example:
20.157
20.158
20.159
20.160
...
But I want to separate items to 2 listboxes which has original order and which has ignores first or the last item.
For instance:
Listbox1 Listbox2
20.157 -
20.158 20,157
20.159 20.158
20.160 20,159
... ...
So how can I seperate items in 2 listboxes as I showed you above?
here is my code;
private void timer1_Tick(object sender, EventArgs e)
{
myTuple2.Add(new Tuple<double,double>
(Convert.ToSingle(gPathBoylam),Convert.ToSingle(gPathEnlem)));
}
private void pictureBoxPath_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < myTuple2.Count-1 ; i++)
{
listBox1.Items.Add(myTuple2[i].Item1);
listBox2.Items.Add(myTuple2[i-1].Item1);
}
}
Related
I'm just learning how to code in c# and I'm trying to figure out a way to search and filter out results from a listbox that contains data. Right now I have a listbox and a search button, my listbox contains website history, and my search button finds the item in the list but I cannot find a way to filter out the other items so that only what I searched for in the textbox appears in the listbox. Right now my search button looks like this. Any ideas?
private void searchBtn_Click(object sender, EventArgs e)
{
listBoxHist.SelectedItems.Clear();
for (int i = 0; i < listBoxHist.Items.Count; i++)
{
if (listBoxHist.Items[i].ToString().Contains(textboxSearch.Text))
{
listBoxHist.SetSelected(i, true);
}
}
}
There is a well know 'trick' to remove items while iterating over a collection. You should iterate backwards (from the last item through the first) using the for...loop.
In this way, when you remove an item you don't affect the condition used to exit the loop and you are guaranteed that every item is evaluated.
private void searchBtn_Click(object sender, EventArgs e)
{
for (int i = listBoxHist.Items.Count - 1; i >= 0; i--)
{
if (listBoxHist.Items[i].ToString().Contains(textboxSearch.Text))
listBoxHist.SetSelected(i, true);
else
listBoxHist.Items.RemoveAt(i);
}
}
If you do this code while looping forward you will have problem to evaluate correctly every item. Suppose you remove an item when the index is 3. What happens to item in position 4? It slides down one position and now it occupies the position 3 and this happens to every other item after. Now your loop will increment the index to 4 and will start evaluating the item that was at position 5 before the call to RemoveAt. You have skipped the evaluation of an item.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.Items.Add("B");
ListBox1.Items.Add("A");
ListBox1.Items.Add("P");
ListBox1.Items.Add("X");
ListBox1.Items.Add("F");
ListBox1.Items.Add("S");
ListBox1.Items.Add("Z");
}
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
String txt=txtsearch.Text;
if (ListBox1.Items.FindByText(txt)!= null)
{
// ListBox1.Items.FindByText(txt).Selected = true;
Response.Write("<script> alert('Item found.');</script>");
}
else
{
Response.Write("<script> alert('Item Not found.');</script>");
}
}
}
I am working on a simple application that to add/delete string/s into an array and show that in ListBox.
My code shows only the latest value that was typed into the textBox and
private void Add_Click(object sender, EventArgs e)
{
string add = textBox1.Text;
List<string> ls = new List<string>();
ls.Add(add);
String[] terms = ls.ToArray();
List.Items.Clear();
foreach (var item in terms)
{
List.Items.Add(item);
}
}
private void Delete_Click(object sender, EventArgs e)
{
}
This code makes no sense. You are adding one single item to a list, then convert it to an array (still containg one item) and finally loop through this array, which of course adds one item to the previously cleared listbox. Therefore your listbox will always contain one single item. Why not simply add the item directly?
private void Add_Click(object sender, EventArgs e)
{
List.Items.Add(textBox1.Text);
}
private void Delete_Click(object sender, EventArgs e)
{
List.Items.Clear();
}
Also clear the listbox in Delete_Click instead of Add_Click.
If you prefer to keep the items in a separate collection, use a List<string>, and assign it to the DataSource property of the listbox.
Whenever you want the listbox to be updated, assign it null, then re-assign the list.
private List<string> ls = new List<string>();
private void Add_Click(object sender, EventArgs e)
{
string add = textBox1.Text;
// Avoid adding same item twice
if (!ls.Contains(add)) {
ls.Add(add);
RefreshListBox();
}
}
private void Delete_Click(object sender, EventArgs e)
{
// Delete the selected items.
// Delete in reverse order, otherwise the indices of not yet deleted items will change
// and not reflect the indices returned by SelectedIndices collection anymore.
for (int i = List.SelectedIndices.Count - 1; i >= 0; i--) {
ls.RemoveAt(List.SelectedIndices[i]);
}
RefreshListBox();
}
private void RefreshListBox()
{
List.DataSource = null;
List.DataSource = ls;
}
The problem with code is quite simple. Instead of adding new item to list your code creates new list with one added item only. I am trying to interpret functions of program and they seem to be:
Enter new text into top level text box.
If Add button is clicked your item goes on top of the list (if it's bottom see end of my answer).
If item(s) is selected in list and Delete is clicked selected item(s) is/are deleted.
To achieve this you should first insert text on top of the list by using Add_Click code, than delete selected items using Delete_Click code. There is additional code to guard against inserting empty or white space only strings plus trimming of leading and trailing white space.
private void Add_Click(object sender, EventArgs e)
{
// Since you do not want to add empty or null
// strings check for it and skip adding if check fails
if (!String.IsNullEmptyOrWhiteSpace(textBox1.Text)
{
// Good habit is to remove trailing and leading
// white space what Trim() method does
List.Items.Insert(0, textBox1.Text.Trim());
}
}
private void Delete_Click(object sender, EventArgs e)
{
// Get all selected items indices first
var selectedIndices = List.SelectedIndices;
// Remove every selected item using it's index
foreach(int i in selectedIndices)
List.Items.RemoveAt(i);
}
To complete adding and deleting logic I would add Delete All button which would just call List.Items.Clear(). If you prefer to add text at the end just use Add method form #Olivier Jacot-Descombes answer.
You can use in C#:
private void Delete_Click(object sender, EventArgs e)
{
if(myList.Contains(textbox1.value))//if your list containt the delete value
{
myList.Remove(textbox1.value); //delete this value
}
else
{
//the list not containt this value
}
}
and you can use the same method for validate if a value exist when try to add
private void AddItem()
{
if (!String.IsNullEmptyOrWhiteSpace(textBox1.Text))
{
var newItem = textBox1.Text.Trim();
if (!List.Items.Contains(newItem))
{
List.Items.Add(newItem);
// Alternative if you want the item at the top of the list instead of the bottom
//List.Items.Insert(0, newItem);
//Prepare to enter another item
textBox1.Text = String.Empty;
textBox1.Focus();
}
}
}
private void Add_Click(object sender, EventArgs e)
{
AddItem();
}
Private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
AddItem();
}
}
private void Delete_Click(object sender, EventArgs e)
{
// Remove every selected item using it's index
foreach(var item in List.SelectedItems)
{
List.Items.Remove(item);
}
}
I need to implement a ComboBox, which acts as follows:
When Click on the ComboBox, the client calling API method and updates the combobox items with the response.
My problem is, when I have 0 results - I want the ComboBox not to open (It has 0 items).
Is there a way to do that?
This is my current code:L
private void Combo_DropDown(object sender, EventArgs e)
{
// Private method which addes items to the combo, and returns false if no itmes were added
if (!AddItemsToComboBox())
{
// This is not working
Combo.DroppedDown = false;
}
}
You can make the DropDownHeight as small as possible (1). For example:
int iniHeight;
private void Form1_Load(object sender, EventArgs e)
{
iniHeight = Combo.DropDownHeight;
}
private void Combo_DropDown(object sender, EventArgs e)
{
Combo.DropDownHeight = (AddItemsToComboBox() ? iniHeight : 1);
}
I have two listboxes: listBox1, listBox2.
If i select the item in first listBox1, item of the same index must be automatically selected in listBox2.
So, If i select item 1 in listbox1 then, item 1 selected automatically in listbox2 and so on.
Not: I found some examples but not work.
private void listBoxControl2_SelectedIndexChanged(object sender, EventArgs e)
{ listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex; }
Edit:
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Here's a sample that you may want to explore more, try to add ListBoxto your form (in this sample 3 listboxes) it should look like the following:
And here's the source that would select the same index whenever you click on an item on it:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeListBoxes();
}
private void InitializeListBoxes()
{
//Populate listboxes
listBox1.Items.Add("Apple");
listBox1.Items.Add("Orange");
listBox1.Items.Add("Mango");
listBox2.Items.Add("Milk");
listBox2.Items.Add("Cheese");
listBox2.Items.Add("Butter");
listBox3.Items.Add("Coffee");
listBox3.Items.Add("Cream");
listBox3.Items.Add("Sugar");
//Subscribe to same events
listBox1.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox2.SelectedIndexChanged += listBox_SelectedIndexChanged;
listBox3.SelectedIndexChanged += listBox_SelectedIndexChanged;
}
void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox listBox = (ListBox)sender;
listBox1.SelectedIndex = listBox.SelectedIndex;
listBox2.SelectedIndex = listBox.SelectedIndex;
listBox3.SelectedIndex = listBox.SelectedIndex;
}
}
What happens is on the InitializeListBoxes you subscribe to the same event which would trigger the SelectedIndexChanged event, and select appropriate item from each of the ListBox.
to solve your problem, you can use a pattern called Observer: https://msdn.microsoft.com/en-us/library/ee850490(v=vs.110).aspx
Basically, you will have to create a notifier method in the listboxes that you want to notify. When you select an item in listBox1, you will call the notifier method of listBox2.
I solved it using the selected index code in This answer in SelectedValueChanged Event.
private void listBoxControl2_SelectedValueChanged(object sender, EventArgs e)
{
listBoxControl5.SelectedIndex = listBoxControl2.SelectedIndex;
}
Fastest and easiest way can be via MouseDown event:
private void lstBoxes_MouseDown(object sender, MouseEventArgs e)
{
ListBox lstBox = (ListBox)sender;
lstBx1.SelectedIndex = lstBox.SelectedIndex;
lstBx2.SelectedIndex = lstBox.SelectedIndex;
lstBx3.SelectedIndex = lstBox.SelectedIndex;
}
i have a form that when i add a item it stores it within a list and adds it to a checklistbox
i have a button that deletes the item from the checklist box, but how would i get it so that when i delete it from the checklistbox it also deletes it within the list its been stored in
here is the code for the delete button
private void btnDelete_Click(object sender, EventArgs e)
{
for (int i = clbSummary.CheckedIndices.Count - 1; i >= 0; --i)
{
clbSummary.Items.RemoveAt(clbSummary.CheckedIndices[i]);
}
}
Why don't you do remove the item from the list within the btnDelete_Click method.
For example:
private void btnDelete_Click(object sender, EventArgs e)
{
for (int i = clbSummary.CheckedIndices.Count - 1; i >= 0; --i)
{
object item = clbSummary.Items[clbSummary.CheckedIndices[i]];
myList = myList.Remove(item);
clbSummary.Items.RemoveAt(clbSummary.CheckedIndices[i]);
}
}
I'm not sure if you can use the [] operator on Items, but this is to give you a general idea.
Set the checklistbox DataSource property to the list where you are storing the items. When you make any changes to the list, your checklistbox will update.