Adding items to a listbox from a Class

Accessing a form control from a void in a Class

Page 1 of 1

9 Replies - 11632 Views - Last Post: 14 August 2008 - 09:24 AM Rate Topic: -----

#1 Junije  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 08

Adding items to a listbox from a Class

Post icon  Posted 13 August 2008 - 02:39 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Adding items to a listbox from a Class

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 05:34 AM

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 --
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 --
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");
        }
    }
}


Was This Post Helpful? 1

#3 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 06: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:

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


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 August 2008 - 09:21 AM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 07:07 AM

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:

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.
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:

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



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 07:42 AM

View Postbaavgai, on 13 Aug, 2008 - 10:07 AM, said:

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

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 07:42 AM

View Postzakary, on 13 Aug, 2008 - 09:08 AM, said:

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

#7 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 07:50 AM

View Posteclipsed4utoo, on 13 Aug, 2008 - 10:42 AM, said:

View Postzakary, on 13 Aug, 2008 - 09:08 AM, said:

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

#8 Junije  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 08-July 08

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 02:19 PM

Thanks for the help all of you.
Your posts were really extensive and helpful!
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,596
  • Joined: 16-October 07

Re: Adding items to a listbox from a Class

Posted 13 August 2008 - 04:09 PM

View Postzakary, on 13 Aug, 2008 - 10:42 AM, said:

View Postbaavgai, on 13 Aug, 2008 - 10:07 AM, said:

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:
foreach(string item in new Class1().CreateCollectionOfItems();)/> {
	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:

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



Reference my prior post for more information.
Was This Post Helpful? 0
  • +
  • -

#10 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Adding items to a listbox from a Class

Posted 14 August 2008 - 09:24 AM

View Postbaavgai, on 13 Aug, 2008 - 07:09 PM, said:

Agreed. But you're doing this:

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

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



Reference my prior post for more information.


Good to know, I never considered Binding the data.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1