there is a listView,label which shows the current count of a list called Name, and there is a event that raise when list count goes beyond 5. but when ever i enter the names in textbox (it goes to the list) it shows the count but the list view is not getting them properly.EX- when i enter the first name label shows the count as 1 and list view shows the name i entered in textbox and WHEN I ENTERED THE SECOND NAME label shows the list counter correctly but list view adds not only the second item i entered but also the first i have enetered before. then there is three items in list view. here is my code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> emps = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
listMake lm = new listMake();
lm.ListItemAdded += new listMake.listMethods(lm_ListItemAdded);
lm.adding(emps, textBox1,listView1);
label1.Text = emps.Count.ToString();
}
void lm_ListItemAdded(List<string> names)
{
MessageBox.Show("it enough i think its more than 5");
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
class listMake
{
public delegate void listMethods(List<string> names);
public event listMethods ListItemAdded;
public List<string> Name = new List<string>();
public void adding(List<string>Name,TextBox t1,ListView l1)
{
try
{
if (t1.Text != "")
{
Name.Add(t1.Text);
for (int i = 0; i < Name.Count; i++)
{
l1.Items.Add(Name[i]);
}
if (Name.Count > 5)
ListItemAdded(Name);
}
}
catch (Exception er) { MessageBox.Show(er.StackTrace); ;}
}
}
Problem : You are not clearinig the ListView Items each time you add items from the list.
Solution 1 : you need to clear the items from listview before adding into it.
if (t1.Text != "")
{
Name.Add(t1.Text);
l1.Items.Clear(); //add this statement
for (int i = 0; i < Name.Count; i++)
{
l1.Items.Add(Name[i]);
}
if (Name.Count > 5)
ListItemAdded(Name);
}
Solution 2: You can add only the item enetered in TextBbox instead of adding all items from begining.so you needto remove the for-loop here.
if (t1.Text != "")
{
Name.Add(t1.Text);
l1.Items.Add(t1.Text);
if (Name.Count > 5)
ListItemAdded(Name);
}
Related
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.
I am really confused on thinking which control to use for my purpose.
I am having list of items say item1 to item10. User can select 4 or 5 items in any order.
Now user selected items has to be separated in same order.
For example, if the user selected the items in following order, item4, item8, item3 and item2.
I want it in the same order. item4,item8,item3,item2.
How do I achieve this in winforms control?
It is not a very nice solution but I wrote it as you asked.
Set the SelectionMode of your ListBox to MultiExtended or MultiSimple as you need.
Then write this code in SelectedIndexChanged event of your ListBox:
List<string> orderedSelection = new List<string>();
bool flag = true;
private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
if (flag)
{
flag = false;
var list1 = listBox3.SelectedItems.Cast<string>().ToList();
if (listBox3.SelectedItems.Count > orderedSelection.Count)
{
orderedSelection.Add(list1.Except(orderedSelection).First());
}
else if (listBox3.SelectedItems.Count < orderedSelection.Count)
{
orderedSelection.Remove(orderedSelection.Except(list1).First());
}
var list2 = listBox3.Items.Cast<string>().Except(list1).ToList();
listBox3.Items.Clear();
for (int i = 0; i < list1.Count; i++)
{
listBox3.Items.Add(list1[i]);
listBox3.SelectedIndex = i;
}
foreach (string s in list2)
{
listBox3.Items.Add(s);
}
flag = true;
}
}
When user selects an item, It comes to first of the list and the rest of the items comes next.
Also, there is an alternative way. You can use a CheckedListBox with two extra button for moving selected items up and down. So user can change the order of selected items.
This solution uses CheckListBox's ItemCheck event along with a private List to keep track of the click items order.
protected List<string> clickOrderList = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
// Populate the checked ListBox
this.checkedListBox1.Items.Add("Row1");
this.checkedListBox1.Items.Add("Row2");
this.checkedListBox1.Items.Add("Row3");
this.checkedListBox1.Items.Add("Row4");
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (sender != null && e != null)
{
// Get the checkListBox selected time and it's CheckState
CheckedListBox checkListBox = (CheckedListBox)sender;
string selectedItem = checkListBox.SelectedItem.ToString();
// If curent value was checked, then remove from list
if (e.CurrentValue == CheckState.Checked &&
clickOrderList.Contains(selectedItem))
{
clickOrderList.Remove(selectedItem);
}
// else if new value is checked, then add to list
else if (e.NewValue == CheckState.Checked &&
!clickOrderList.Contains(selectedItem))
{
clickOrderList.Insert(0, selectedItem);
}
}
}
private void ShowClickOrderButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string s in clickOrderList)
{
sb.AppendLine(s);
}
MessageBox.Show(sb.ToString());
}
Problem solved.
The original "private void buttonSave_Click" was changed to:
private void buttonSave_Click(object sender, EventArgs e)
{
if (MusicCollection.FormMain.PublicVars.AlbumList.Count != 100)
{
MusicCollection.FormMain.PublicVars.AlbumList.Add(new Album(NameTextBox.Text));
MessageBox.Show("New Album added: " + NameTextBox.Text);
formMain.ListAlbums(formMain.AlbumsListBox.Items);
this.Close();
}
else
{
MessageBox.Show("No room for new album.");
this.Close();
}
}
Original Post:
I'm new to using C#, so appologies for any seemly obvious mistakes or terrible coding.
I'm trying to create a new Album object (that gets its Name from NameTextBox.Text on Form FormAlbumAC) and add it to List AlbumList when the user clicks the save button on FormAlbumAC. Then I want to list all of AlbumList in a ListBox on Form FormMain.
When I run the program and click the save button, I'm getting the error "ArgumentOutOfRangeException was unhandled, Index was out of range" at the line:
if (MusicCollection.FormMain.PublicVars.AlbumList[i] == null)
// line 8 on my excerpt from Form FormAblumAC
I'm not sure what I'm doing wrong. Any help would be much appreciated, thank you.
Form FormMain:
public const int MAX_ALBUMS = 100;
public int totalAlbums = 0;
public FormMain()
{
InitializeComponent();
}
public static class PublicVars
{
public static List<Album> AlbumList { get; set; }
static PublicVars()
{
AlbumList = new List<Album>(MAX_ALBUMS);
}
}
public ListBox AlbumListBox
{
get
{
return AlbumListBox;
}
}
public void ListAlbums(IList list)
{
list.Clear();
foreach (var album in PublicVars.AlbumList)
{
if (album == null)
continue;
list.Add(album.Name);
}
}
Form FormAlbumAC:
private FormMain formMain;
private void buttonSave_Click(object sender, EventArgs e)
{
int index = -1;
for (int i = 0; i < MusicCollection.FormMain.MAX_ALBUMS; ++i)
{
if (MusicCollection.FormMain.PublicVars.AlbumList[i] == null)
{
index = i;
break;
}
}
if (index != -1)
{
MusicCollection.FormMain.PublicVars.AlbumList[index] = new Album(NameTextBox.Text);
++formMain.totalAlbums;
MessageBox.Show("New Album added: " + NameTextBox.Text);
formMain.ListAlbums(formMain.AlbumsListBox.Items);
this.Close();
}
else
{
MessageBox.Show("No room for new album.");
this.Close();
}
}
Your problem (from your comments) is that your for loop's condition is incorrect. Your for loop is this:
for (int i = 0; i < MusicCollection.FormMain.MAX_ALBUMS; ++i)
There is one problem and one potential problem here. First, when this code is actually run, it's really running:
for (int i = 0; i < 100; ++i)
because MusicCollection.FormMain.MAX_ALBUMS is declared as 100. This causes an error when the length of MusicCollection.FormMain.PublicVars.AlbumList is less than 100, because you're trying to grab an index that doesn't exist.
Instead, you need to iterate from i=0 to the length of ....PublicVars.AlbumList-1, or, preferably, for(int i = 0; i < ....PublicVars.AlbumList.Count; i++).
The second potential problem is that you are potentially skipping index 0. Arrays start at index zero and continue to index length-1. As such, you probably want i++, not ++i. Depends on your implementation, though.
I make a listbox and it works perfectly, I also made a search function, but I want to hide the items in the listbox and show them only when searched through index.
here is my code for adding items
private void Savebtn_Click(object sender, EventArgs e)
{
addTolist(gatherItem());
refreshView();
}
private void addTolist(Person p)
{
Person.listperson.Add(p);
}
private void refreshView()
{
listBox1.Items.Add(getItem());
}
private String getItem()
{
String result = null;
foreach (Person p in Person.listperson)
{
result = p.lastname;
}
return result;
}
and this is my code for search
private void button1_Click(object sender, EventArgs e)
{
int index = listBox1.FindString(textBox6.Text);
if (0 <= index)
{
listBox1.SelectedIndex = index;
}
}
Is there a way for this? thanks :)
There is no easy way to do this with winforms. It a lot easier with WPF. With winforms you will have to remove the items. You could technically use databinding, but its not recommended at all, because listbox is supposed to used directly from your code according the MSDN documentation.
On second thought, you could overdraw the listbox and set the item you want to hide to transparent or something, buts its not easy. You may want to consider storing all your values in an array and then loop through array and only adding the elements you want. Or switch to WPF.
Hope this helps.
You can cover a dummy ListBox on your origin one, and set origin one invisible.
Then you can archive the same effect by writing codes like
class DummyItem {
public string text;
public int index;
public override string ToString() {
return text;
}
}
public void build_dummy() {
listbox_dummy.Items.Clear();
for (int i = 0; i < listbox_origin.Items.Count; i++) {
// replace with your own judgement
if (! should_hide(listbox_origin.Items[i])) {
DummyItem item = new DummyItem();
item.text = listbox_origin.Items[i].ToString();
item.index = i;
listbox_dummy.Items.Add(item);
}
}
}
private void listbox_dummy_SelectedIndexChanged(object sender, EventArgs e) {
var item = (DummyItem) listbox_dummy.SelectedItem;
var index = item.index;
listbox_origin.SelectedIndex = index;
}
The easiest way to do this, is to create a List<string> item that will store removed items (or items that do not match the search-phrase given in textbox1), an int variable that will store how many characters there were the last time the TextChanged event was triggered, and then create a function for your listbox element that activates when the phrase in a textbox is updated:
private void textbox1_TextChanged(object sender, EventArgs e) {}
in this function, there are two things that could happen:
if a character was added to textbox1:
you iterate over the elements in your listbox.
remove the ones that do not contain the phrase in textbox1
if a character was removed from textbox1:
look through the List<string> variable you made to store the values you removed from your listbox
add back each item that fits the text in textbox1 (check for duplicates though)
Here's the code I wrote to use as reference:
private List<string> temporarilyRemovedSongs = new List<string>();
private int previousTextLength = 0;
private void filter_songs_textbox_TextChanged(object sender, EventArgs e)
{
List<string> toRemove = new List<string>();
string text = Tools.Strip(this.filter_songs_textbox.Text.ToLower());
if (text.Length > this.previousTextLength)
{
// a char was added, remove songs that do not start with the search-phrase
foreach (string song in this.songs_list.Items)
{
if (!song.ToLower().Contains(text))
{
this.temporarilyRemovedSongs.Add(song);
toRemove.Add(song);
}
}
foreach (string song in toRemove)
{
this.songs_list.Items.Remove(song);
}
}
else
{
// a char was removed; look through removed songs to add back
foreach (string song in this.temporarilyRemovedSongs)
{
if (song.Contains(text) && !this.songs_list.Items.Contains(song))
this.songs_list.Items.Add(song);
}
}
previousTextLength = text.Length;
}
I have two listboxes named listBox1 and listBox2 with 4 items (strings) in both listboxes. I can select multiple items from both listboxes. I have also two buttons.
On clicking button1, I have to move multiple selected items from listBox1 to listBox2. Similarly, on clicking button2, I have to move multiple selected items from listBox2 to listBox1.
How can it be done?
private void MoveListBoxItems(ListBox source, ListBox destination)
{
ListBox.SelectedObjectCollection sourceItems = source.SelectedItems;
foreach (var item in sourceItems)
{
destination.Items.Add(item);
}
while (source.SelectedItems.Count > 0)
{
source.Items.Remove(source.SelectedItems[0]);
}
}
Use:
On the click event of your move from 1 to 2 button:
MoveListBoxItems(listBox1, listBox2);
To move them back:
MoveListBoxItems(listBox2, listBox1);
A ListBox has a SelectedItems property you can use to copy the items in the click event handler of the button. Like this:
foreach(var item in listBox1.SelectedItems)
{
listBox2.Items.Add(item);
}
private void Move(ListControl source, ListControl destination)
{
List<ListItem> remove = new List<ListItem>();
foreach(var item in source.Items)
{
if(item.Selected == false) continue;
destination.Items.Add(item);
remove.Add(item);
}
foreach(var item in remove)
{
source.Items.Remove(item);
}
}
then you can call it like this
Move(listbox1, listbox2);
//or
Move(listbox2, listbox1);
According to this question How to remove multiple selected items in ListBox?
private void button1_Click(object sender, EventArgs e)
{
for(int x = listBox1.SelectedIndices.Count - 1; x>= 0; x--)
{
int idx = listBox1.SelectedIndices[x];
listBox2.Items.Add(listBox1.Items[idx]);
listBox1.Items.RemoveAt(idx);
}
}
You can do like this.
private void button1_Click(object sender, EventArgs e)
{
foreach (var item in listBox1.SelectedItems)
{
listBox2.Items.Add(item);
}
for (int s = 0; s < listBox1.Items.Count; s++)
{
for (int t = 0; t < listBox2.Items.Count; t++)
{
if (listBox1.Items[s].ToString().Equals(listBox2.Items[t].ToString()))
{
listBox1.Items.RemoveAt(s);
}
}
}
}
private void move(ListBox source, ListBox destination) {
for (int i = 0; i <= source.Items.Count-1; i++)
{
destination.Items.Add(source.Items[i]);
}
source.Items.Clear();
}
private void Btn_Right_Click(object sender, EventArgs e)
{
while(ListBox_Left.SelectedItems.Count!=0)
{
ListBox_Right.Items.Add(ListBox_Left.SelectedItem);
ListBox_Left.Items.Remove(ListBox_Left.SelectedItem);
}
}
private void Btn_Left_Click(object sender, EventArgs e)
{
while (ListBox_Right.SelectedItems.Count != 0)
{
ListBox_Left.Items.Add(ListBox_Right.SelectedItem);
ListBox_Right.Items.Remove(ListBox_Right.SelectedItem);
}
}