Changing RichTextBox to a Label [duplicate] - c#

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.)

Related

How do I prevent RichTextBox from jumping to the top of the document when links are clicked?

As part of my GUI in C# (.Net 5.0) I'm using the RichTextBox control (part of System.Windows.Forms) to display text and clickable links. My problem is when I click any links included without the form first having focus the act of clicking a link also focuses the textbox which automatically places the caret at the start of the text, this has the effect of making the textbox jump (or scroll) to the very top of document, losing one's position.
For my particular problem, the RichTextBox classes are assigned to TabPages within a TabControl- and all three of these components are created in response to user interaction (I understood the creation of these at runtime might limit the types of event handling available to me).
I've defined the RichTextBox control with multiline and both horizontal and vertical scrollbars, and to enable clickable links I'm using the DetectUrls option. Here is the full list of properties being used.
var control = new RichTextBox();
control.DetectUrls = true;
control.Dock = DockStyle.Fill;
control.Multiline = true;
control.ReadOnly = true;
control.WordWrap = false;
control.Text = myContent; // large content filling multiple screens
control.ScrollBars = RichTextBoxScrollBars.Both;
control.LinkClicked += new LinkClickedEventHandler(RichTextBoxLinkClicked);
I've included the LinkClicked because I want to provide some interaction for the user, but whether this handler is assigned or not doesn't seem to influence the problem.
Edit1 I came up with a solution that works to some extent; it prevents the scrolling by placing the caret at the beginning of the link (It doesn't work well though if there are more than one of the same link)
private void RichTextBoxLinkClicked(object sender, LinkClickedEventArgs e) {
int caret = ((RichTextBox) sender).Text.IndexOf(e.LinkText);
SelectionStart = caret;
}
Edit2 A second solution that I found is to set the focus of the text-box on a MouseEnter event. I've come to think the key to the problem is to set the focus of the text boxes before any user interaction takes place, but I'm having problems finding a satisfactory way to achieve it.

How to make "links" added to the text? [duplicate]

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)

C# When the mouse cursor hovering in ControlLabel it will show up list of items? What kind of control is that? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a Label Control then when mouse cursor is hovering around it for about 1second, it will show list of items in text format(the items are either plain text or it is also a control). It's like in the website Home/Contact Us/About Us/Help. What kind of control i'm going to use?
Like for example. www.ebay.com. Under "My Ebay", it will shows you the items, eg Summary/Bids/List/Messages...etc...
What you see is best implemented by a ToolTip.
To add a ToolTip to a Control you usually do this:
Add a ToolTip from the toolbox to the Form
Now each control on the form as a new property field in the properties pane: "ToolTip" on "yourToolTipName"
Set a tooltip text for each control you want to show a tool tip.
This is really simple.
Therefore many folks believe that that is all a ToolTip can do.
But it can be created and modified dynamically and styled in many ways. Showing a list of items is no problem at all..
To load it with data dynamically you code the MouseHover event of your Label:
private void yourLabel_MouseHover(object sender, EventArgs e)
{
toolTip1.SetToolTip(yourLabel, yourData);
}
Of course you may want to call a function to load the right and current data for each of your Labels..: string loadData(Label lbl).
If you want to you can easily ownerdraw the ToolTip. First code its Popup event:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
toolTip1.BackColor = Color.LightGoldenrodYellow; // pick a Color if you want to
toolTip1.OwnerDraw = true;
}
Then the Draw event:
private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
{
using (Font font = new Font("Consolas", e.Font.SizeInPoints))
{
e.DrawBackground();
e.DrawBorder();
e.Graphics.DrawString(e.ToolTipText, font, Brushes.DarkMagenta, e.Bounds);
}
Note that the ToolTip will automatically resize to display the text. Also note that you can embed \n characters to create line breaks.
I chose the monospaced font Consolas, so I can create nicely aligned columns of text. Also note that if you want to enlarge the font you should add enough room via extra lines and/or space, this is because the size of the ToolTip area is calculated from its original font size and you can't pick a different Font for it.. See here for a little more on that..
Also note that you do not need to set the property in the designer at all. The MouseHover does all you need..
Create ContextMenuStrip, set event handlers for items:
var contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Item 1");
contextMenu.Items.Add("Item 2");
contextMenu.Items.Add("Item 3");
Your ControlLabel:
var label = new Label { Parent = this, BorderStyle = BorderStyle.FixedSingle, Text = "Test" };
Show menu manually:
label.MouseHover += (s, e) =>
{
contextMenu.Show(label, 0, label.Height);
};

why all text gets selected in RichTextBox in C#? [duplicate]

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.

C# Windows forms textbox greyed out [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'll try to be as specific as possible. I'm using visual basic 2010 c# express edition. I'm trying to create a textbox that is filled with information from the program. Suppose I put the text "Hello" in the textbox, when I run it, the form has a textbox saying Hello.
Here, the user can select the text and copy it. Basically, when the mouse goes over the textbox, it changes appearance and the textbox is interactive.
What I need to make is the textbox as not-interactive. In the textbox properties, there is an option called "Enabled". If I make it as False, all my requirements are satisfied. But the textbox is greyed out. Is there any way to get "Enabled" to false and still make the textbox look not greyed out. My query is regarding aesthetics.
You can make the textbox readonly:
Creating a Read-Only Text Box (Windows Forms)
To make the background gray, you probably need to change the background color:
txtFoo.BackColor = ...;
And if you do no want to make the text selectable, set Enabled = false;
You can create your own control that will look exactly like a TextBox but will be static. It's very easy to achieve that. Right-click on your project name in Solution Explorer and choose: Add > New Item... > Custom Control. You can name it somehow, e.g. DisabledTextBox.
Here's the full code of the new control.
public partial class DisabledTextBox : Control
{
public DisabledTextBox()
{
InitializeComponent();
DoubleBuffered = true; // To avoid flickering
}
protected override void OnPaint(PaintEventArgs pe)
{
pe.Graphics.Clear(SystemColors.Window); // White background
pe.Graphics.DrawRectangle(SystemPens.ActiveBorder, new Rectangle(0, 0, Width - 1, Height - 1)); // Gray border
pe.Graphics.DrawString(Text, Font, SystemBrushes.WindowText, 1, 3); // Our text
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
Invalidate(); // We want to repaint our control when text changes
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
Height = Font.Height + 7; // This limit the height of our control so it will beahave like a normal TextBox
}
}
When you compile it, your new control will be available in Toolbox, so you can use like any other control. It will look exactly like TextBox.
textBox.BackColor = System.Drawing.SystemColors.Window;
Setting ReadOnly property to True should do the trick
I was looking for the easy solution to this question>
Here is what worked for me:
textBox Enabled property -- true
textBox ReadOnly property -- true
And below line of code to get rid of they greyed out area.
public Test_class()
{
InitializeComponent();
textBox.BackColor = System.Drawing.SystemColors.Window;
}
Yes, the user still can select the value in the text box but not entering a new value or edit old one.
Cheers!

Categories

Resources