C# Forms

  • Thread starter Thread starter Stu
  • Start date Start date
S

Stu

Hi guys,

I have gone through google and tried different ideas for the past 4 hours
now and am getting nowhere.

I currently have a class that raises an event, from this event I want to add
a value to a listbox on form1.

However I cannot access the listbox on form1. I need to make a reference to
the object (so VS tells me), the following line works but not for what I
want (am new so bear with me),

Form1 fm = new Form1();

I know this creates a new instance of a form (which I dont want) I want to
access the already running form, I have a property on the form that will set
the value for me, I'm trying to access that property, the only way I can do
that is to make it static (comes up in intellisense) and make a reference to
it which takes me to the Form1 fm = new Form1(); :/

Is this the wrong way to go about this?

Any pointers in the right direction is much appreciated.

Thanks

Stu
 
Hello Stu,
I currently have a class that raises an event, from this event I want to
add a value to a listbox on form1.

When you created the class, is there a way that you can pass the reference
to Form 1 at that time? How about for this particular method? Can the
Form1 object be passed into the method as a parameter. If the caller, or
the caller's-caller, (etc) was the form, then you can use the keyword 'this'
to pass in a reference to the Form1 object that can be used by the callee
code to invoke a method on the form.

That's the simplest way.

the only way I can do that is to make [the form] static (comes up in
intellisense) and make a reference to it which takes me to the Form1 fm =
new Form1(); :/

Bad idea. Won't work. You can make it work with a lot of fiddling, but if
you do, you've made a very frail object.

Note that some folks use delegates for this. They will set up their forms
with an event delegate, and when the object changes, the object layer raises
and event that the form receives. The form then reads the objects,
repopulates the correct controls (depending on the event raised) and
returns. Not a bad way to do it.

An intro to delegates:
http://www.developer.com/net/net/article.php/3445561

You could also create data bound controls, but I'm not sure if this will
work in your scenario. I just don't know enough about what you are trying
to accomplish. That said, here's a link that may help with this:
http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet02252003.asp

I hope this helps,
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
Hi Nick,

thank you very much for your post.

I'm basically playing around with irc and getting messages to post to a
listbox. I'm using the Smartirc4net libary (smartirc4net.meebey.net), his
work is great. The class triggers an event when a message is received to a
channel and i was trying to get the method for the event to add to the
listbox on form1, but because something was declared static it wasn't having
any of it.

I made a class that I created a new instance of on form1 that would take
care of connecting etc, perhaps this was the wrong way to go and should have
just had form1 talk to the instance of smartirc4net directly. Since using
IRC4net directly on form1 and having nothing that's static all seems to be
well. The late night last night must have messed with the brain on the
static methods :/ However, all is sorted now and I am accessing the instance
of smartirc4net on the form with nothing being static and it will allow me
to talk to the listbox.

Thanks for the read on delegates, time to get learning them.

Thanks again

Stu

Nick Malik said:
Hello Stu,
I currently have a class that raises an event, from this event I want to
add a value to a listbox on form1.

When you created the class, is there a way that you can pass the reference
to Form 1 at that time? How about for this particular method? Can the
Form1 object be passed into the method as a parameter. If the caller, or
the caller's-caller, (etc) was the form, then you can use the keyword
'this' to pass in a reference to the Form1 object that can be used by the
callee code to invoke a method on the form.

That's the simplest way.

the only way I can do that is to make [the form] static (comes up in
intellisense) and make a reference to it which takes me to the Form1 fm =
new Form1(); :/

Bad idea. Won't work. You can make it work with a lot of fiddling, but
if you do, you've made a very frail object.

Note that some folks use delegates for this. They will set up their forms
with an event delegate, and when the object changes, the object layer
raises and event that the form receives. The form then reads the
objects, repopulates the correct controls (depending on the event raised)
and returns. Not a bad way to do it.

An intro to delegates:
http://www.developer.com/net/net/article.php/3445561

You could also create data bound controls, but I'm not sure if this will
work in your scenario. I just don't know enough about what you are trying
to accomplish. That said, here's a link that may help with this:
http://msdn.microsoft.com/library/en-us/dnadvnet/html/vbnet02252003.asp

I hope this helps,
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
Back
Top