3 Replies - 1192 Views - Last Post: 02 June 2009 - 07:23 PM Rate Topic: -----

#1 dawidass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Add string to listBox1 in other class then Form1

Post icon  Posted 02 June 2009 - 02:22 PM

I want to add string to listBox which is placed on Form1, but I wanto do it in another class then Form1, like in the code :

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

		public class Cat
		{
			private int weight;
			private string name;

			public Cat(string name, int weight)
			{			   
				this.name = name;
				this.weight = weight;
			}

		 
			public void TellWeight()
			{
				
				Form1 list = new Form1();
				list.listBox1.Items.Add(name + " wazy" + weight + " kg ");

				// but it dosn't show any result in listBox1	 
			}
		}

		public void Run()
		{
			Cat frisky = new Cat("Filemon", 2);
			frisky.TellWeight();
		}

		private void btnShow_Click(object sender, EventArgs e)
		{
			Run();
		}
	}




I also try to sent listBox1 as an object to method TellWeight() , changing the code like this :

 public void TellWeight(object e)
			{
							   
						  
			   this.listBox1.Items.Add("cos");
			   
			  //but it doesn't work at all
			   
			}


public void Run()
		{
			Cat frisky = new Cat("Filemon", 2);
			frisky.TellWeight(listBox1);
		}




Is it possible to do it in the way I posted ?

Is This A Good Question/Topic? 0
  • +

Replies To: Add string to listBox1 in other class then Form1

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Add string to listBox1 in other class then Form1

Posted 02 June 2009 - 02:44 PM

In your code you are trying to use this like you would on the form but you are inside the class Cat, not Form1. That is why you can't access the list box, it is not part of the class Cat. One way to fix this problem would be to a list box field to the class and pass the list box to the class when you call the constructor to use the list box in the form. Then you can use it like you would use it on the form.

private ListBox listBox;

public Cat(string name, int weight, ListBox listBox)
{
	this.name = name;
	this.weight = weight;
	this.listBox = listBox;
}

public void TellWeight()
{
	this.listBox.Items.Add(name + " wazy" + weight + " kg ");
}


Was This Post Helpful? 1
  • +
  • -

#3 dawidass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 02-June 09

Re: Add string to listBox1 in other class then Form1

Posted 02 June 2009 - 03:48 PM

Ok, now it works ;]

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Add string to listBox1 in other class then Form1

Posted 02 June 2009 - 07:23 PM

No problem, happy to help. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1