Jyina
Jyina

Reputation: 2902

How to append a link label to an existing label?

I have a label inside a panel to display simple text. Now I have a link label that is added dynamically that displays some more information. How can I show this link label right next to the label text at run time? For example, the lable displays

A record is added.

I need to show with a link label "View Additional Details" next to the label text.

A record is added. View Additional Details

I have the code as below but it overlaps the existing label text. Thanks for any help!

        LinkLabel details = new LinkLabel();
        details.Text = "View Additional Details";

        LinkLabel.Link link = new LinkLabel.Link();
        link.LinkData = infoDetails;

        details.Links.Add(link);

        details.LinkClicked += new LinkLabelLinkClickedEventHandler(details_LinkClicked);
        //Adding the link label control to the existing label control
        lblInfo.Visible = true;
        lblInfo.AutoSize = true;
        lblInfo.Controls.Add(details);

Upvotes: 0

Views: 326

Answers (1)

fotijr
fotijr

Reputation: 1096

Why are you trying to add a LinkLabel to a label? Add the LinkLabel to the same form as the label, and set the location of the LinkLabel appropriately.

In the example below, I'm assuming the code is being called from the form's class (or to a panel if you are using one). If not, replace this with your form instance. I'm setting the Y location as the same as lblInfo so the LinkLabel appears next to it. Adjust lblInfo.Margin.Right and details.Margin.Left as desired.

details.Margin.Left = 5;
details.Location = new Point(
     lblInfo.Location.X + lblInfo.Width + lblInfo.Margin.Right + details.Margin.Left,
     lblInfo.Location.Y
 );
this.Controls.Add(details);

Update: changed padding to use Margin (thanks Anthony).

Upvotes: 2

Related Questions