31 Replies - 2857 Views - Last Post: 16 July 2009 - 08:09 AM
#1
Adding items and storing as variables (array?)
Posted 04 July 2009 - 08:29 AM
I have a text box, 3 buttons(Add, Remove and Next) and a list box.
First problem, I want the user to enter a name into the text box and when they click Add it adds it into a list box. When the user clicks on one of the names in the list box and clicks remove it removes it from the list (and reorders the list alphabetically). I have no idea how to go about doing this. Can anyone point me in the right direction of how to achieve this? Please, please, please don't just write the code and paste it, I need to figure this stuff out. So if you have any links that can explain this to me then that would be great.
Second problem (the harder one). Once the user is satisfied that the list is complete, they can click Next, at this point I need all items in the list to be recorded either in separate variables or an array. So that later I can populate a combo box for them to select multiple names. Again, this I have even less knowledge on how to achieve. So any links or examples would be of great benefit.
Thanks again all,
Premier2k
Replies To: Adding items and storing as variables (array?)
#2
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 08:34 AM
#3
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 08:41 AM
Just a thought but will the list box automatically add scroll bars when the items go off the screen? As I'm expecting anything up to 100+ items in the list box?
Prem.
EDIT: Yes it does add scrollbars if anyone's interested
This post has been edited by Premier2k: 04 July 2009 - 08:46 AM
#4
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 08:48 AM
#5
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 08:53 AM
Take my code for instance:
MainListView.BeginUpdate();
foreach (FileInfo X in FileArray)
{
ListItem = new ListViewItem();
ListItem.Text = X.Name;
ListItem.ImageIndex = 1;
ListItem.Tag = X.FullName;
ListSubItem = new ListViewItem.ListViewSubItem();
ListSubItem.Text = X.Length.ToString();
ListItem.SubItems.Add(ListSubItem);
MainListView.Items.Add(ListItem);
}
MainListView.EndUpdate();
MainListView is just a name I made up for a ListView. I call the .BeginUpdate() and .EndUpdate() methods so it doesn't "paint" the ListView until I say all the items necessary are loaded.
#6
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 08:59 AM
I have the code for the Add button working, shown below for anyone else trying to work this out.
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if (tbName.Text == "")
{
MessageBox.Show("Please enter a territory name", "Error");
}
lbnames.Items.Add(tbName.Text);
tbName.Text = "";
tbName.Focus();
}
I now have this code for the Remove button, but it doesn't seem to remove anything.. What's wrong?
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
object itemRemove = lbnames.SelectedItems;
lbnames.SelectedItems.Remove(itemRemove);
}
I'm missing something but can't work out what.....
Prem.
papuccino1, on 4 Jul, 2009 - 07:53 AM, said:
Take my code for instance:
MainListView.BeginUpdate();
foreach (FileInfo X in FileArray)
{
ListItem = new ListViewItem();
ListItem.Text = X.Name;
ListItem.ImageIndex = 1;
ListItem.Tag = X.FullName;
ListSubItem = new ListViewItem.ListViewSubItem();
ListSubItem.Text = X.Length.ToString();
ListItem.SubItems.Add(ListSubItem);
MainListView.Items.Add(ListItem);
}
MainListView.EndUpdate();
MainListView is just a name I made up for a ListView. I call the .BeginUpdate() and .EndUpdate() methods so it doesn't "paint" the ListView until I say all the items necessary are loaded.
So the list box won't fill till you've explicity said that your done entering names? If that's the case, that won't work for me. I need the users to know what they've entered.
Prem.
This post has been edited by Premier2k: 04 July 2009 - 08:59 AM
#7
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 08:59 AM
As for sorting the items, since there is no Sort method for a ListBox one way would be to add all the items to an ArrayList, then sort the ArrayList, then re add the items to the ListBox. For this I'll show an example. With this method you pass it the ListBox you want to sort
/// <summary>
/// method for sorting the items in a ListBox
/// </summary>
/// <param name="list">the ListBox we wish to sort</param>
public void SortListBoxItems(ListBox list)
{
//clear the ListBox
list.Items.Clear();
//create an ArrayList
ArrayList sorted = new ArrayList();
//now we loop through the items in the list
//and add to our SortedList (this will sort them
foreach(ListItem item in list.Items)
{
sorted.Add(list.Text);
}
//now sort the items
sorted.Sort();
//now add to ListBox
foreach (string s in sorted)
{
ListItem li = new ListItem(s);
list.Items.Add(li);
}
}
For removing the items check this out.
Now for issue number 2, when you click the next button you can loop through the items in the ListBox and add to the DropDownList. Here's an example
public void LoadComboBoxFromListBox(ListBox list, DropDownList ddl)
{
foreach (ListItem li in list.Items)
ddl.Items.Add(li.Text);
}
Then just call LoadComboBoxFromListBox in the button's click event and pass it the name of the ListBox and the DropDownList you wish to populate.
Hope this helps
#8
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 12:08 PM
So far I have this:
In my form:
else
{
data newdb = new data();
newdb.CreateArrayList(lbnames);
}
and in my CreateArrayList method I have this:
public Array CreateArrayList(ListBox lbList)
{
for (int x = 0; x < lbList.Items.Count; x++)
{
lbList.SetSelected(x, true);
}
It keeps telling me that SetSelected isn't recognised, I assume I'm missing a using statement. But which one. MSDN tells me I need using System.Windows.Forms but it doesn't exist on the intellisense.
Also, once it has the number of items, how do I return that number outside of my FOR loop. Correct me if I'm wrong but doesn't x exist only within the loop?
Any ideas?
Premier2k
#9
Re: Adding items and storing as variables (array?)
Posted 04 July 2009 - 12:40 PM
#10
Re: Adding items and storing as variables (array?)
Posted 10 July 2009 - 03:46 PM
I have my form which has various controls on it, the text boxes are taking different inputs, 1 takes a name, 4 take integers and there are 3 checkboxes. I also have an add button and a next button.
Now I need a way of storing the information that is entered. For example, the user enters the name and the integers and sets the checkboxes, when the user clicks add, the name appears in a listbox on the same form and the fields are reset. now I need a way of storing the information that the user has set but they need to be stored individually as each setting will be called at different times on different forms. So how would I go about storing this information? And more importantly how do I go about retrieving that particular setting.
For example, on the next form the listbox is populated with the names but later on I need to know if a user selected a checkbox for that particular name and what integers he set for that particular name.
Still with me? I hope so. lol
How do I do this? I have no idea where to begin.
I have this little bit code which takes the names in the listbox and shows them each in a messagebox, but so far this is it! And I have no idea how to call this to retrieve later on.
class data
{
public void CreateCollection(ListBox list)
{
List<String> l = new List<String>();
int n;
for (n = 0; n < list.Items.Count; n++)
l.Add((String)list.Items.GetItemAt(n));
foreach (String s in l)
//NEED TO REPLACE MESSAGEBOX WITH CODE TO POPULATE A LISTBOX ON A DIFFERENT FORM OR TO CALL THIS AND GET THE INFORMATION OUT
MessageBox.Show(s);
}
}
Can anyone help with this?
Premier2k
This post has been edited by Premier2k: 10 July 2009 - 03:46 PM
#11
Re: Adding items and storing as variables (array?)
Posted 10 July 2009 - 04:15 PM
class Data
{
string name;
int a, b;
bool check1, check2;
//this is the constructor
public Data(string name, int a, int b, bool check1, bool check2)
{
this.name = name;//this passes the arguments to instance variables
this.a = a;//instance variables will be created once for every instance
this.b = b;//of the class you make
this.check1 = check1;
this.check2 = check2;
}
}
Now you have kind of your own datatype. You can create a new instance of this by using:
Data New_Data = new Data("New Data 1", 5, 10, true, false);
and do that over and over again.
You can even make arrays and lists using your own data type
Data[] List = new Data[5];
List[1]= new Data("New Data 1", 5, 10, true, false);
Well....maybe I got a bit carried away here with my examples, and I hope it makes sense,but this is what object oriented programming is about. So if you're struggling with the concepts using here, take the time to study OOP basics, and it will pay off.
#12
Re: Adding items and storing as variables (array?)
Posted 15 July 2009 - 09:26 AM
namespace datahandling
{
class data
{
string tname;
int sx, sy, lx, ly;
bool cbS, cbK, cbN;
public data(string tname, int sx, int sy, int lx, int ly, bool cbS, bool cbK, bool cbN)
{
this.tname = tname;
this.sx = sx;
this.sy = sy;
this.lx = lx;
this.ly = ly;
this.cbS = cbS;
this.cbK = cbK;
this.cbN = cbN;
}
}
This seemed to work ok, but when I was testing I noticed that after I pressed ADD and it ran this code, if I entered more information and then pressed ADD again it overwrote the previous stuff I had stored. Obviously this is not good as I need to be able to store multiple "versions" of this and be able to call anyone of them at any point. So how do I achieve this? I've spent the last few days going through the internet and I think I've discovered what I need to do.
I think (please correct me if I'm wrong) I need to create either a HASHTABLE or a collection using ListT??
Can someone explain to me how I go about doing this or point me in the direction of a good site to learn this from?
I've had a look at the MSDN site but I never understand the way Microsoft show how to do something.
Thanks everyone, you've all been brilliant so far and I'm learning new stuff as I do this!
Premier2k
#13
Re: Adding items and storing as variables (array?)
Posted 15 July 2009 - 09:52 AM
List<t> would definately work, however there are multiple options.
Try this, it should work:
List<data> lijst = new List<data>();
lijst.Add(new data("one", 1, 1, 1, 1, true, true, true));
lijst.Add(new data("two", 2, 2, 2, 2, false, false, false));
Good luck!
#14
Re: Adding items and storing as variables (array?)
Posted 15 July 2009 - 11:10 AM
#15
Re: Adding items and storing as variables (array?)
Posted 15 July 2009 - 12:02 PM
Renagado, on 15 Jul, 2009 - 08:52 AM, said:
List<t> would definately work, however there are multiple options.
Try this, it should work:
List<data> lijst = new List<data>();
lijst.Add(new data("one", 1, 1, 1, 1, true, true, true));
lijst.Add(new data("two", 2, 2, 2, 2, false, false, false));
Good luck!
I've followed that code and put it in and it seems to work ok, so thanks for that! (needs further testing)
But now another problem, if the user selects one of the names in the listbox and clicks remove I need to be able to remove that from the data list. I've had a play around and think I'm close, I have this:
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
lbnames.Items.Remove(lbnames.SelectedItem);
List<data> lijst = new List<data>();
lijst.RemoveAll(Predicate <lbnames.SelectedItem>());
}
But I get an error: System.ArgumentNullException, now if I'm reading that right, I'm going wrong somewhere??? lol
So how do I find the entry the user has entered in the data array I've created and remove everything associated with that name?
Sorry for all the questions but I am learning!!
Premier2k
|
|

New Topic/Question
Reply



MultiQuote



|