Best way to have linklabel open a windows form

B

bsturg21

Hello,
I have a windows form that has a series of linklabels on it, and I
need to have each linklabel, when clicked, open a separate windows
form that has a single paramter passed into it. The form that has the
System.Windows.Forms.LinkLabel controls on it is in a different
project and under a different namespace from the file where the
LinkLabel_LinkClicked events are, so I can't just do frm.ShowDialog
under the LinkClicked method. I've tried using eventhandlers, but the
controls are System.Windows.Forms controls and it will not let me pass
in that type of control to a custom eventhandler. Is the eventhandler
the best way to go about doing this, or is there something else I am
missing that would be simpler? Thanks.
 
P

Peter Duniho

bsturg21 said:
Hello,
I have a windows form that has a series of linklabels on it, and I
need to have each linklabel, when clicked, open a separate windows
form that has a single paramter passed into it. The form that has the
System.Windows.Forms.LinkLabel controls on it is in a different
project and under a different namespace from the file where the
LinkLabel_LinkClicked events are, so I can't just do frm.ShowDialog
under the LinkClicked method.

Why not? What is the definition of the variable "frm"? Is it a type
declared in the same namespace with the form containing the LinkLabel
instances?

And what do you mean by "...from the file where the
LinkLabel_LinkClicked events are"? The events themselves are in the
LinkLabel instances. Do you actually mean "...where the
LinkLabel_LinkClicked event handlers are"?

All you've said so far is that in one project/namespace you have a form
containing the LinkLabels, and in another project/namespace you have (I
am guessing you mean) the event handlers.

But as long as the event handlers are in the same project/namespace
where the dialog forms you want to show are, I don't see the problem.

Can I assume that the dialog forms are also in a different
project/namespace from where the event handlers are? If not, you should
clarify what you mean.
I've tried using eventhandlers, but the
controls are System.Windows.Forms controls and it will not let me pass
in that type of control to a custom eventhandler.

I also don't know what you mean here. What did you try to do? What
didn't work about it?

Your event handlers are, by definition, custom event handlers. That is,
you have to write them explicitly, and they do whatever you want them to
do. In addition, if you subscribe to an event on the LinkLabel control,
then that control should automatically be passed as the sender parameter
to the event handler method.

So, is there some other control that you are trying to pass to the event
handler instead? If so, what control is that? The only controls you
mentioned in your post are LinkLabel controls.
Is the eventhandler
the best way to go about doing this, or is there something else I am
missing that would be simpler? Thanks.

Your post isn't very clear about what you're actually trying to do and
what isn't working for you. Generally speaking, it's not a problem to
use classes from one project in another project, other than the need to
reference the project that has the class you want to use.

If you can't reference the project (for example, the project that needs
to use the class somehow is actually a general-purpose project that's
supposed to work with a variety of other projects), then you need to
delegate the creation of the forms to the referencing project somehow.
This could be done in a variety of ways.

One method might involve defining an event to which the referencing
project subscribes, and then when the event is raised, that referencing
project (the one that contains the dialog forms you want to instantiate)
creates the dialog form, and possibly even shows it (if you want the
referenced project -- that is, the one where the LinkClicked handlers
exist -- to actually show the dialog, then you'll instead want to define
an EventArgs-derived class that the referencing project can use to pass
the dialog back.

Another method might involve the referencing class simply instantiating
the dialog form class from the outset, and somehow passing that to the
event-handling class. Then the event-handling class can simply call the
ShowDialog() method on the form at the appropriate time.

There are probably a number of other mechanisms you can use, but without
a more clear explanation from you about what it is exactly you're trying
to do, it's hard to say. For best results, you should post a
concise-but-complete example of code that demonstrates what you're
trying to do, and what problem you're having with it.

Pete
 
B

bsturg21

Yes. The LinkClicked eventhandlers are in a different namespace from
the form that I am trying to show. I tried making an eventhandler
with the following code:

private void lnkName_LinkClicked(object sender,
Peracon.Client.Application.Controls.LinkLabelLinkClickedEventArgs e)
{
this.AssetClicked(sender, e);
}

but when the code to assign the eventhandler to the link label is
called, it complains because the linklabel is a System.Windows.Forms
control and the eventhandler takes in
Peracon.Client.Application.Controls.LinkLabelLinkClickedEventArgs.
Here is the code where it is assigning the eventhandler to the
linklabel. The AssetClick is the delegate:

this.lnkName.LinkClicked += new
Peracon.Client.Application.Controls.AssetClick(this.lnkName_LinkClicked);

So that is the problem. I think I actually want the ShowDialog() to
be called from the form that contains all the linklabels, and not from
the eventhandler, which is in a separate namespace, because I will
want that form to be passed as the parent to the form that is brought
up, which will be a dialog.
 
P

Peter Duniho

bsturg21 said:
Yes. The LinkClicked eventhandlers are in a different namespace from
the form that I am trying to show. I tried making an eventhandler
with the following code:

private void lnkName_LinkClicked(object sender,
Peracon.Client.Application.Controls.LinkLabelLinkClickedEventArgs e)
{
this.AssetClicked(sender, e);
}

but when the code to assign the eventhandler to the link label is
called, it complains because the linklabel is a System.Windows.Forms
control and the eventhandler takes in
Peracon.Client.Application.Controls.LinkLabelLinkClickedEventArgs.

Can you please explain what it is you expect the above code to do? You
seem to have defined your own EventArgs class, but the LinkLabel control
doesn't know anything about it, nor could it. Its event requires a
delegate that uses the
System.Windows.Forms.LinkLabelLinkClickedEventArgs class. Even if you
could somehow convince the compiler to compile your delegate that
declares a different type, the LinkLabel class is still only going to
pass the class it knows: its own LinkLabelLinkClickedEventArgs.

If you want to write a delegate that takes some other kind of EventArgs
parameter, where is it that you are expecting the instance of that class
to be created?
Here is the code where it is assigning the eventhandler to the
linklabel. The AssetClick is the delegate:

this.lnkName.LinkClicked += new
Peracon.Client.Application.Controls.AssetClick(this.lnkName_LinkClicked);

So that is the problem.

Why are you using a different delegate type than the one required by the
event? That is, the LinkLabelLinkClickedEventHandler type?

And finally, how is the above related to the last part of your message?
That is:
I think I actually want the ShowDialog() to
be called from the form that contains all the linklabels, and not from
the eventhandler, which is in a separate namespace, because I will
want that form to be passed as the parent to the form that is brought
up, which will be a dialog.

The two are not mutual requirements. That is, if you want the form with
the LinkLabel instances (I'll call it the "main form") to be the parent
of the dialog forms, all you need is for that parent to be used when
calling ShowDialog() on the dialog forms. The event handler does not
need to be in the same namespace as the main form to have this
reference; it just needs a way to get the reference.

As an example: if you have the event handler for a LinkClicked event,
that event handler is going to get the LinkLabel instance reference as
the "sender" argument. You can follow the Control.Parent property from
that argument up to the main form; if the LinkLabel instances are
top-level children of that form, you can just look at the Parent
property of the sender. Otherwise, you'll have to have a loop that
keeps getting the Parent property of the previous Parent until you reach
the top of the chain (Parent is null).

Of course, you need to cast the "sender" argument to a Control before
getting the Parent property.

So in the event handler, just get the parent form from the Parent
property of the sender and use that as your parent when calling
ShowDialog().

Now, you may have other reasons for wanting to call ShowDialog() from
the main form. But if the only reason you want to call ShowDialog()
from the main form is because of the parent issue, that's not actually a
reason you have to do that.

And all that said, I still don't see how this issue is related to the
first. That is, what does the need to set the parent when calling
ShowDialog() have to do with your apparent desire to use a class other
than the proper LinkLabelLinkClickedEventArgs class as the second event
handler argument?

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top