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!
Related
i am making a flat GUI in windows forms.. i know i cant just ignore and leave tab indexing and i have included it. when i am using tab in my program to select control,a black outline appears on controls(as in this pic). what i want is to remove this outlining when i mouse-click anywhere because it seems Ugly if i am not using my keyboard.
how can i remove this outline on flat button
can you suggest me how can i achieve this target?
Try hiding the focus cue by creating your own button:
public class ButtonEx : Button {
protected override bool ShowFocusCues {
get {
return false;
}
}
}
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.
Tried most properties and did not manage to completely disable the focus of a checkbox in visual studio. Does anyone know how to do it?
I am using a System.Windows.Forms.CheckBox object. I am using an image as background and when the CheckBox is in focused state a border is drawn, which makes the background image look pretty bad. So I want to get rid of it...
EDIT: Adding a picture to clarify the intention of this question...
The user can tap "TAB" and click on the object to see it displayed as focused.
That was a problem for me since it made the GUI look simply terrible.
The code for the CheckBox control that takes care of the painting is very elaborate, shared with Button and RadioButtion, supporting many styles. It cannot be overridden, the involved classes and methods are all internal.
But luckily you only want to mess with the focus rectangle. All you have to do is convince the control that it should not show it. That's very easy, add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox.
using System.Windows.Forms;
class MyCheckBox : CheckBox {
protected override bool ShowFocusCues {
get { return false; }
}
}
I'm refactoring a body of code, looking through it all, line by line.
I came across an event handler:
private void mnuUpdate_Click(object sender, EventArgs e)
...and, not recognizing which menu item called this (the menu item names do not always match their labels, or even come close), was curious.
The main menu on the form has no such menu item among its children.
I r-clicked the event handler, selected "Find Usages*" and was led here:
this.mnuUpdate.Text = "Update";
this.mnuUpdate.Click += new System.EventHandler(this.mnuUpdate_Click);
(This is an antedeluvial app that predates .NET's partial class goodness, so this is in the same file)
On the form in the designer, when I select "mnuUpdate" from the properties page combobox, the mainMenu on the form disappears altogether.
How can I track down this fugitive menu item? There is no popupMenu or contextMenu on the form, just the mainMenu control...???
The only other usage is:
if (ResetConnectionFetchForm)
mnuUpdate_Click(sender, e);
Is it possible that this is simply a phantom menu item that should be converted into a "regular old" method?
UPDATE
As the most intelligent George used to say, "Curiouser and Curiouser." Now I find this:
public void btnCancel_Click(object sender, EventArgs e)
...and though it is called from seven places in the code, there is no btnCancel on the form...It is a "fake" button click event. Oh my Lanta!!!
So, I replaced it with a parameterless private method with the exact same code (it didn't use either sender or event args).
If the cat who wrote this cockamamie glob of fruitcake-battered spaghetti was deliberately trying to drive the next cat (me) crazy, it's working pretty well, and would make a good Poe-style story or Hitchcock-style flick.
...I see...Dead Code!!!
Okay, mystery solved. mnuUpdate is dynamically added (conditionally) to mnuSetup (which is a top level menu item with the Text property "Fetch") like so:
if (!mnuSetup.MenuItems.Contains(mnuUpdate))
{
mnuSetup.MenuItems.Add(mnuUpdate);
UpdateMenuItemSelectable = true;
}
I reckon selecting mnuUpdate from the combobox in the form's Properties page is because there is no visual representation to show at that point.
Selecting "mnuSetup" highlights the "Fetch" menu item, but selecting "mnuUpdate" causes it all to scurry away faster than cockroaches from the light.
So the bizarre thing about it now is: why is the menu item not dynamically created as necessary, instead of being explicitly created and then dynamically added; seems like a strange way for a cat to skin a cat.
I'd suggest you turn it into "regular old menu" so someone else doesn't waste time figuring it out.
Me - I would have thought it obsolete code because it doesn't have a Handles clause.
You can use .Visible and .Enabled to control what the user sees.
How would you prevent the user from adding or removing lines in a TextBox? By that I mean, if I set the text in a textbox to 7 lines with some text, how can I make sure that it will always be 7 lines of text there? The user must be able to edit those lines like usual, but not remove a line entirely, and not add any new ones.
Would have to take into account both keyboard input and also things like cut and paste, etc.
Any good ideas?
Reason: I want to make a file renamer kind of like Oscar's Renamer. You give it a folder, and it loads the filenames into a textbox where you can do changes pretty much like you do in a text editor. When you are happy with your changes, you write them back. Reason for constant n lines in textbox is then of course that line n is the name of file n. Adding a new line shouldn't be allowed since you only have those files in that folder. Removing a line should also not be allowed since you would then be missing a name for a file.
Why go through the trouble of making something like this when it already exists? Well, I am curious to see if I could do it, and thought it could be a nice excercise to learn a few things along the way. Since it has some interesting problems that needs to be solved. Like this one :) There are also some features I think are lacking in that Oscar's Renamer. So... to sum up: I'm doing it to learn and to try make an even better version of it. I know fully well that I may just as well fail completely though, or just never finish it :p But that is another story. I want to learn
You have the wrong interface then for the data. In this case, you should have a fixed number of textboxes, one for each line of data. This would allow the user to modify the contents of each line, but not remove a line or add a line.
Trying to make a multi-line textbox do this will be maddening at best, since you will have to determine when a new line is added/removed and then kill the change.
Why not use a Listbox instead?
I'd probably let the user do what they want on the textbox, then add validation to show the user there is an error when they go above 7 lines (e.g. red-outline and tooltip or something). If they are under 7 lines, no problem, add them when you come to process that data.
What is the specific reason you want the always 7 lines? Like casperOne said, maybe the interface you're using isn't ideal to your needs.
One possible way of doing this is to sub-class the Textbox control and override the winProc method. This method handles all window messages pumped to the windowed control (Textbox in your case). You could monitor the use of the backspace and delete keys and carat position and discard the key strokes that attempt to remove carriage return line feed sequences. And provide the user with an interactive alert that tells them why they cannot remove entire lines.
Doing it this way gives you complete control and is the lowest level way to see all input that is coming into your Textbox control. You can intercept certain messages and discard them, the ones that you want to allow just pass them through into the base class method. Such as if the user highlights all lines and hits the delete key. There is other event handlers that you can use to intercept keyboard input but they have some limitations, the winProc will allow you to check any message directed to the control including delete, backspace copy and paste etc, mouse clicks etc.
Sample:
public class myCustomTextBox : TextBox
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 770) // window paste message id
{
string clipBoardData = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
handlePasteEvent(clipBoardData);
}
else
{
base.WndProc(ref m);
}
}
private void handlePasteEvent(string pasteData)
{
// process pasted data
}
}
Since you already have stated that this is a learning project, a see if I can do it-project, I think you should throw in some WPF into it. A listbox with a itemtemplate would solve this very nicely.
If that's out of the way, I would consider using a datagrid instead of a textbox.
Load your data in a editable DataGrid instead of a TextBox, that should make things much more simple, and also you can pick which columns are editable and which are not.
Here's an example that uses a DataGridView and simulates your textbox:
The grid lines are hidden.
The headers of columns and rows are hidden.
The background color is the same color a TexBox has.
Add a DataGridView to a form and use the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
this.dataGridView1.BackgroundColor = SystemColors.Window;
this.dataGridView1.BorderStyle = BorderStyle.None;
this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.ColumnHeadersVisible = false;
this.dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
dataGridView1.DataSource = Directory.GetFiles(Environment.CurrentDirectory).Select(f => new FileEdit { FileName = Path.GetFileName(f) }).ToList();
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
dataGridView1.BeginEdit(true);
}
}
class FileEdit
{
public string FileName { get; set; }
}
}