Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,527 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,827 people online right now. Registration is fast and FREE... Join Now!




Adding items to a listbox from a Class

 
Reply to this topicStart new topic

Adding items to a listbox from a Class, Accessing a form control from a void in a Class

Junije
13 Aug, 2008 - 01:39 AM
Post #1

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 13

The problem I have is accessing a form control from a void in a class, I just have one form and a class in my project, the control on the Form1 is a listbox, and I want to add an item to that listbox.
The annoying part is that the code for adding the item lies in a Class, and the class doesn't recognize the Form1 in which the listbox is.
I'm new to C# and I'm frustrated with such small things holding me back. I just know the VB 6 way does not work in C#. Can someone help me out?
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 04:34 AM
Post #2

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
1. click on the listbox, and set it's "Modifiers" property to "Public".
2. In your class method, accept a parameter for the form.
3. add items to the list.

-- Windows Form --
csharp

namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Processing processingClass = new Processing();
processingClass.LoadListBox(this);
}
}
}


-- Class --
csharp

namespace WindowsApplication2
{
public class Processing
{
public void LoadListBox(Form1 f1)
{
f1.listBox1.Items.Add("Testing");
f1.listBox1.Items.Add("Testing1");
f1.listBox1.Items.Add("Testing2");
}
}
}

User is offlineProfile CardPM
+Quote Post

zakary
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 05:08 AM
Post #3

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 405



Thanked: 6 times
Dream Kudos: 175
My Contributions
eclipsed4utoo code could work if you never change the name of your listbox, and you are looking to have a button click to load your listbox. From reading the original post, here is what I came up with:

Note: you will need to make sure you have the namespace correct and remember void with out public in front will always be private.

here is the class

csharp


using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication1
{
class Class1
{
private System.Collections.ArrayList list;
public Class1()
{
list = new System.Collections.ArrayList();

}

public System.Collections.ArrayList CreateCollectionOfItems()
{
list.Add("John");
list.Add("James");
list.Add("Steve");
list.Add("Sarah");

return list;
}
}
}

[\code]

here is the Form

[code = csharp]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private Class1 class1 = new Class1();
public Form1()
{
InitializeComponent();
LoadListBox();
}

public void LoadListBox()
{
System.Collections.ArrayList list = class1.CreateCollectionOfItems();

foreach(string item in list)
{
this.listBox1.Items.Add(item);
}
}
}
}



This post has been edited by PsychoCoder: 13 Aug, 2008 - 08:21 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 06:07 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
If you want to manipulate the control directly like the first poster offered, you'd do better to only pass the control. You don't want to be locked into a particular form or naming convention. So:

csharp

class DataLoader {
public void LoadFruit(ListBox lb) {
lb.Items.Add("Apple");
lb.Items.Add("Orange");
lb.Items.Add("Dorian");
}
}

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
new DataLoader().LoadFruit(this.listBox1);
}
}


If you wish to use the class like a data source as proposed by the second poster... I don't actually see an advantage to that approach over the above one. However, there is another I would recommend: the DataSource.

You will usually bind a list box to a DataSource. This eliminates a lot of code, and offers a number of extra benefits.

e.g.
csharp

class MyDataSource : List<string> {
public MyDataSource() {
this.Add("Apple");
this.Add("Orange");
this.Add("Dorian");
}
}

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.listBox1.DataSource = new MyDataSource();
}
}


Now, say you want to manipulate your data source, you need only rebind:

csharp

private void button1_Click(object sender, EventArgs e) {
((MyDataSource)this.listBox1.DataSource).Add("Kiwi");
((CurrencyManager)listBox1.BindingContext[listBox1.DataSource]).Refresh();
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

zakary
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 06:42 AM
Post #5

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 405



Thanked: 6 times
Dream Kudos: 175
My Contributions
QUOTE(baavgai @ 13 Aug, 2008 - 10:07 AM) *

If you wish to use the class like a data source as proposed by the second poster... I don't actually see an advantage to that approach over the above one. However, there is another I would recommend: the DataSource.


by using a DataSource or collection you do not limit yourself to a Form or Listbox. If you have to change your listbox to a CheckListBox or your form to a UserControl you will have to change more code to get this application to work.
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 06:42 AM
Post #6

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
QUOTE(zakary @ 13 Aug, 2008 - 09:08 AM) *

eclipsed4utoo code could work if you never change the name of your listbox, and you are looking to have a button click to load your listbox. From reading the original post, here is what I came up with:


I am sorry, but I don't know many developers who change the name of their controls everyday. oh, and if you use the Designer to change the name of the control, it will change the name in all classes...not just the form's class. So changing the name of the control would have no effect on my code.

and a button click isn't required. you could put that code in any event. it's called an EXAMPLE. For reading the original post, there is nothing to determine WHEN he is loading the listbox.
User is offlineProfile CardPM
+Quote Post

zakary
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 06:50 AM
Post #7

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 405



Thanked: 6 times
Dream Kudos: 175
My Contributions
QUOTE(eclipsed4utoo @ 13 Aug, 2008 - 10:42 AM) *

QUOTE(zakary @ 13 Aug, 2008 - 09:08 AM) *

eclipsed4utoo code could work if you never change the name of your listbox, and you are looking to have a button click to load your listbox. From reading the original post, here is what I came up with:


I am sorry, but I don't know many developers who change the name of their controls everyday. oh, and if you use the Designer to change the name of the control, it will change the name in all classes...not just the form's class. So changing the name of the control would have no effect on my code.

and a button click isn't required. you could put that code in any event. it's called an EXAMPLE. For reading the original post, there is nothing to determine WHEN he is loading the listbox.


I am not saying your way is wrong, and you most have some great customers. I develop Government Application and they change things all the time. I know the Designer will change the names of the control but only local to that control not in other classes, so f1.listBox1.Items.Add("Testing"); would have to be changed by the developer to reflect the new name.

I guess its really just a matter of opinion. I would not do it that way. I would use a collection or data source. I feel that it's just easier to maintain.
User is offlineProfile CardPM
+Quote Post

Junije
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 01:19 PM
Post #8

New D.I.C Head
*

Joined: 8 Jul, 2008
Posts: 13

Thanks for the help all of you.
Your posts were really extensive and helpful!
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Adding Items To A Listbox From A Class
13 Aug, 2008 - 03:09 PM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(zakary @ 13 Aug, 2008 - 10:42 AM) *

QUOTE(baavgai @ 13 Aug, 2008 - 10:07 AM) *

If you wish to use the class like a data source as proposed by the second poster... I don't actually see an advantage to that approach over the above one. However, there is another I would recommend: the DataSource.


by using a DataSource or collection you do not limit yourself to a Form or Listbox. If you have to change your listbox to a CheckListBox or your form to a UserControl you will have to change more code to get this application to work.


Agreed. But you're doing this:
csharp

foreach(string item in new Class1().CreateCollectionOfItems()wink2.gif {
this.listBox1.Items.Add(item);
}


Which is not using a DataSource per se, but rather loading the ListBox Items Collection from string collection. You've lost your connection of the object returned by the method and lost all the benefits that can be offered by binding to the object.

Consider binding to the list, for better performance, more flexiabily, and economy of code:

csharp

this.listBox1.DataSource = new Class1().CreateCollectionOfItems();


Reference my prior post for more information.

User is offlineProfile CardPM
+Quote Post

zakary
RE: Adding Items To A Listbox From A Class
14 Aug, 2008 - 08:24 AM
Post #10

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 405



Thanked: 6 times
Dream Kudos: 175
My Contributions
QUOTE(baavgai @ 13 Aug, 2008 - 07:09 PM) *

Agreed. But you're doing this:

Consider binding to the list, for better performance, more flexiabily, and economy of code:

csharp

this.listBox1.DataSource = new Class1().CreateCollectionOfItems();


Reference my prior post for more information.


Good to know, I never considered Binding the data.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 09:50PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month