[英]Failing to convert TextBox_TextChanged() in C++ To C#
I was working on C++ with Juce Library for few months. 我使用Juce Library开发C ++已有几个月。 I had written a code in my project where the formatting of textbox was modified to only hexadecimal values with few features:
我在项目中编写了一个代码,其中文本框的格式被修改为仅具有一些功能的十六进制值:
Demonstratation: 演示:
12 ab 32 a5 64 12 ab 32 a5 64
Now if my cursor is at the end and i go on pressing backspace, it shud remove the values as it happens in a general text box. 现在,如果我的光标位于末尾,并且我继续按Backspace键,它将删除一般文本框中发生的值。
Now If my cursor is at the beginning of a5, and i press "delete key", the value should become like: 现在,如果我的光标位于a5的开头,并且我按了“删除键”,则该值应变为:
12 ab 32 56 4 12腹32 56 4
If my cursor is at the end of a5 and i press the 'delete key" nothing should happen. while entering the values space bar should not let spacing bw two values. Only af and 0-9 should be allowed to enter. 如果我的光标位于a5的末尾,并且我按了'delete键',则什么也不会发生。在输入值时,空格键不应让bw的两个值隔开。只能输入af和0-9。
Code in C++ here: 这里的C ++代码:
void CMSP430CommPanel::textEditorTextChanged (TextEditor& editor)
{
if(&editor == m_texti2cWrite)
{
int count = 0;
int location;
String text1 = m_texti2cWrite->getText();
String text = m_texti2cWrite->getText().removeCharacters(" ");
String hexString = String::empty;
int countCaret = m_texti2cWrite->getCaretPosition();
for(int i=0; i < text.length(); i++)
{
hexString = hexString + String (&text[i], 1);
if((i+1) % 2 == 0)
{
if(i != text.length()-1)
{
hexString = hexString + T(" ");
count ++;
}
}
count ++;
}
m_texti2cWrite->setText(hexString,false);
if(text1.length() == m_texti2cWrite->getCaretPosition())
{
m_texti2cWrite->setCaretPosition(count);
}
else
{
m_texti2cWrite->setCaretPosition(countCaret);
}
}
}
I want the same thing to work in my WPF application. 我希望同一件事在我的WPF应用程序中起作用。 Lets say the general implementation of the same code in C#.
可以说C#中相同代码的一般实现。
please help!!! 请帮忙!!!
Try this (TextChanged-Event of your TextBox): 尝试以下操作(您的TextBox的TextChanged-Event):
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox m_texti2cWrite = (TextBox)sender;
int count = 0;
string text1 = m_texti2cWrite.Text;
string text = m_texti2cWrite.Text.Replace(" ", string.Empty);
string hexString = string.Empty;
int countCaret = e.Changes.ToList()[0].Offset;
for (int i = 0; i < text.Length; i++)
{
hexString += text[i];
if ((i + 1) % 2 == 0)
{
if (i != text.Length - 1)
{
hexString = hexString + " ";
count++;
}
}
count++;
}
m_texti2cWrite.Text = hexString;
if (text1.Length == countCaret)
{
m_texti2cWrite.Select(count, 0);
}
else
{
if (e.Changes.ToList()[0].RemovedLength == 0)
{
m_texti2cWrite.Select(countCaret + 1, 0);
if (string.IsNullOrWhiteSpace(hexString.Substring(countCaret, 1)))
m_texti2cWrite.Select(countCaret + 2, 0);
}
else
{
m_texti2cWrite.Select(countCaret, 0);
if (string.IsNullOrWhiteSpace(hexString.Substring(countCaret, 1)))
m_texti2cWrite.Select(countCaret + 1, 0);
}
}
}
}
EDIT (only accept Digits, ControlKeys or af): 编辑(仅接受数字,ControlKey或af):
Add this method: 添加此方法:
private Boolean IsTextAllowed(String text) { string acceptedChars = "ABCDEFabcdef"; foreach (Char c in text.ToCharArray()) { if (Char.IsDigit(c) || Char.IsControl(c) || acceptedChars.Contains(c)) continue; else return false; } return true; }
Add the TextBox_PreviewTextInput-Event to your TextBox 将TextBox_PreviewTextInput-Event添加到您的TextBox
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = !IsTextAllowed(e.Text); }
public class CMSP430CommPanel
{
//C++ TO C# CONVERTER WARNING: The original C++ declaration of the following method implementation was not found:
public void textEditorTextChanged(TextEditor editor)
{
if (editor == m_texti2cWrite)
{
int count = 0;
int location;
string text1 = m_texti2cWrite.getText();
string text = m_texti2cWrite.getText().removeCharacters(" ");
string hexString = string.empty;
int countCaret = m_texti2cWrite.getCaretPosition();
for (int i = 0; i < text.Length; i++)
{
hexString = hexString + (string)(text[i], 1);
if ((i + 1) % 2 == 0)
{
if (i != text.Length - 1)
{
hexString = hexString + T(" ");
count++;
}
}
count++;
}
m_texti2cWrite.setText(hexString,false);
if (text1.Length == m_texti2cWrite.getCaretPosition())
{
m_texti2cWrite.setCaretPosition(count);
}
else
{
m_texti2cWrite.setCaretPosition(countCaret);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.