-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextBoxEx.cs
More file actions
53 lines (44 loc) · 1.01 KB
/
TextBoxEx.cs
File metadata and controls
53 lines (44 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Windows.Forms;
namespace PhatStudio
{
/// <summary>
/// When a TextBoxEx receives focus,
/// all text in the textbox is selected.
/// </summary>
public class TextBoxEx : TextBox
{
private bool alreadyFocused;
public TextBoxEx()
{
}
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
alreadyFocused = false;
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
// Select all text only if the mouse isn't down.
// This makes tabbing to the textbox give focus.
if (MouseButtons == MouseButtons.None)
{
SelectAll();
alreadyFocused = true;
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
// Web browsers like Google Chrome select the text on mouse up.
// They only do it if the textbox isn't already focused,
// and if the user hasn't selected all text.
if (!alreadyFocused && SelectionLength == 0)
{
alreadyFocused = true;
SelectAll();
}
}
}
}