473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to use Form1 listbox from other class within the same namespace

Hi

I have an Form1 class generated with studio with an listbox on it.
how can i access this listbox from other classes ?

public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.ListBox listBox1;
private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Add("some text");
}
}

public class ServerClass : MyTestInterface
{
public override string MyFirstMethod(string what)
{
// here i want to add some text to Form1.listbox1
return "" + what;

}

}

Nov 15 '05 #1
4 1972
Robert,

First, you have to expose the listbox, either by making its
accessibility public, or by exposing it through a property of some kind.
Then, you would pass a reference to the form to the class that you want to
use it, by setting a property, or passing it through a method. A form is
like any other object which has properties, methods, and fields. You have
to pass it around to access it.

Hope ths helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"Robert" <ro*****************@softech.ro> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
Hi

I have an Form1 class generated with studio with an listbox on it.
how can i access this listbox from other classes ?

public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.ListBox listBox1;
private void button1_Click(object sender, System.EventArgs e)
{
listBox1.Items.Add("some text");
}
}

public class ServerClass : MyTestInterface
{
public override string MyFirstMethod(string what)
{
// here i want to add some text to Form1.listbox1
return "" + what;

}

}


Nov 15 '05 #2
You need to pass the object reference to Form1 when creating ServerClass,
then use Form1.listBox1.Items.Add("more text");

ServerClass newServerClass = new ServerClass(this);

and

private Form1 parent = null;

public ServerClass(Form1 parent)
{
this.parent = parent;
}

There may be other and better ways, but this should work.

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #3


yes but when the client invokes MyFirstMethod method
an another instance of ServerClass is cretead
where the parent is not set .

there is no choice to use an "global" variable ?
Robert

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
simply declare it as public static. this is usefull when you cannot instance
a form (main form for example).
from time to time, stupid editor will remove static from code, so just add
it back
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: polarz | last post by:
I'm having trouble getting items from different classes to display in my textBox. e.g. private void button2_Click(object sender, System.EventArgs e) { someData sd = new someData();...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
5
by: PAPutzback | last post by:
Form2 has one purpose to open and list some names and ids. I want to handle the list box click event on form2 so I can get the selected value onto a field in form1. I changed this Dim MyForm2...
3
by: R. Harris | last post by:
Hi. I have 2 forms: form1 form2 On Form2 I have a listbox and a button. When I click the button it calls a function from form1 and within that function it updates the listbox on form2. My...
6
by: Paul | last post by:
Hi All, Framework 1.1 listbox control unable to DataBind I've been googling for an answer to this query that appears quite a lot, but none, it seem, answers my problem directly. I am...
1
by: bg_ie | last post by:
Hi, I have my own UserSettings class that I wish to include within my windows application. To include it at the moment, I do something like this - namespace WindowsApplication1 { static...
2
by: Jamey Bon | last post by:
I am a C# newbie. I am having a tough time with several issues of scope and visibility. In short, why can't I see any of the elements of Form1 (the base form generated by the "Windows...
2
by: lewisms | last post by:
Hello all, I am quite new to c++/. Net so please don't shoot me down for being a newbie. Any way I am trying to make a simple multithreading program that is just to learn the ideas behind it...
0
by: ezrollers | last post by:
I'm still learning C# and this is really annoying me. I need to modify the contents of a text box on a windows program. (Using windows app template on VS 2005) I have created a property on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.