请建议如何在文本框中的已删除位置填充空格字符 [英] Please suggest how to stuff a space character at the deleted position in a textbox
本文介绍了请建议如何在文本框中的已删除位置填充空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的专业人士,
我正在设计一个继承自TextBox类的自定义控件。
在这里我已经在keypress事件中捕获了退格键和删除键,并且在那个字符将被删除的位置我想在文本框中填入一个空格字符。
请提供合适的方法。在此先感谢。
Dear Professionals,
I am designing a custom control which inherits from the TextBox class.
In this I have trapped the backspace and delete key in keypress event and at that point where the character is going to be removed I want to stuff a 'space' character into the textbox.
Please suggest suitable methods. Thanks in advance.
推荐答案
您需要处理此事件: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v = vs.110 ).aspx [ ^ ],并跟踪文本框的内容。
You need to handle this event: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.110).aspx[^], and keep track of the content of the textbox.
Here is a complete code of your problem.
public partial class TrapDeleteBackSpaceTextBox : TextBox
{
string m_TextBeforeTheChange;
int m_CursorPosition = 0;
bool m_BackPressed = false;
bool m_DeletePressed = false;
public TrapDeleteBackSpaceTextBox():base()
{
}
protected override void OnKeyDown(KeyEventArgs e)
{
m_TextBeforeTheChange = this.Text;
m_BackPressed = (e.KeyData == Keys.Back) ? true : false;
m_DeletePressed = (e.KeyData == Keys.Delete) ? true : false;
m_CursorPosition++;
base.OnKeyDown(e);
}
protected override void OnTextChanged(EventArgs e)
{
this.SelectionStart = m_CursorPosition;
base.OnTextChanged(e);
}
protected override void OnKeyUp(KeyEventArgs e)
{
if (m_BackPressed || m_DeletePressed)
{
this.Text = m_TextBeforeTheChange;
this.Text = this.Text + " "; //I added Space you an add any charecter
}
base.OnKeyUp(e);
}
}
public partial class NumberBox : MaskedTextBox
{
public NumberBox()
{
this.TextAlign = HorizontalAlignment.Right;
this.Mask = "#####.##";
this.PromptChar = ' ';
this.KeyPress += NumberBox_KeyPress;
this.KeyDown += NumberBox_KeyDown;
}
void NumberBox_KeyDown(object sender, KeyEventArgs e)
{
MaskedTextBox SenderTextBox = (MaskedTextBox)sender;
if (e.KeyCode == Keys.Enter)
{
String NumberBoxMask = SenderTextBox.Mask;
String NumberBoxText = SenderTextBox.Text;
Int32 DecimalPointLocation = NumberBoxMask.IndexOf('.');
/* Storing the Length of the mask
before the decimal point location */
Int32 IntegerLength = StrLeft(NumberBoxMask, DecimalPointLocation).Length;
/* Storing the Length of the mask
after the decimal point location */
Int32 DecimalLength = StrRight(NumberBoxMask, NumberBoxMask.Length - (DecimalPointLocation + 1)).Length;
Double aDoubleNum = 0.00;
/* Storing the integer part in a string.
Even if all numbers are deleted in the
integer part, we still get spaces
to the extent of the integer part length atleast */
String strInteger = NumberBoxText.Substring(0, IntegerLength);
/* Storing the decimal part in a string. If numbers are deleted from the
decimal portion then an error occurs, hence applied this logic,
though looks absurd, I accept ...
(Better logic to handle this, Welcome) */
String strDecimal = "".PadRight(DecimalLength, '0');
for (int mi = DecimalLength; mi > 0; mi--)
{
try
{
strDecimal = NumberBoxText.Substring(NumberBoxMask.Length - DecimalLength, mi);
break;
}
catch { }
}
/* Removing the spaces if any in
between numbers in the integer part */
for (int mi = 0; mi < strInteger.Length; mi++)
{
if (strInteger[mi] == ' ')
strInteger = strInteger.Remove(mi, 1);
}
/* Removing the spaces if any in
between numbers in the decimal part */
for (int mi = 0; mi < strDecimal.Length; mi++)
{
if (strDecimal[mi] == ' ')
strDecimal = strDecimal.Remove(mi, 1);
}
/* Arriving the finally formatted Double number. */
String FormattedNum = "";
FormattedNum = strInteger.Trim().PadLeft(IntegerLength, ' ') + "." + strDecimal.Trim().PadRight(DecimalLength, '0');
SenderTextBox.Text = FormattedNum;
// Just check if it is valid Double type digit
try
{
aDoubleNum = Double.Parse(FormattedNum);
e.Handled = true;
}
catch
{
MessageBox.Show("Invalid Value);
e.Handled = false;
}
}
}
public void NumberBox_KeyPress(object sender, KeyPressEventArgs e)
{
/* This logic is as per suggestion from */ <a href="http://www.codeproject.com/Questions/869033/Please-guide-me-how-to-format-the-textbox-to-take?arn=0">Please guide me how to format the textbox to take input of double values.</a>[<a href="http://www.codeproject.com/Questions/869033/Please-guide-me-how-to-format-the-textbox-to-take?arn=0" target="_blank" title="New Window">^</a>]
MaskedTextBox SenderTextBox = (MaskedTextBox)sender;
if (e.KeyChar == '.')
{
Int32 PositionOfDecimalPoint = SenderTextBox.Text.IndexOf('.');
PositionOfDecimalPoint++;
SenderTextBox.SelectionStart = PositionOfDecimalPoint;
SenderTextBox.SelectionLength = 0;
e.Handled = true;
}
}
private String StrLeft(String mstr, int mlen)
{
return mstr.Substring(0, mlen);
}
private String StrRight(String mstr, int mlen)
{
return mstr.Substring((mstr.Length - mlen), mlen);
}
}
这篇关于请建议如何在文本框中的已删除位置填充空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文