我有几个textboxes
,我希望光标使用箭头键从一个textbox
移动到另一个{。
我该怎么做?
看起来像这样,水龙头垂直移动也很奇怪。
感谢。
答案 0 :(得分:3)
如果文本框中有一些文字,则以下解决方案有效。
首先创建一个KeyDown事件处理程序:
private void textBoxLeft_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Right))
{
e.Handled = true;
//SendKeys.Send("{TAB}");
textBoxRight.Focus();
}
}
private void textBoxRight_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Left))
{
e.Handled = true;
//SendKeys.Send("+{TAB}");
textBoxLeft.Focus();
}
}
答案 1 :(得分:2)
您可以覆盖表单的ProcessCmdKey函数,并按下按键并关注其中的文本框。
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;
namespace TextBoxes
{
public partial class Form1 : Form
{
List<TextBox[]> _textBoxes;
public Form1()
{
InitializeComponent();
//Getting a 2 dimentional structure to hold textboxes
this._textBoxes= new List<TextBox[]>();
TextBox[] row1 = new TextBox[4];
row1[0] = textBox1;
row1[1] = textBox2;
row1[2] = textBox3;
row1[3] = textBox4;
TextBox[] row2 = new TextBox[4];
row2[0] = textBox5;
row2[1] = textBox6;
row2[2] = textBox7;
row2[3] = textBox8;
TextBox[] row3 = new TextBox[4];
row3[0] = textBox9;
row3[1] = textBox10;
row3[2] = textBox11;
row3[3] = textBox12;
TextBox[] row4 = new TextBox[4];
row4[0] = textBox13;
row4[1] = textBox14;
row4[2] = textBox15;
row4[3] = textBox16;
this._textBoxes.Add(row1);
this._textBoxes.Add(row2);
this._textBoxes.Add(row3);
this._textBoxes.Add(row4);
}
/// <summary>
/// Processes a command key.
/// </summary>
/// <param name="msg">A <see cref="T:System.Windows.Forms.Message"/>, passed by reference, that represents the Win32 message to process.</param>
/// <param name="keyData">One of the <see cref="T:System.Windows.Forms.Keys"/> values that represents the key to process.</param>
/// <returns>
/// true if the keystroke was processed and consumed by the control; otherwise, false to allow further processing.
/// </returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//A key was pressed!
Control activeCtrl = this.ActiveControl;
TextBox txb = activeCtrl as TextBox;
//Handle it only if it was sent when a textbox was focused
if (txb != null)
{
int row;
int column;
//find out which text box is currently active
this.GetTextBoxIndexes(txb, out row, out column);
//change indexes according to key stroke
if (keyData == Keys.Up)
{
row--;
}
else if (keyData == Keys.Down)
{
row++;
}
else if (keyData == Keys.Left)
{
column--;
}
else if (keyData == Keys.Right)
{
column++;
}
//Make sure we are not in negative / out of ranfe index
row = Math.Abs(row + 4) % 4;
column = Math.Abs(column + 4) % 4;
//focus requested text box
TextBox slected = this._textBoxes[row][column];
slected.Focus();
}
//don't forget not to break the chain...
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// Gets the text box indexes.
/// </summary>
/// <param name="txb">The texbox.</param>
/// <param name="row">The out row index.</param>
/// <param name="column">The out column index.</param>
private void GetTextBoxIndexes(TextBox txb, out int row, out int column)
{
row = -1;
column = -1;
for (int rowIdx = 0; rowIdx < this._textBoxes.Count; rowIdx++)
{
TextBox[] currRow = this._textBoxes[rowIdx];
for (int colIdx = 0; colIdx < currRow.Length; colIdx++)
{
TextBox currTextBox = this._textBoxes[rowIdx][colIdx];
if (currTextBox.Equals(txb))
{
row = rowIdx;
column = colIdx;
return;
}
}
}
}
}
}
答案 2 :(得分:2)
创造性地将它从头顶上移开。这将为您提供您想要的。
步骤1创建一个TableLayoutPanel并创建第4列和第5行。删除所有原始文本框和 第2步将此添加到您的代码中。
TextBox[] text = new TextBox[20]; //these are your new textboxes
private void Form1_Load(object sender, EventArgs e)
{
for(int i=0; i<text.Length;i++) text[i] = new TextBox() {Text="0"}; //initialize each textbox
tableLayoutPanel1.Controls.AddRange(text); //add textboxes to the tablelayoutpanel
for(int i = 0; i<tableLayoutPanel1.Controls.Count;i++) //add everything in the tablelayoutpanel to the same keydown event
tableLayoutPanel1.Controls[i].KeyDown+=new KeyEventHandler(tableLayoutPanel1_KeyDown);
}
void tableLayoutPanel1_KeyDown(object sender, KeyEventArgs e)
{ // this moves the box focus up down left or right
if (e.KeyData == Keys.Left)
for (int i = 0; i < text.Length; i++)
{
if (sender.Equals(text[i]))
{ text[i - 1 < 0 ? text.Length - 1 : i - 1].Focus(); break; }
}
else if (e.KeyData == Keys.Right)
for (int i = 0; i < text.Length; i++)
{
if (sender.Equals(text[i]))
{ text[i + 1 > text.Length - 1 ? 0 : i + 1].Focus(); break; }
}
else if (e.KeyData == Keys.Up)
for (int i = 0; i < text.Length; i++)
{
if (sender.Equals(text[i]))
{ text[i - 4 < 0 ? i - 4 + text.Length : i - 4].Focus(); break; }
}
else if (e.KeyData == Keys.Down)
for (int i = 0; i < text.Length; i++)
{
if (sender.Equals(text[i]))
{ text[i + 4 > text.Length - 1 ? i + 4 - text.Length : i + 4].Focus(); break; }
}
}
这将允许您使用箭头键向上向上导航,它将使用选项卡修复您的问题,因此选项卡现在将向右移动并且shift + tab将返回。此外,这个解决方案很不错,因为如果你上下移动它会像你直觉所期望的那样循环回来。此外,如果你感觉很兴奋,你现在可以对角移动。
答案 3 :(得分:0)
尝试处理按键事件
→,←可以分别强制标签,移动+标签键。
我只是一个粗略的想法,因为我从未尝试过这个。