|
|
26.1 How can I prevent the beep when enter is hit in textbox?
|
 |
You can prevent the beep when the enter key is pressed in a TextBox by deriving the TextBox and overriding OnKeyPress.
|
public class MyTextBox : TextBox
|
protected override void OnKeyPress(KeyPressEventArgs e)
|
if(e.KeyChar == (char) 13)
|
Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
|
If e.KeyChar = CChar(13) Then
|
26.2 How can I disable the right-click context menu in my textbox?
|
 |
You can set the ContextMenu property of the TextBox to a dummy, empty ContextMenu instance. |
26.3 How do I disable pasting into a TextBox (via Ctrl + V and the context menu)?
|
 |
First set the ContextMenu property of the TextBox to a dummy, empty ContextMenu instance. This will prevent the default context menu from showing. Then, override the ProcessCmdKey method as follows in a TextBox derived class:
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
if(keyData == (Keys.Control | Keys.V))
|
return base.ProcessCmdKey(ref msg, keyData);
|
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
|
If keyData =(Keys.Control | Keys.V) Then
|
Return MyBase.ProcessCmdKey( msg, keyData)
|
26.4 How do I format numbers, dates and currencies in a TextBox?
|
 |
Each type has a ToString method that can be used to accomplished formatting. Also, you can use the String.Format method to format things as well. To format dates, use the ToString member of DateTime. You may want to use the InvariantInfo setting (see below) to get culture-independent strings.
 |
26.5 How do I browse for, and read a text file into an TextBox?
|
 |
You use the Frameworks OpenFileDialog to implement this functionality.
|
private void button1_Click(object sender, System.EventArgs e)
|
OpenFileDialog dlg = new OpenFileDialog();
|
dlg.Title = "Open text file" ;
|
dlg.InitialDirectory = @"c:\" ;
|
dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
|
if(dlg.ShowDialog() == DialogResult.OK)
|
StreamReader sr = File.OpenText(dlg.FileName);
|
string s = sr.ReadLine();
|
StringBuilder sb = new StringBuilder();
|
textBox1.Text = sb.ToString();
|
26.6 How do I send a EM_XXXX message to a textbox to get some value such as line index in C#?
|
 |
There is a protected SendMessage call you can use for this purpose, so you have to derive from TextBox.
|
public MyTextBox : TextBox
|
private const int EM_XXXX = 0x1234;
|
return base.SendMessage(EM_XXXX, 0, 0);
|
(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)
|
26.7 How do I prevent a control from receiving focus when it receives a mouseclick?
|
 |
You can set the control's Enabled property to false. This will prevent clicking but also gives the disabled look.
There is a ControlStyles.Selectable flag that determines whether the control can get focus or not. But it does not appear to affect some controls such as TextBox. But you can prevent the TextBox from getting focus by overriding its WndProc method and ignoring the mouse down.
|
public class MyTextBox : TextBox
|
const int WM_LBUTTONDOWN = 0x0201;
|
protected override void WndProc(ref System.Windows.Forms.Message m)
|
if(m.Msg == WM_LBUTTONDOWN)
|
26.8 How do I make my textbox use all upper (or lower) case characters?
|
 |
Use the CharacterCasing property of the TextBox.
|
textBox1.CharacterCasing = CharacterCasing.Upper;
|
// textBox1.CharacterCasing = CharacterCasing.Lower;
|
26.9 I have a delete key shortcut for my main menu. When my textbox is being edited, pressing delete activates this menu items instead of being processed by the TextBox. How can I get my TextBox to handle this delete?
|
 |
Subclass the TextBox and override its PreProcessMessage method.
|
public class MyTextBox : TextBox
|
const int WM_KEYDOWN = 0x100;
|
const int WM_KEYUP = 0x101;
|
public override bool PreProcessMessage( ref Message msg )
|
Keys keyCode = (Keys)(int)msg.WParam & Keys.KeyCode;
|
if((msg.Msg == WM_KEYDOWN || msg.Msg == WM_KEYUP)
|
&& keyCode == Keys.Delete)
|
return base.PreProcessMessage(ref msg);
|
26.10 How can I use a TextBox to enter a password?
|
 |
Set the TextBox.PasswordChar property for the textbox.
|
textBox1.PasswordChar = '\u25CF';
|
26.11 How can I place a TextBox in overwrite mode instead of insert mode?
|
 |
You can handle the textbox's KeyPress event and if you want the keypress to be an overwrite, just select the current character so the keypress will replace it. The attached sample has a derived textbox that has an OverWrite property and a right-click menu that allows you to toggle this property.
The snippet below shows you a KeyPress handler that automatically does an overwrite if the maxlength of the textbox has been hit. This does not require a derived class.
|
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
|
if(textBox1.Text.Length == textBox1.MaxLength && textBox1.SelectedText == "")
|
textBox1.SelectionLength = 1;
|
26.12 How can I restrict the characters that my textbox can accept?
|
 |
You can handle the textbox's KeyPress event and if the char passed in is not acceptable, mark the events argument as showing the character has been handled. Below is a derived TextBox that only accepts digits (and control characters such as backspace, ...). Even though the snippet uses a derived textbox, it is not necessary as you can just add the handler to its parent form.
|
public class NumbersOnlyTextBox : TextBox
|
public NumbersOnlyTextBox()
|
this.KeyPress += new KeyPressEventHandler(HandleKeyPress);
|
private void HandleKeyPress(object sender, KeyPressEventArgs e)
|
if(!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
|
Public Class NumbersOnlyTextBox
|
AddHandler Me.KeyPress, AddressOf HandleKeyPress
|
Private Sub HandleKeyPress(sender As Object, e As KeyPressEventArgs)
|
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
|
End Class 'NumbersOnlyTextBox
|
26.13 How do I set several lines into a multiline textbox?
|
 |
There are a several of ways to do this. Here are a few:
1) Insert a carriage return-linefeed, "\r\n", between your lines.
|
textBox1.Text = "This is line 1.\r\nThis is line 2";
|
// or textBox1.Text = stringvar1 + "\r\n" + stringvar2;
|
2) Use the Environment.NewLine property. This property is normally set to "\r\n".
|
textBox1.Text = "This is line 1." + Environment.NewLine + "This is line 2";
|
3) Use the Lines property of the TextBox. Make sure you populate your array before you set the property.
|
string[] arr = new string[2];
|
arr[0] = "this is line1";
|
arr[1] = "this is line 2";
|
26.14 When I set a TextBox to Readonly or set Enabled to false, the text color is gray. How can I force it to be the color specified in the ForeColor property of the TextBox.
|
 |
Felix Wu gives this solution in the microsoft.public.dotnet.frameworks.windowsforms newgroup.
You can download a VB.NET sample.
Override the OnPaint event of the TextBox. For example:
|
protected override void OnPaint(PaintEventArgs e)
|
SolidBrush drawBrush = new SolidBrush(ForeColor); //Use the ForeColor property
|
// Draw string to screen.
|
e.Graphics.DrawString(Text, Font, drawBrush, 0f,0f); //Use the Font property
|
Note: You need to set the ControlStyles to "UserPaint" in the constructor.
|
// This call is required by the Windows.Forms Form Designer.
|
this.SetStyle(ControlStyles.UserPaint,true);
|
// TODO: Add any initialization after the InitForm call
|
26.15 How can I make the focus move to the next control when the user presses the Enter key in a TextBox?
|
 |
Add a handler for your TextBox's KeyDown event. (This assumes you set the AcceptsReturn property to False).
Then in your KeyDown eventhandler, have code such as:
|
if (e.KeyCode = Keys.Enter)
|
If e.KeyCode = Keys.Enter Then
|
26.16 How can I make a ReadOnly TextBox ignore the mousedowns so that you cannot scroll the text or set the cursor?
|
 |
You can do this by deriving the TextBox, overriding the WndProc method and ignoring these mousedowns.
|
public class MyTextBox : TextBox
|
protected override void WndProc(ref System.Windows.Forms.Message m)
|
{ // WM_NCLBUTTONDOWN WM_LBUTTONDOWN
|
if(this.ReadOnly && (m.Msg == 0xa1 || m.Msg == 0x201))
|
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
|
' WM_NCLBUTTONDOWN WM_LBUTTONDOWN
|
If Me.ReadOnly AndAlso(m.Msg = &HA1 OrElse m.Msg = &H201) Then
|
|
|
|
|