954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Adding LinkLabel-s to RichTextBox

I want to add LinkLabel-s at the end of the text in RichTextBox control.I do that with this code:

int index = richTextBox1.Text.Length;
Point position = richTextBox1.GetPositionFromCharIndex(index);
LinkLabel label = new LinkLabel();
label.Text = "whatever";
label.AutoSize = false;
label.Location = position;
label.LinkBehavior = LinkBehavior.NeverUnderline;
richTextBox1.Controls.Add(label);

The problem here is that i do not know how to add text right after the last added LinkLabel,i.e. i want when add LinkLabel to move the caret next to the LinkLabel.How to do that?

cygnusX
Newbie Poster
15 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

Reference below(sample---add LinkLabel into ListBox):

for (int i = 0;i < 3;i++)
{
LinkLabel ll = new LinkLabel();
ll.Name = "test"+i;
ll.Text = ll.Name;
ll.Location = new Point(0,i*ll.Height);
this.listBox1.Controls.Add(ll);
Console.WriteLine(ll.Location);
ll.LinkClicked +=
new LinkLabelLinkClickedEventHandler(this.linkClicked);
}

—————————————————————————————————
This answer's key is the Location attribute of the public class
System.Windows.Forms.Control,if you are familiar with this
attribute then would not have the question at all.

xueshanfh
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

Reference below(sample---add LinkLabel into ListBox):

for (int i = 0;i < 3;i++) { LinkLabel ll = new LinkLabel(); ll.Name = "test"+i; ll.Text = ll.Name; ll.Location = new Point(0,i*ll.Height); this.listBox1.Controls.Add(ll); Console.WriteLine(ll.Location); ll.LinkClicked += new LinkLabelLinkClickedEventHandler(this.linkClicked); }

————————————————————————————————— This answer's key is the Location attribute of the public class System.Windows.Forms.Control,if you are familiar with this attribute then would not have the question at all.

when there are too many LinkLabel lines in the ListBox control,
then you must add "this.listBox1.Items.Add(ll);" under the above
code "this.listBox1.Controls.Add(ll);" because of this can help
the ListBox control to show the vertical scrollbar.

xueshanfh
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 
when there are too many LinkLabel lines in the ListBox control, then you must add "this.listBox1.Items.Add(ll);" under the above code "this.listBox1.Controls.Add(ll);" because of this can help the ListBox control to show the vertical scrollbar.

The entire code is:

this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.listBox1.ItemHeight = new LinkLabel().Height;
for (int i = 0;i < 55;i++)
{
LinkLabel ll = new LinkLabel();
ll.Name = "test"+i;
ll.Text = "test"+i;
ll.Location = new Point(0,i*ll.Height);
this.listBox1.Controls.Add(ll);
this.listBox1.Items.Add(ll);
Console.WriteLine(ll.Location);
ll.LinkClicked +=
new LinkLabelLinkClickedEventHandler(this.linkClicked);
}

xueshanfh
Newbie Poster
3 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

Hi,

sorry for me horrible english, but I have one question.

It is possible detected which (automaticaly generated) linklabel is clicked?

I write more items to listBox, and i can add linklabels at delete links.
Example:
1. book ............. delete (this is linklabel id book is 1)
2. book ............. delete (id this book is 2)
...
...

Thanks for whatever answer!

dotmon
Newbie Poster
1 post since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You