Adding items and storing as variables (array?)

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 3624 Views - Last Post: 16 July 2009 - 08:09 AM Rate Topic: -----

#1 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Adding items and storing as variables (array?)

Post icon  Posted 04 July 2009 - 08:29 AM

I'm having a real tough time with this one.
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

Is This A Good Question/Topic? 0
  • +

Replies To: Adding items and storing as variables (array?)

#2 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 08:34 AM

Take a look at this for adding items to a ListBox:

http://www.java2s.co...mstoListBox.htm
Was This Post Helpful? 1
  • +
  • -

#3 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 08:41 AM

Excellent! I should be able to work out the Add and Remove code from this site.

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

Was This Post Helpful? 0
  • +
  • -

#4 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 08:48 AM

Yes it will add bars automatically, except for the horizontal one, you need to change a property for that.good luck figuring things out!
Was This Post Helpful? 0
  • +
  • -

#5 papuccino1  Icon User is offline

  • His name was Robert Paulson.
  • member icon

Reputation: 63
  • View blog
  • Posts: 1,121
  • Joined: 02-March 08

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 08:53 AM

Just a suggestion:

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.
Was This Post Helpful? 0
  • +
  • -

#6 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 08:59 AM

Right!

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.


View Postpapuccino1, on 4 Jul, 2009 - 07:53 AM, said:

Just a suggestion:

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

Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 08:59 AM

For adding items to a ListBox check out the Items Property of the ListBox (Hint: ListBox.Items.Add).

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 :)
Was This Post Helpful? 0
  • +
  • -

#8 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 12:08 PM

I have my Add and Remove buttons sorted and now I'm trying to create an array to store everything from the listbox.
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
Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Adding items and storing as variables (array?)

Posted 04 July 2009 - 12:40 PM

On the main menu (at the top of the IDE) select Project, then select Add Reference. When the Add Reference dialog opens make sure the .Net tab is selected, scroll down and highlight System.Windows.Forms and click OK. That will add the reference you need.
Was This Post Helpful? 0
  • +
  • -

#10 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: Adding items and storing as variables (array?)

Posted 10 July 2009 - 03:46 PM

right, I'm still struggling to work out how to achieve this.

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

Was This Post Helpful? 0
  • +
  • -

#11 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Adding items and storing as variables (array?)

Posted 10 July 2009 - 04:15 PM

Easiest is to create a class for them. Since I don't know what data you're collecting, i'll give a general example.
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. :D
Was This Post Helpful? 1
  • +
  • -

#12 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: Adding items and storing as variables (array?)

Posted 15 July 2009 - 09:26 AM

After looking at Renagado's excellent post, I implemented this into my code and came up with this:

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
Was This Post Helpful? 0
  • +
  • -

#13 Renagado  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: Adding items and storing as variables (array?)

Posted 15 July 2009 - 09:52 AM

Well done, and you're welcome :D

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!
Was This Post Helpful? 0
  • +
  • -

#14 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Adding items and storing as variables (array?)

Posted 15 July 2009 - 11:10 AM

you should also post your code to where you are adding them to the list. You class looks fine. There is probably a problem in your other code.
Was This Post Helpful? 0
  • +
  • -

#15 Premier2k  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 180
  • Joined: 26-August 08

Re: Adding items and storing as variables (array?)

Posted 15 July 2009 - 12:02 PM

View PostRenagado, on 15 Jul, 2009 - 08:52 AM, said:

Well done, and you're welcome :D

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
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3