Disable Up/Down in textbox?

  • Thread starter Thread starter Tomer
  • Start date Start date
T

Tomer

When the textbox has only one line the Up/Down function as Left/Right, which
is not good for me.


How can I disable the Up/Down key functioning in the TextBox control?

I've tried to manually set back the SelectionStart/Length parameters, but it
resulted flicks in the textbox display.

I'm trying to avoid writing a textbox control from scratch.

Thanks, Tomer.
 
Try this code as well,
Cheers,
Arun.

---------------------------------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace textboxex
{

public class TextBoxEx : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if(e.KeyChar == (char)Keys.Enter)
e.Handled = true;
}
protected override void OnKeyUp(KeyEventArgs e)
{
if(e.KeyCode == Keys.Up)
e.Handled = false;
else
base.OnKeyUp (e);
}
protected override void OnKeyDown(KeyEventArgs e)
{
if(e.KeyCode == Keys.Down)
e.Handled = false;
else
base.OnKeyDown (e);
}
}


public class Form1 : System.Windows.Forms.Form
{
private TextBoxEx textBox1;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.textBox1 = new TextBoxEx();
this.textBox1.Location = new System.Drawing.Point(72, 96);
this.textBox1.Text = "textBox1";
this.textBox1.Multiline = true;
this.Controls.Add(this.textBox1);
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code

private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.Menu = this.mainMenu1;
this.Text = "Form1";

}
#endregion


static void Main()
{
Application.Run(new Form1());
}
}
}
 
Back
Top