Having trouble putting a List<T> in a listbox

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1245 Views - Last Post: 31 May 2013 - 07:55 PM Rate Topic: -----

#1 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 07:15 AM

I'm trying to use a listbox to display the contents of a list of Person objects, the objects only contain one thing at the moment and that is a string called 'name' which should then be used to form the lines in the listbox. I don't get an error, but nothing happens when I hit the button that's supposed to save the object and update the list (AddnewBtnClick method).

Also, I cannot use data source, it's forbidden in the assignment.

Here's the form that handles it:

MainForm class

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace MedProject
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		int index;
		PersonHandler handler;
		public MainForm()
		{
			InitializeComponent();
			InitializeGUI();
			
			handler = new PersonHandler();
		}
		
		private void InitializeGUI()
		{
			relationshipCombo.Items.Add("Unknown");	
			relationshipCombo.Items.Add("Single");		
			relationshipCombo.Items.Add("In a relationship");			
			relationshipCombo.Items.Add("Married");
			relationshipCombo.SelectedIndex = 0;
			
			conditionCombo.Items.Add("Unknown");
			conditionCombo.Items.Add("Stabile");
			conditionCombo.Items.Add("Critical");
			conditionCombo.SelectedIndex = 0;
		}
		
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			Application.Exit(); //The PassForm doesn't close properly without this.
		}
		
		void AddnewBtnClick(object sender, EventArgs e)
		{
			AddModeOn();
			UpdateList();			
		}
		
		void SavenewBtnClick(object sender, EventArgs e)
		{
			
			if (string.IsNullOrEmpty(nameBox.Text))
				MessageBox.Show("Please enter a name.","No Name Entered");				
			else
				AddNewPerson();
		}
		
		private void AddNewPerson()
		{
			for (int i = 0; i < handler.PersonList.Count; i++) 
			{
				if (string.IsNullOrEmpty(handler.PersonList[i].Name)) 
				{
					handler.PersonList.Insert(i,new Person(nameBox.Text));	
					handler.PersonList[i].Name = nameBox.Text;
				}
				
				else
					continue;
			}
		}
		
		private void RemovePerson()
		{
			patientsBox.Items.Remove(nameBox.Text);
			UpdateList();
		}
		
		private void UpdateList()
		{
			patientsBox.Items.Clear();
			for (int i = 0; i < handler.PersonList.Count; i++) 
			{
				if (string.IsNullOrEmpty(handler.PersonList[i].Name)) 
				{
					patientsBox.Items.Insert(i,ListString(i));
				}
				
				else
					continue;
			}					
		}
		
		private string ListString(int incIndex)
		{
			string returnString = string.Format("{0}", handler.PersonList[incIndex].Name);
			return returnString;
		}
		
		private void AddModeOn()
		{
			addNewBtn.Visible = false;
			removePersonBtn.Visible = false;
			saveNewBtn.Visible = true;
			cancelNewBtn.Visible = true;
			patientsBox.Enabled = false;
			saveChangesBtn.Visible = false;
			ClearPersonalFields();
		}
		
		private void AddModeOff()
		{
			addNewBtn.Visible = true;
			removePersonBtn.Visible = true;
			saveNewBtn.Visible = false;
			cancelNewBtn.Visible = false;
			saveChangesBtn.Visible = true;
			patientsBox.Enabled = true;
		}
		
		private void ClearPersonalFields()
		{
			nameBox.Text = null;
			streetBox.Text = null;
			zipBox.Text = null;
			cityBox.Text = null;
			countryBox.Text = null;
			relationshipCombo.SelectedIndex = 0;
			infoBox.Text = null;
		}
		
		void CancelNewBtnClick(object sender, EventArgs e)
		{
			ClearPersonalFields();
			AddModeOff();
		}
		
		void ContactsBtnClick(object sender, EventArgs e)
		{
			if (patientsBox.SelectedIndex == -1) 
			{
				MessageBox.Show("Please select a person in the list.", "No Person Selected");
			}
			
			else
			{
				index = patientsBox.SelectedIndex;
				handler.PersonList[index].ContactManager.Show();
			}
		}
		
		void RemovePersonBtnClick(object sender, EventArgs e)
		{
			RemovePerson();
		}
	}
}


Here's the class that handles the object list.

using System;
using System.Collections.Generic;

namespace MedProject
{
	/// <summary>
	/// Description of PersonHandler.
	/// </summary>
	public class PersonHandler
	{
		List<Person> personList;
		
		public PersonHandler()
		{
			personList = new List<Person>();
		}
		
		public List<Person> PersonList
		{
			get {return personList;}
			
			set {personList = value;}
		}
	}
}


And here's the class containing the object.

using System;
using System.Collections.Generic;

namespace MedProject
{
	/// <summary>
	/// Description of Person.
	/// </summary>
	public class Person
	{
		List<Visit> visits;
		ContactForm contactManager;
		string name;
		
		public Person(string incName)
		{
			name = incName;
			contactManager = new ContactForm();
			visits = new List<Visit>();
		}
		
		public string Name
		{
			get {return name;}
			
			set {name = value;}
		}
		
		public ContactForm ContactManager
		{
			get {return contactManager;}
			
			set {contactManager = value;}
		}
	}
}



I appreciate any help. If you see anything odd, please point it out. I'm happy to learn.

Is This A Good Question/Topic? 1
  • +

Replies To: Having trouble putting a List<T> in a listbox

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,276
  • Joined: 12-June 08

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 07:42 AM

Where in there do you actually attempt to set the datasource for the listbox to your collection?
Was This Post Helpful? 0
  • +
  • -

#3 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 07:49 AM

View Postmodi123_1, on 31 May 2013 - 07:42 AM, said:

Where in there do you actually attempt to set the datasource for the listbox to your collection?


I can't use a data source, it's forbidden in the assignment. I use the method called UpdateList to add objects from the PersonList to the listbox. It's basically a for-loop that goes through the PersonList.

		private void UpdateList()
		{
			patientsBox.Items.Clear();
			for (int i = 0; i < handler.PersonList.Count; i++) 
			{
				if (string.IsNullOrEmpty(handler.PersonList[i].Name)) 
				{
					patientsBox.Items.Insert(i,ListString(i));
				}
				
				else
					continue;
			}					
		}

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,276
  • Joined: 12-June 08

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 07:55 AM

Quote

I can't use a data source, it's forbidden in the assignment.

Wait - do you mean the use of a datasource object, or the use of the listbox's datasource property?
Was This Post Helpful? 0
  • +
  • -

#5 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 08:17 AM

View Postmodi123_1, on 31 May 2013 - 07:55 AM, said:

Quote

I can't use a data source, it's forbidden in the assignment.

Wait - do you mean the use of a datasource object, or the use of the listbox's datasource property?


Well, the data source property. The literal data source is the PersonList.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,276
  • Joined: 12-June 08

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 08:25 AM

   patientsBox.Items.Insert(i,ListString(i));

Any specific reason why you are using 'insert' and not just the 'add'?


090	                else
091	                    continue;


I also do not see a specific reason for this 'else'.

Have you put a breakpoint in your 'updatelist' and debugged through the lines to make sure it is operating correctly?
Was This Post Helpful? 0
  • +
  • -

#7 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 09:02 AM

View Postmodi123_1, on 31 May 2013 - 08:25 AM, said:

   patientsBox.Items.Insert(i,ListString(i));

Any specific reason why you are using 'insert' and not just the 'add'?


I figured it would keep the indexes matched up and keep things easier later on.

Quote

090	                else
091	                    continue;


I also do not see a specific reason for this 'else'.

Have you put a breakpoint in your 'updatelist' and debugged through the lines to make sure it is operating correctly?


I just did, it breaks if I put it on UpdateList's Clear() and the for, but not on any other line in that method. I put breakpoints on AddNewPerson, where it breaks on the for but not on any other line. Am I using the IsNullOrEmpty wrong?

I also removed the unnecessary 'elses'.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,276
  • Joined: 12-June 08

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 09:06 AM

Well.. what's the error it is giving you when the line(s) generate an exception?
Was This Post Helpful? 0
  • +
  • -

#9 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 09:08 AM

View Postmodi123_1, on 31 May 2013 - 09:06 AM, said:

Well.. what's the error it is giving you when the line(s) generate an exception?


It doesn't generate an error or throw an exception, it just does nothing. It doesn't have any effect at all. It seems to just ignore the ifs in AddNewPerson and UpdateList.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,276
  • Joined: 12-June 08

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 09:21 AM

06	        if (string.IsNullOrEmpty(handler.PersonList[i].Name)) 


What's the return value for the 'isunullortempty'?

Quote

Return Value
Type: System.Boolean
true if the value parameter is null or an empty string (""); otherwise, false.

http://msdn.microsof...ullorempty.aspx

Does that seem to jive with what you are trying to do in the 'if' statement?
Was This Post Helpful? 0
  • +
  • -

#11 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 09:57 AM

View Postmodi123_1, on 31 May 2013 - 09:21 AM, said:

06	        if (string.IsNullOrEmpty(handler.PersonList[i].Name)) 


What's the return value for the 'isunullortempty'?

Quote

Return Value
Type: System.Boolean
true if the value parameter is null or an empty string (""); otherwise, false.

http://msdn.microsof...ullorempty.aspx

Does that seem to jive with what you are trying to do in the 'if' statement?


Well, yeah. I want it to see if the list placement is used by something, then place the Person to-be-added if the position is free. It's listed by name, so it should place the Person if the Name property is empty.

Are you saying I should use something else?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,276
  • Joined: 12-June 08

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 10:29 AM

06	        if (string.IsNullOrEmpty(handler.PersonList[i].Name))
07	        {
08	            patientsBox.Items.Insert(i,ListString(i));
09	        }

Read that if statement out loud.

If handler.PersonList[i].Name *IS* null or *IS* empty then do this: "patientsBox.Items.Insert(i,ListString(i));".

Re read that - if 'NAME' is nothing or it is null then insert the name? Does that sound right?
Was This Post Helpful? 0
  • +
  • -

#13 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 10:44 AM

I see it now, changed it.

		private void UpdateList()
		{
			patientsBox.Items.Clear();
			for (int i = 0; i < handler.PersonList.Count; i++) 
			{
				if (string.IsNullOrEmpty(handler.PersonList[i].Name)) 
					continue;
				else
					patientsBox.Items.Insert(i,ListString(i));
			}					
		}


Thanks a lot for the patience. However, there seems to be some trouble with the AddNewPerson method too, I put a breakpoint on the if and it isn't breaking.

		private void AddNewPerson()
		{
			for (int i = 0; i < handler.PersonList.Count; i++) //Goes through the PersonList...
			{
				if (handler.PersonList[i].Name == "") //...until it finds an empty position...
				{
					handler.PersonList.Insert(i,new Person(nameBox.Text));	//...then inserts a Person with the current name. 
				}
			}
		}

Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15790
  • View blog
  • Posts: 63,276
  • Joined: 12-June 08

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 10:47 AM

Quote

I put a breakpoint on the if and it isn't breaking.

What do you mean? If you are running your app in 'debug' mode - and that breakpoint is in the execution path - the compiler *will* stop on it and you can hit the 'next step' command to go to the next line in the execution.


Why are you looking for empty name values?
Was This Post Helpful? 0
  • +
  • -

#15 breakdown   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 11-April 13

Re: Having trouble putting a List<T> in a listbox

Posted 31 May 2013 - 11:07 AM

View Postmodi123_1, on 31 May 2013 - 10:47 AM, said:

Quote

I put a breakpoint on the if and it isn't breaking.

What do you mean? If you are running your app in 'debug' mode - and that breakpoint is in the execution path - the compiler *will* stop on it and you can hit the 'next step' command to go to the next line in the execution.


Why are you looking for empty name values?


I figured out why it wasn't breaking. I thought List<T>.count would be a default max number of the entire list range and not the actual amount of items in the list. Now it keeps changing the same line in the listbox but I'll try to fix it.

I'm looking for empty name values so that I don't overwrite a value that's already there. If I didn't, it would either overwrite or not have the indexes matched.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2