How to make new event when hover on a text in RichTextBox? - c#

I have a RichTextBox that contains a string, e.g: "Hello", and I want to create a new event when I hover the mouse over the word "Hello", or to simplify that, showing a message box when hover on the word "Hello". So how to achieve that?

First off let's define a method that gets the word nearest to the cursor:
public static class Helper
{
public static string GetWordUnderCursor(RichTextBox control, MouseEventArgs e)
{
//check if there's any text entered
if (string.IsNullOrWhiteSpace(control.Text))
return null;
//get index of nearest character
var index = control.GetCharIndexFromPosition(e.Location);
//check if mouse is above a word (non-whitespace character)
if (char.IsWhiteSpace(control.Text[index]))
return null;
//find the start index of the word
var start = index;
while (start > 0 && !char.IsWhiteSpace(control.Text[start - 1]))
start--;
//find the end index of the word
var end = index;
while (end < control.Text.Length - 1 && !char.IsWhiteSpace(control.Text[end + 1]))
end++;
//get and return the whole word
return control.Text.Substring(start, end - start + 1);
}
}
In order to raise MouseMove event ONLY if the cursor is above RichTextBox and the nearest word is "Hello" you'll need to define your own control deriving from RichTextBox and override the OnMouseMove method, and use it in your form instead RichTextBox:
public class MyRichTextBox : RichTextBox
{
protected override void OnMouseMove(MouseEventArgs e)
{
//get the word under the cursor
var word = Helper.GetWordUnderCursor(this, e);
if (string.Equals(word, "Hello"))
{
//let RichTextBox raise the event
base.OnMouseMove(e);
}
}
}
However, in my opinion, it's better to let the RichTextBox raise the MouseMove event normally and take action only if conditions are met. In order to do that you only need to register MouseMove handler and check the conditions:
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
var control = sender as RichTextBox;
//get the word under the cursor
var word = Helper.GetWordUnderCursor(control, e);
if (string.Equals(word, "Hello"))
{
//do your stuff
}
}

I think you can use the Cursor class to achieve this. Some people tried to achieve similar things. Have a look here.

Make sure you have an event 'richTextBox1_MouseHover' wired to the hover of your Rich Text Box.
private void richTextBox1_MouseHover(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}

Related

Hightlight Listbox item on mouse over event

I am attempting to change a listview item's background colour when a mouse hovers over it
I have a mouse hover event, but how can I add a "highlight" effect upon a mouse hovering over the item?
private void pinnedAppsListBox_MouseHover(object sender, EventArgs e)
{
}
Use this:
private void pinnedAppsListBox_MouseHover(object sender, EventArgs e){
Point point = pinnedAppsListBox.PointToClient(Cursor.Position);
int index = pinnedAppsListBox.IndexFromPoint(point);
if (index < 0) return;
//Do any action with the item
pinnedAppsListBox.GetItemRectangle(index).Inflate(1,2);
}
Go to the ListView's ItemMouseHover event and add then set the property "BackColor" of the Item.
private void listView1_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
e.Item.BackColor = Color.Black;
}
Declare this Global variable
Use this Listview Item variable to keep track of what item was hovered on
ListViewItem lvHoveredItem;
Set the following function to turn on DoubleBuffering for your control to prevent flickering:
public static void SetDoubleBuffering(System.Windows.Forms.Control control, bool value)
{
System.Reflection.PropertyInfo controlProperty = typeof(System.Windows.Forms.Control)
.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
controlProperty.SetValue(control, value, null);
}
Where your control is loaded call this function
SetDoubleBuffering(lvTaskList, true);
Then use this code in the mousemove event of your listview
private void lvTaskList_MouseMove(object sender, MouseEventArgs e)
{
//Set the Color you want the list Item to be when mouse is over
Color oItemColor = Color.Lavender;
Color oOriginalColor = Color.blue; //Your original color
//get the Item the Mouse is currently hover
ListViewItem lvCurrentItem = lvTaskList.GetItemAt(e.X, e.Y);
if ((lvCurrentItem != null) && (lvCurrentItem != lvHoveredItem))
{
lvCurrentItem.BackColor = oItemColor;
if(lvHoveredItem != null)
{
lvHoveredItem.BackColor = oOriginalColor ;
}
lvHoveredItem = lvCurrentItem;
return;
}
if (lvCurrentItem == null)
{
if (lvHoveredItem != null)
{
lvHoveredItem.BackColor = oOriginalColor;
}
}
}
You can also add the MouseLeave Event
private void lvTaskList_MouseLeave(object sender, EventArgs e)
{
Color oOriginalColor = Color.Blue; //Your original color
//When the mouse leave the control. If a ListViewItem was highlighted then set it's original color back
if (lvHoveredItem != null)
{
lvHoveredItem.BackColor = oOriginalColor ;
}
lvHoveredItem = null;
}
If you're using a ListBox, it is quite more difficult to handle, you will need to set a MouseHover event for the ListBox and determine which item is being hovered on and then draw it manually.
See this answer.
However if you're using a ListView, you can easily add an ItemMouseHover event like this:
private void pinnedAppsListView_MouseHover(object sender, EventArgs e)
{
e.Item.BackColor = Color.Lime;
}
I have seen this question many times without a good answer.
There is no good answer that I know, but, I did it, using some hints elsewhere.
I did this using Lazarus, but you should be able to adapt it to your language.
Get the item. You may want to set variables to catch these individually.
You may also want to get the state of your mouse buttons at mousedown first.
If (ListView.GetItemAt(X,Y) <> nil) then // do this with an if else
// Next, you can get the bounding rect:
ListView.GetItemAt(X,Y).DisplayRect(drSelectBounds);
//Option: If Button up or down then
// you may have to catch this elsewhere, such as for a drag operation.
// Create and set a boolean variable:
HighLightOn := True;
ListView.Repaint; // clears previous hightlights
ListView.Canvas.Brush.Color := clBtnFace; // or your color of choice
ListView.Canvas.FillRect(Rect);
// If you are moving around in an area where GetItem is nil,
// then do this to stop flicker and remove the highlight:
If (ListView.GetItemAt(X,Y) = nil) // do this with an if else
If HighLightOn then
begin
SelectedList.Repaint;
HighLightOn := False;
end;
// If a highlight gets left behind,
// you may need to repeat this elsewhere, such as in a component exit.
// This is the basic gist of the issue.
// There can be a lot of options or things to look for,
// so you code could get more complicated.
// I am not suggesting this is the best way to implement it,
// but it is easy. Part of this code only works inside your app!

Did "MouseUp" event change value of NumericUpDown?

I need to determine if the value of a NumericUpDown control was changed by a mouseUp event.
I need to call an expensive function when the value of a numericupdown has changed. I can't just use "ValueChanged", I need to use MouseUp and KeyUp events.
Basically, I need to know:
Did the value of the numericUpDown change when the user let go of the
mouse? If any area which is not highlighted in red is clicked, the
answer is no. I need to IGNORE the mouse up event, when ANYWHERE but the red area is clicked.
How can I determine this by code? I find events a little confusing.
This will fire when the user releases the mouse button. You might want to investigate which mousebutton was released.
EDIT
decimal numvalue = 0;
private void numericUpDown1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && numvalue != numericUpDown1.Value)
{
//expensive routines
MessageBox.Show(numericUpDown1.Value.ToString());
}
numvalue = numericUpDown1.Value;
}
EDIT 2
This will determine if the left mousebutton is still down, if it is exit before performing expensive routine, doesn't help with keyboard button down.
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
{
return;
}
//expensive routines
}
Edit 3
How to detect the currently pressed key?
Will help solve the Any key down, Though I think the only ones that matter are the arrow keys
Problem - I need to IGNORE the mouse up event, when ANYWHERE but the red area is clicked.
Derive a custom numeric control as shown below. Get the TextArea of the Numeric Control and ignore the KeyUp.
class UpDownLabel : NumericUpDown
{
private Label mLabel;
private TextBox mBox;
public UpDownLabel()
{
mBox = this.Controls[1] as TextBox;
mBox.Enabled = false;
mLabel = new Label();
mLabel.Location = mBox.Location;
mLabel.Size = mBox.Size;
this.Controls.Add(mLabel);
mLabel.BringToFront();
mLabel.MouseUp += new MouseEventHandler(mLabel_MouseUp);
}
// ignore the KeyUp event in the textarea
void mLabel_MouseUp(object sender, MouseEventArgs e)
{
return;
}
protected override void UpdateEditText()
{
base.UpdateEditText();
if (mLabel != null) mLabel.Text = mBox.Text;
}
}
In the MainForm, update your designer with this control i.e. UpDownLabel:-
private void numericUpDown1_MouseUp(object sender, MouseEventArgs e)
{
MessageBox.Show("From Up/Down");
}
Referred from - https://stackoverflow.com/a/4059473/763026 & handled the MouseUp event.
Now, use this control instead of the standard one and hook on the
KeyUp event. You will always get the KeyUp event from the Up/Down button only i.e. RED AREA when you click the
spinner [Up/Down button, which is again a different control derived
from UpDownBase].
I think you should use Leave event that when the focus of NumericUpDown control gone, it would called.
int x = 0;
private void numericUpDown1_Leave(object sender, EventArgs e)
{
x++;
label1.Text = x.ToString();
}

WPF (with C#) TextBox Cursor Position Problem

I have a WPF C# program where I attempt to delete certain characters from a text box at TextChanged event. Say, for instance, the dollar sign. Here is the code I use.
private void txtData_TextChanged(object sender, TextChangedEventArgs e)
{
string data = txtData.Text;
foreach( char c in txtData.Text.ToCharArray() )
{
if( c.ToString() == "$" )
{
data = data.Replace( c.ToString(), "" );
}
}
txtData.Text = data;
}
The problem I have is that whenever the user enters $ sign (Shift + 4), at the TextChanged event it removes the $ character from the textbox text alright, but it also moves the cursor to the BEGINNING of the text box which is not my desired functionality.
As a workaround I thought of moving the cursor the the end of the text in the text box, but the problem there is that if the cursor was positioned at some middle position then it would not be very user friendly. Say, for instance the text in the textbox was 123ABC and if I had the cursor after 3, then moving the cursor to the end of the text would mean that at the next key stroke user would enter data after C, not after 3 which is the normal functionality.
Does anybody have an idea why this cursor shift happens?
Its not an answer to your question, but probably a solution for your problem:
How to define TextBox input restrictions?
If it is overkill for you, set e.Handled = true for all characters you want to avoid in PreviewKeyDown (use Keyboard.Modifiers for SHIFT key) or PreviewTextInput.
Try TextBox.CaretIndex for restoring cursor position in TextChanged event.
Hope it helps.
You can use the Select function of TextBox to change the cursor position.
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Text = textBox1.Text.Replace("$", "");
textBox1.Select(textBox1.Text.Length, 0);
}
You can see more about Position the Cursor on the MSDN
You can use the SelectionStart property of the textbox. Probably something along these lines should work:
private void txtData_TextChanged(object sender, TextChangedEventArgs e)
{
var pos = txtData.SelectionStart;
string data = txtData.Text.Replace("$", "");
txtData.Text = data;
txtData.SelectionStart = pos;
}
You can try Regular Expression
Sample
1) Use PreviewTextInput="CursorIssueHandler" in .xaml file
2) In your .cs file ,write the below code:
private void CursorIssueHandler(object sender, TextCompositionEventArgs e)
{
var TB = (sender as TextBox);
Regex regex = new Regex("[^0-9a-zA-Z-]+");
bool Valid = regex.IsMatch(e.Text);
//System.Diagnostics.Debug.WriteLine(Valid); // check value for valid n assign e.Handled accordingly your requirement from regex
e.Handled = Valid;
}

How to determine whether TextChanged was triggered by keyboard in C#?

I have a method
private void textBoxPilot_TextChanged(object sender, TextChangedEventArgs e)
{ ... }
where the textbox in question takes a search string from the user and populates a ListBox with the results on every keystroke.
Subsequently, when an item is picked from the ListBox, I would like the choice reflected in the same Textbox. However, I don't want to trigger the search mechanism, which would cause the Listbox to forget its selection.
How can I determine whether the TextChanged event was triggered by the user (via they keyboard or maybe copy/paste) or by another method using textBoxPilot.Text = "Pilot name";?
Thanks.
bit of a hack, but....
public class MyForm : Form
{
private bool _ignoreTextChanged;
private void listView1_SelectionChanged( object sender, EventArgs e )
{
_ingnoreTextChanged = true;
textBoxPilot.Text = listView1.SelectedValue.ToString(); // or whatever
}
private void textBoxPilot_TextChanged( object sender, TextChangedEventArgs e )
{
if( _ignoreTextChanged )
{
_ignoreTextChanged = false;
return;
}
// Do what you would normally do.
}
}
A disabled control will not fire a event. So two options are either always disable update the text then re-enable or create a derived class wrapper (using this method you could still do data binding)
class myClass : TextBox
{
public virtual string TextWithoutEvents
{
get
{
return base.Text;
}
set
{
bool oldState = Enabled;
Enabled = false;
base.Text = value;
Enabled = oldState;
}
}
}
If the user selects "Pilot name" from the list, you set the text box to "Pilot name". This will cause the list box to select "Pilot name". So the selection should be kept. You just have to break the recursion.
In my scenario where user has to type in text to trigger auto-complete and we didn't want a re-trigger when the auto-complete changes the text again, I used the text lengths. This won't work if user copy/pastes and therefore adds more than 1 character at a time with the keyboard.
private void HandleTextChanged(object sender, TextChangedEventArgs e){
var oldText = e.OldTextValue;
var newText = e.NewTextValue;
// Assuming text changed from keyboard is always 1 character longer,
// ignore this text changed event if new text > 1 character longer.
if (newText.Length > oldText.Length + 1) {
return;
}
...
}
In your scenario, if you always know the values you want to skip, then you could check for them instead:
if (newText == "Pilot name") {
return;
}
or
if (myListOfNamesToIgnore.Contains(newText)) {
return;
}

Keep selection when clicking into textbox

I need to have the text in a TextBox become selected when a user clicks into the box. If the text is already selected, it needs to be a regular cursor. So on the click event of all the textboxes I have this code:
TextBox t = (TextBox)sender;
bool alreadyselected = t.SelectedText == t.Text;
if (!alreadyselected) t.SelectAll();
the problem is, by the time the click event is reached, t.SelectedText is empty
so the full text always becomes selected even when clicking multiple times
I would appreciate a solution that can be for all the textboxes at once if possible
You're correct, the default Click for the TextBox is changing the position of the caret and thus clearing any selected text. But you can restore it.
First add 2 int vars to store the selection Start and Length and initialize Start as -1 to signal not set:
private int SelectedStart = -1;
private int SelectedLength = 0;
then make a handler for the TextBox's Leave event and save the Start and Length for the currently selected text when we lose focus.
private void textBox1_Leave (object sender, EventArgs e)
{
SelectedStart = textBox1.SelectionStart;
SelectedLength = textBox1.SelectionLength;
}
Finally, make a handler for the TextBox's Click event and, if we previously saved the Start and Length, restore them to the TextBox and then set Start to -1 to signal not set again (this allows for normal click behavior within textbox when it is focused).
private void textBox1_Click (object sender, EventArgs e)
{
if (SelectedStart != -1) {
textBox1.SelectionStart = SelectedStart;
textBox1.SelectionLength = SelectedLength;
SelectedStart = -1;
}
}
Use the Control.Tag property to set a bool flag to select or deselect the TextBox text:
private void TextBox_Click(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.SelectionStart = 0;
// First click will select the text
if (txtBox.Tag == null)
{
txtBox.Tag = true;
txtBox.SelectionLength = txtBox.Text.Length;
}
// Second click will deselect the text
else
{
txtBox.Tag = null;
txtBox.SelectionLength = 0;
}
}

Categories