C# 在文本框中从右向左键入?
我有一个带面具的货币C# 在文本框中从右向左键入?,c#,.net,winforms,textbox,maskedtextbox,C#,.net,Winforms,Textbox,Maskedtextbox,我有一个带面具的货币文本框。 掩码在文本框中显示为----------- 因此,用户在掩码上键入数字 现在客户说他不想从左到右输入字母。 他想从右到左打字 类似于我们的计算器 现在我尝试将文本框的right更改为left属性,但这对我的原因没有帮助 最后,我不得不处理按键事件来手动改变位置。 我可以改变位置,但在完成逻辑时遇到了困难 下面是我的代码的样子: void Textbx_KeyDown(object sender, KeyEventArgs e) { St
文本框
。
掩码在文本框中显示为-----------
因此,用户在掩码上键入数字
现在客户说他不想从左到右输入字母。
他想从右到左打字
类似于我们的计算器
现在我尝试将文本框的right更改为left
属性,但这对我的原因没有帮助
最后,我不得不处理按键事件来手动改变位置。
我可以改变位置,但在完成逻辑时遇到了困难
下面是我的代码的样子:
void Textbx_KeyDown(object sender, KeyEventArgs e)
{
String temp = T.Text;
string temp2 = T.Text;
int CursorIndex = T.SelectionStart - 1;
for (int i = 0; i <= CursorIndex; i++)
{
if (i == 7)
{
temp2 = temp2.Insert(i, temp[i + 2].ToString());
temp2 = temp2.Remove(i, 2);
//i = i + 2;
}
else if (CursorIndex == i)
{
temp2 = temp2.Remove(i, 1);
temp2 = temp2.Insert(i, temp[i + 1].ToString());
}
else
{
// T.Text = T.Text.Insert(i + 1, "_");
temp2 = temp2.Insert(i, temp[i + 1].ToString());
temp2 = temp2.Remove(i + 1, 1);
}
}
T.Text = temp2;
// T.Text = T.Text.Insert(CursorIndex-1, temp[CursorIndex].ToString());
if (CursorIndex != -1)
T.SelectionStart = CursorIndex - 1;
}
void Textbx\u KeyDown(对象发送方,KeyEventArgs e)
{
字符串温度=T.文本;
字符串temp2=T.Text;
int CursorIndex=T.SelectionStart-1;
对于(int i=0;i,文本框中有一个属性:
T.RightToLeft = RightToLeft.Yes
我已经为您编写了此代码;请尝试:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
string mychar = "000000";
string mtxt;
int mypos = 6;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
maskedTextBox1.Text = mychar;
}
private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
mtxt = mtxt + e.KeyChar;
mypos--;
mychar = mychar.Remove(mypos, mtxt.Length);
mychar = mychar.Insert(mypos, mtxt);
maskedTextBox1.Text = mychar;
}
}
}
使用maskedTextBox尝试此操作
设置TextMaskFormat属性=IncludePrompt
private void maskedTextBox1_Click(object sender, EventArgs e)
{
maskedTextBox1.SelectionStart = maskedTextBox1.Mask.Length + 1;
}
private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != (char) Keys.Back)
{
String a = maskedTextBox1.Text + e.KeyChar;
maskedTextBox1.Text = a.Substring(1, a.Length - 1);
maskedTextBox1.SelectionStart = maskedTextBox1.Mask.Length + 1;
}
}
private void maskedTextBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
maskedTextBox1.Text = "_" + maskedTextBox1.Text;
maskedTextBox1.SelectionStart = maskedTextBox1.Mask.Length + 1;
}
}
嗨,我试过了,但是当我做那个面具的时候,我把它改成了“-.----”从“-----”.所以这里的操作和以前一样,就是从数字到小数点。我想让用户从小数点到单位、十等进行键入。对于键入6.95,光标将位于右侧,用户只需键入695,它将显示为6.95。