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.
Related
This question already has answers here:
How to use PasswordBox as TextBox? [closed]
(4 answers)
Closed 5 years ago.
I am currently making a c# application, in which I am using a text box with property PasswordChar - •. However, this text box has a button like the one below:
However, as a student and amateur, I am unable to make an if code for the button, which when pressed, shows or hides the real numbers of the password.
I think it should be something like this:
private void metroTextBox1_ButtonClick(object sender, EventArgs e)
{
if (metroTextBox1.PasswordChar='\•') metroTextBox1.PasswordChar = '\0';
else metroTextBox1.PasswordChar = '\•';
}
but I think I have at least 1 mistake here.
Please, help!
Your mistakes are in this :
textBox1.PasswordChar = '\•'
You must use == instead of = and '•' instead of '\•'
textBox1.PasswordChar == '•'
It worth be added as #Cody Gray has already noted that it is better to use UseSystemPasswordChar
I am trying to create a form with a multi-line TextBox with the following requirements:
The text in the text box might be short or long.
There is not much space availble.
I want to be sure that the entirety of the text has been seen. I can't be sure that the user has actually read it, but I can at least require that all of it has been seen.
So I'm trying to make a "fully viewed" multi-line TextBox.
This picture should make it clear what I'm trying to do:
If they check the checkbox before they've scrolled through the whole thing, I'll know not to believe them.
I think I need to know:
When the form comes up, was the text that was put into the TextBox short (all visible without scrolling) or long (the vertical scroll bar was shown)?
If the vertical scroll bar is showing, has the user scrolled it all the way to the bottom?
Any ideas about how to achieve this?
The TextBox has no scrolling event but the RichTextBox has. Also it has a method that allows you to get the index of the character closest to a point position.
private readonly Point _lowerRightCorner;
public frmDetectTextBoxScroll()
{
InitializeComponent();
_lowerRightCorner = new Point(richTextBox1.ClientRectangle.Right,
richTextBox1.ClientRectangle.Bottom);
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
int index = richTextBox1.GetCharIndexFromPosition(_lowerRightCorner);
if (index == richTextBox1.TextLength - 1) {
// Enable your checkbox here
}
}
I want to create filter insert text in my simple image editor program
based on this nice article. I found out how to do it, but I need some configure.
I make insert text more dynamic, just like Photoshop or Notepad. User click add text icon, cursor change and when user click on canvas that will be position for text. I already found this solution by mouse down event to get x & y screen coordinate.
I wanna make after (1), program will create something like textbox (minus background & border) so user can type text on it where in tutorial text is static.
I don't have idea how to do this, can anyone give me suggestion or example maybe?
Update
i really don't have any idea how to make this question more clear, spesific, or etc
i already try to divided what i want to 2 problem and solved one of them
i know if there is not just single method to archive what i want...but i don't know if there will be triple many (many many many) way to do it, so i must write spesific method i wanna use...
how can i know which one i will use if i even don't have any idea one of them....
in my mind just come dumb way create custom textbox with no background & border
Code
//set flagText active or not
private void InsertText(){
if (flagText){
flagText = false;
imageBoxCamera.Cursor = Cursors.Default;
}
else {
flagText = true;
imageBoxCamera.Cursor = Cursors.IBeam;
}
}
//in mouse down event i wanna create something like textbox (minus label & border) with coordinat x & y from mouse down
private void imageBoxCamera_MouseDown(object sender, MouseEventArgs e)
if (flagText) {
MessageBox.Show(Cursor.Position.X.ToString() + " " + Cursor.Position.Y.ToString());
}
}
Take a look on OpenPDN source code(fork for Paint.NET).
As I know there is a similar function there
https://code.google.com/p/openpdn/source/browse/#hg%2Fsrc
This question already has answers here:
How can I access one window's control (richtextbox) from another window in wpf?
(4 answers)
Closed 9 years ago.
Alright, so I have a form with textboxes and a button (Form 1), and that button opens a new form (Form 2) consisting of a textbox.
What I want to do is get the contents of one of the textboxes of the first form (like TextboxForm1.Text) and use that text in the second form, like TextboxForm2.Text = {however to reference textbox 1 from form 1}.Text;.
Is there an obvious way that I overlooked?
Thanks.
Edit: Tried both solutions and the both worked well, but making it public was much easier in the case of multiple textboxes.
http://msdn.microsoft.com/en-us/library/aa970905.aspx
<TextBox Name="TextboxForm1" x:FieldModifier="Public" />
Yes, you could use a property to expose the value of the desired textbox from the appropriate form. So, add something like the following to your window class:
public string TextBox1Text {
get { return TextBox1.Text; }
}
And then access it from the instance, as you seem to know, like this:
AnotherTextBox.Text = instance.TextBox1Text;
As for using the access modifier for the control as per nmclean's answer (i.e. FieldModifier="Public"), I would only say make the entire control public if it is needed.
I have a RichTextBox control on an application and here's my problem: when the application runs, if I start selecting with the mouse some of the characters inside a word and continue selecting outside it, the selection automatically includes the whole word in which I begun selection and any other words from which I want to select just a part, ms word-ish, if I'm not mistaken.
e.g.:
the text is: "Just another foobar"
what I want to select is: "Just ano[ther foo]bar" (the thing between the [])
what is actually selected: "Just [another foobar]"
The problem is just with mouse selecting, if I select text with the keyboard it works just fine. Also, the auto word select property of the control is turned off. Any idea why is that?
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 left an annotation at the bottom of this MSDN Library page with the details of the bug.
Maybe things have changed since this question was answered, but I have an even simpler solution:
Just add richTextBox1.AutoWordSelection = false; to the code.
Sounds crazy, but setting this to false in the properties-box does not work.
You have to do it in the code, even if the property is already false.
Then it works!