Read-only textbox in C# - c#

In C#, I am creating a form window for a LAN messenger with two textboxes. I need to create a particular textbox as read-only, but any text submitted to it is appearing grey which is not desirable. Is there any way that can be prevented?

I would use a Textbox and set ReadOnly to true, ForeColor to Color.Black, and BackColor to Color.White. This way you can still select the text and copy it with Ctrl-C.

You could replace it with a label or on the text box in the KeyPress event, set handled to true:
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}

You can set the colour of the text by setting the Textbox ForeColor property.
For example:
myTextBox.ForeColor = Color.Black

In order to keep the textbox white (or Window) when it's read-only, you must explicitly set the BackColor property to Window. To do this, you must first set the BackColor to some other value, then back to Window. The backcolor property should become bold indicating it is no longer the default value.

The grey color is indicative of the ReadOnly state of the textbox. It is a visual indication to the user who will not need to enter text to discover that the textbox is in fact, disabled.
If you need only the readonly behaviour, you would be better off using a Label instead.

Related

Avoid a textbox get's focus

I'm working in a Windows Form Application, I have a textbox which I want to avoid that gets the focus.
Now I'm using the property Enable but it gives a bad appearance to the form.
Also I tried with this
private void txtMyTextbox_Enter(object sender, EventArgs e)
{
ActiveControl = objMyOtherControl;
}
But like I'm selecting words of that textbox when the event is raise the textbox lose the selection.
If you want to make the textbox unfocusable but you still want be able to select the text, what you are looking for is the ReadOnly property.
Here is an extract from the official DOC:
You can transform an editable Windows Forms text box into a read-only
control. For example, the text box may display a value that is usually
edited but may not be currently, due to the state of the application.
To create a read-only text box Set the TextBox control's ReadOnly
property to true. With the property set to true, users can still
scroll and highlight text in a text box without allowing changes. A
Copy command is functional in a text box, but Cut and Paste commands
are not. Note The ReadOnly property only affects user interaction at
run time. You can still change text box contents programmatically at
run time by changing the Text property of the text box.

WPF: RichTextBox selection style properties are reset

I've made a simple text editor (based on RichTextBox) that can bolden/tilt/change font size of selected text. And those things work just fine - e.g. I can apply both Bold and Italic to the same selection.
I recently added "Capitalize" button:
private void buttonCapitalize_Click(object sender, RoutedEventArgs e)
{
if (!textField.Selection.IsEmpty)
{
textField.Selection.Text = textField.Selection.Text.ToUpper();
}
}
aand it kinda works. Whenever I click it the selected text is capitalized but also other properties (of the current selection only) such as FontStyle, FontWeight are set to normal and FontSize to default.
Is there any better way to implement this?
I run some testing and from my results it seems that the RichTextBox will always take the style from the 1st character BEFORE your selection and not the default style as you mentioned
This probably happens because
textField.Selection.Text = textField.Selection.Text.ToUpper();
will actually create a new string and not edit it (strings are immutable in C#)
If you want to keep your styling I'm guessing that you'll have to iterate over your selection and create it per selected char

Why does DataGridView adopt the font of a windows form?

When I set the Font of a Form the DataGridView adopts the same font.
Why does the DataGridView adopt the font of the form?
From the documentation for DataGridView.Font:
The Font property is an ambient property. An ambient property is a control property that, if not set, is retrieved from the parent control. For example, a Button will have the same BackColor as its parent Form by default. For more information about ambient properties, see the AmbientProperties class or the Control class overview.
In other words, it sounds like it's behaving exactly as documented. If you want it to use a font other than the form's font, set the property explicitly.
I have experienced this same issue, where I have a default font on the form but I want the DataGridView to have a different font.
You can fix this by placing a Load event that will change the font.
private void Form1_Load(object sender, System.EventArgs e)
{
// set to your font you want.
this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 15);
}
Or place the DataGridView inside of a GroupBox and set the GroupBox to the font that I want -- this fix is ugly but it works.

How to disable textbox from editing?

I want to use a text box to display some text. I can not disable it, because then the scroll bar will not work.
How can I prevent editing within the multi-line textbox, yet make it appear as if it is enabled, so that the scroll bar works correctly?
You can set the ReadOnly property to true.
Quoth the link:
When this property is set to true, the contents of the control cannot
be changed by the user at runtime. With this property set to true, you
can still set the value of the Text property in code. You can use this
feature instead of disabling the control with the Enabled property to
allow the contents to be copied and ToolTips to be shown.
The TextBox has a property called ReadOnly. If you set that property to true then the TextBox will still be able to scroll but the user wont be able to change the value.
As mentioned above, you can change the property of the textbox "Read Only" to "True" from the properties window.
textBox1.ReadOnly = true;
"true" property will make the text box readonly activate. and "false" will make it in regular form. Thanks.

Conditional prevention of the textbox change

I have a TextBox, and a TextBlock within the Border. The TextBlock's Text property is bound to TextBox's value. When I type into the TextBox, the Border changes its width according to the TextBlock's new size.
There is an event handler for TextBox.TextChanged in which I test whether the size of the border exceeds a certain number. If it does, I want to prevent the TextBox from making the change that caused the handler.
If a character was always added to an end, I would be able just to substring the text, but all other sorts of changing can occur, for example pasting a large amount of text into the TextBox.
So, what would be the way of preventing the change from the handler? I remember in some WindowsForms e.Cancel property which when set would ignore the action, but haven't seen that in WPF and the TextChangedEventArgs obviously does not have one.
Thanks
You can listen to the PreviewTextInput event and set e.Handled to true to prevent the change from taking effect.

Categories

Resources