473,398 Members | 2,403 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,398 software developers and data experts.

Best way to have linklabel open a windows form

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.

Aug 17 '07 #1
3 4646
bsturg21 wrote:
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
Aug 17 '07 #2
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.LinkLabelLinkC lickedEventArgs 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.LinkLabelLinkC lickedEventArgs.
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(thi s.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.

On Aug 17, 1:18 am, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
bsturg21 wrote:
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

Aug 17 '07 #3
bsturg21 wrote:
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.LinkLabelLinkC lickedEventArgs 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.LinkLabelLinkC lickedEventArgs.
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(thi s.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
Aug 17 '07 #4

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

Similar topics

1
by: Nick P | last post by:
Is it possible to use javascript within a link label link in C#? I want to call up a web page from a link label but without any scroll bars and have it a certain size. I am trying to use the...
0
by: tota | last post by:
i'm using windowsApplication to connect to DB made by SQL i need to Display the Data of the first table in the DB as links when click it it openes a table and i navigte between them using buttons...
1
by: gabe | last post by:
I am looking for a way to mimic some of the functionality of a web page without writing html (or I am looking to someone to point out something really obvious that I am over looking) Currently I...
3
by: vince | last post by:
Hello, I have a ListBox filled with many lines of simply text I want to make few lines be a LinkLabel How can I do this Thanks
0
by: Suz | last post by:
I am stumped! I can't seem to figure out how to put a linklabel in a datagrid on a windows form. I have working code that displays a linklabel in edit mode but need the linklabel to display during...
1
by: Marcus Kwok | last post by:
I am having problems getting my LinkLabel hyperlink to work properly. Every time I click on it, I get the following exception: System.ComponentModel.Win32Exception: The requested lookup key was...
4
by: David Veeneman | last post by:
I'm creating a UserControl that uses a LinkLabel. For reasons that I won't bore everyone with, I don't want the LinkLable to show the default hand cursor when the mouse enters the control. Should...
0
by: kirk | last post by:
I started with a System.Windows.Forms.Form and subscribed to its LostFocus event. The event fires perfect. I added a System.Windows.Forms.LinkLabel to the aforementioned Form and tested the...
2
by: prokopis | last post by:
am using c# for windows applications. am using dynamic linklayer array to print some labels in the form.i get some data from the database i add them in the linklabel array and i print it on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.