This question already has answers here:
How can I make a hyperlink work in a RichTextBox?
(4 answers)
Closed 4 years ago.
Made a button to add "links" to "richTextBox".
"Links" are added not to the text, but from above "richTextBox".
Question
How to make the "links" added to the text based on the current code or based on another solution?
LinkLabel link = new LinkLabel();
link.Text = "*** LINK ***";
// link.LinkClicked
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
// data
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
// link
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.richTextBox1.GetPositionFromCharIndex(this.richTextBox1.TextLength);
// richTextBox1
this.richTextBox1.Controls.Add(link);
this.richTextBox1.AppendText(link.Text + " ");
this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
GIF
This is wronga approach. The text in RichTextBox is not parts of Controls, so if you add LinkLabel into Controls, their positions will not be synchronized.
Look at this question How can I make a hyperlink work in a RichTextBox?
As an alternative to the solution by TcKs, check this: Links with arbitrary text in a RichTextBox
An author of related article wrote:
every time the text in the RichTextBox is changed, the text is parsed
for URLs and the matching text ranges are formatted as links
(underlined, blue foreground by default)
Related
This question already has answers here:
Add Row Dynamically in TableLayoutPanel
(4 answers)
Closed 3 years ago.
I am trying to make a sort of notification within my form. The panel will consist of a rich textbox followed by two buttons underneath it.
Design of panel
I would need a way of programatically displaying new panels with different data in each one and having it displayed so that it can be scrolled. Initially, i have tried to create a table layout panel and then a panel. I could not figure out how to create a new instance of the panel i created in designer to place it into the table layout.
If there is another simple way of achieving my goal of having a group of controls being added beneath each other programatically i would appreciate it.
You can use this code to generate dynamic controls, but i am just sharing with you sample code.
int n = 10;
Panel[] p=new Panel[n];
int j = 10;
for (int i=0;i<n;i++)
{
j += 10;
p[i] = new Panel();
p[i].BorderStyle = BorderStyle.Fixed3D;
p[i].Height = 100;
p[i].Location = new Point(100,10+j);
this.Controls.Add(p[i]);
}
This question already has an answer here:
Line break in Label Box
(1 answer)
Closed 5 years ago.
I know how to display one line of text in a label using
thislabels.Text = "Hello";
But I want to be able to display a line of text each time an event happens, while also maintaining whats currently on display.
For example, I'm making a scoreboard and I want to print every time someone scores something like this..
Joe Smith scored 3 points
Joe Smith scored 5 points
...
Smith Joe scored 3 points
How do I do this? I'm not concerned with any of the formatting of how to display the text, I just want to know I can keep a running of events
You could append a newline every time, some examples:
label.Text += $"{Environment.NewLine}YourTextHere";//String-interpolation
label.Text += String.Format("{0}YourTextHere", Environment.NewLine);
label.Text += Environment.NewLine + "YourText";
This could however get quite big, I suggest putting your label in a panel with the property AutoScroll to true, this way the panel is the max size of the label and instead of going off the Form it will make a scrollbar:
panel.AutoScroll = true;
thislabel.Text += "Hello"
Hello
thislabel.Text += "\r\nWorld"
Hello
World
This question already has answers here:
How to dynamically generate a TextBox control.
(5 answers)
Closed 5 years ago.
First sorry for the inaccurate first question. Now i edited it.
My question/problem is how to save (and keep) controls added at run time in my code? (so if i add label and text box at run time in program it must be in my code like if i implemented it manual)
For example text box added dynamically, but it disappears after reboot the program:
protected void Button1_Click(object sender, EventArgs e)
{
var txt = new TextBox();
txt.ID = "textBox1";
txt.Text = "helloo";
form1.Controls.Add(txt);
var lbl = new Label();
lbl.ID = "label1";
lbl.Text = "I am a label";
form1.Controls.Add(lbl);
// Increase the number added and add the new label and textbox
TotalNumberAdded++;
AddControls(TotalNumberAdded);
}
I planned to implement asp.net web application where can users add additional tasks next to pre-implemented.
Thanks for answers,
Rookie programmer Alex :)
For adding controls at runtime you can try the PlaceHolder control like this:
Button b = new Button();
b.ID = "YourID";
b.Text = "Button";
PlaceHolderTest.Controls.Add(b);
but I dont know any method to add this to your assembly. Maybe you should consider redesigning to use multiple User Controls, which gives you the ability to load different User Controls that contains your controls, meaning you have to create multiple User Controls.
This question already has answers here:
C# RichTextBox selection problem
(2 answers)
Closed 7 years ago.
I only have form1 with RichTextBox (windows form) and I have no code.
Lets say we write "123456789" in RichTextBox via the keyboard.
The problem is: when I try to select number 9 from right to left by using the mouse then the whole text get selected automatically before even I select the rest of the text.
But I CAN select 9 from left to right without the rest of text gets selected. And also I CAN select number 1 from right to left and without the rest of text gets selected. The problem happens only when you select the last number from right to left.
you can select any number from right to left and the rest of the text does not get selected but if you select the last number from right to left then the whole text gets selected.
I checked the RichTextBox properties but nothing interesting there. TexBox does not behave like this but I do not want to use textbox.
My question is: How can I select number 9 from right to left in RichTextBox using mouse and avoid the whole text from being selected automatically. Thank you
See the answer given by Hans Passant, all credit goes to him.
(At this point I feel that giving him more rep is like taking a p*ss into Niagra Falls)
C# RichTextBox selection problem
In Hans' words:
There's a silly bug in the AutoWordSelection property implementation. The workaround is equally silly. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form, replacing the existing RTB.
using System;
using System.Windows.Forms;
public class FixedRichTextBox : RichTextBox {
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (!base.AutoWordSelection) {
base.AutoWordSelection = true;
base.AutoWordSelection = false;
}
}
}
I could definitely replicate this behaviour before, and the custom RichTextBox fixed it for me.
This question already has answers here:
How do I change the style of a disabled control?
(2 answers)
Closed 9 years ago.
I am using a rich text box as a label for my application. The text box is read-only but its content can be selected. How can I make users can't select text in the rich text box while it is read-only?
When I disable the control can't select text but I loose the colors, because they become grey (disabled). How can I disable text selection without disabling the rich text box control?
FYI: I am using a rich text box as a label because, I need to change the fore color to red for one word in the string which needs to be shown to the user. I used this SO article and following method to do it.
string word = "red";
int start = richTextBox1.Find(word);
if (start >= 0) {
richTextBox1.Select(start, word.Length);
richTextBox1.SelectionColor = Color.Red;
}
EDIT: BTW It's C# WinForm
Simply handle the selection, and restore it to "nothing":
// so you have colour (set via the Designer)
richTextBox.Enabled = true;
// so users cannot change the contents (set via the Designer)
richTextBox.ReadOnly = true;
// allow users to select the text, but override what they do, IF they select the text (set via the Designer)
richTextBox.SelectionChanged += new System.EventHandler(this.richTextBox_SelectionChanged);
// If the user selects text, then de-select it
private void richTextBox_SelectionChanged(object sender, EventArgs e)
{
// Move the cursor to the end
if (this.richTextBox.SelectionStart != this.richTextBox.TextLength)
{
this.richTextBox.SelectionStart = this.richTextBox.TextLength;
}
}
Taken from: http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/d1132ee5-acad-49f3-ae93-19d386fe2d12/
(By the way, a little bit of searching goes a long way.)