求助:C++CLR form窗体应用程序中,如何限制textBox中光标始终在字符串最后。
求助:C++CLR form窗体应用程序中,如何限制textBox中光标始终在字符串最后。无论是按键盘的上、下、左、右还鼠标在字符串中点击,textBox中光标始终在字符串最后,不可以改变输入位置。。。。
程序代码:private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
textBox2->Clear();
if (e->KeyChar == 8) //退格键,不受限制。
return;
//if (e->KeyChar == '\\' || e->KeyChar == '/' || e->KeyChar == ':' || e->KeyChar == '*' || e->KeyChar == '?' || e->KeyChar == '"' || e->KeyChar == '<' || e->KeyChar == '>' || e->KeyChar == '|')
//{
//
// textBox2->Text = "路径名不能包括下列任何字符\r\n\\ / : * ? \" < > |";
//}
if (textBox1->Text == "") //第-次输入硬盘分区,确认硬盘分区存在后,方可输入。。。。。
{
if (e->KeyChar >= 'C' && e->KeyChar <= 'Z' || e->KeyChar >= 'c' && e->KeyChar <= 'z')
{
char _disk_char[4] = {0,':','\\',0};
_disk_char[0] = e->KeyChar >= 'C' && e->KeyChar <= 'Z' ? e->KeyChar :e->KeyChar - 32;
String^ _disk = gcnew String(_disk_char);
if (Directory::Exists(_disk))
textBox1->Text = _disk;
}
e->Handled = true;
textBox1->Select(textBox1->Text->Length, 0);
}
else
{
if (e->KeyChar == ':' && textBox1->Text->Length != 1)
e->Handled = true;
else if (textBox1->Text->Length == 2 && e->KeyChar != '\\')
e->Handled = true;
else if (e->KeyChar == '\\' && textBox1->Text->EndsWith("\\"))
e->Handled = true;
else if (e->KeyChar == '/'|| e->KeyChar == '*' || e->KeyChar == '?' || e->KeyChar == '"' || e->KeyChar == '<' || e->KeyChar == '>' || e->KeyChar == '|')
e->Handled = true;
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
textBox2->Clear();
if (textBox1->Text == "")
{
textBox2->Text = "没有输入任何路径";
}
else
{
String^ path_name = nullptr;
try
{
if (!Directory::Exists(textBox1->Text))
{
textBox2->Text = "开始创建 \"" + textBox1->Text + "\" 目录";
DirectoryInfo^ credir = Directory::CreateDirectory(textBox1->Text);
path_name = credir->FullName;
}
else
{
textBox2->Text = textBox1->Text + "目录已经存在";
}
}
catch (Exception^ e)
{
textBox2->Text +="\r\n目录创建失败\r\n"+ e->Message;
}
finally
{
if(path_name!=nullptr)
textBox2->Text += "\r\n创建目录 \"" + path_name + "\"\r\n请确认是否正确";
}
}
}