[英]Failing to convert TextBox_TextChanged() in C++ To C#
我使用Juce Library開發C ++已有幾個月。 我在項目中編寫了一個代碼,其中文本框的格式被修改為僅具有一些功能的十六進制值:
演示:
12 ab 32 a5 64
現在,如果我的光標位於末尾,並且我繼續按Backspace鍵,它將刪除一般文本框中發生的值。
現在,如果我的光標位於a5的開頭,並且我按了“刪除鍵”,則該值應變為:
12腹32 56 4
如果我的光標位於a5的末尾,並且我按了'delete鍵',則什么也不會發生。在輸入值時,空格鍵不應讓bw的兩個值隔開。只能輸入af和0-9。
這里的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);
}
}
}
我希望同一件事在我的WPF應用程序中起作用。 可以說C#中相同代碼的一般實現。
請幫忙!!!
嘗試以下操作(您的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);
}
}
}
}
編輯(僅接受數字,ControlKey或af):
添加此方法:
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; }
將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.