Connecting Tech Pros Worldwide Forums | Help | Site Map

How do I get control of a list box outside of the namespace from a class

Newbie
 
Join Date: Mar 2009
Posts: 26
#1: Mar 17 '09
What I am trying to do is to modify the Listbox

from a class that is in the main namespace.

I have tried
doing a using WindowsApplication2;

WindowsApplication2.Form1 y = new Form1();

y.listbox1.Insert.Add(String)


But the list box does not seem to populate as it would had I done

listbox1.Insert.Add(String)

In the main. What am I doing wrong?

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,767
#2: Mar 17 '09

re: How do I get control of a list box outside of the namespace from a class


A. Directly affect controls from different threads other than the UI thread that created the control is going to cause problems.

B. I've always added items to ListBoxes by added to its .Items collection:
listbox1.Items.Add(string);
Newbie
 
Join Date: Mar 2009
Posts: 26
#3: Mar 17 '09

re: How do I get control of a list box outside of the namespace from a class


Yes I have done this the problem is I have a class file Crypto that is created and called and within this class is where I try to access the list box and cant seem to do it.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,767
#4: Mar 17 '09

re: How do I get control of a list box outside of the namespace from a class


Is WindowsApplication2 really a completely seperate second application? Do you have one application trying to access the listbox of a totally different application? Or just one class trying to access the listbox of a different class? One form trying to access the listbox of another form?

It really isnt' a good plan to have your class CRYPTO being tightly tied to the UI controls. It makes it a bear to update and make changes later.

Better to have CRYPTO raise an event that your UI is subscribed to. When crypto raises an event with the string as part of its arguments, then the form with the listbox can receive the event and react accordingly.

Think of it this way... The UI has only one purpose: User Interface. Your crypto class has only one purpose: Do cryptography. When crypto is done it should raise it's hand and yell out the answer. The UI can react as it needs to. But crypto doesn't need to understand what UI does, or even how many other classes are listening. You could have 10 classes all listening for crypto to finish and yell the answer. One does the listbox... one sends the answer over a socket connection... one stops a method timer... one logs the action... You wouldn't want to make your crypto class do all those other things explicitly.
Reply