Sign in to follow this  
chrisliando

[.net] How to lock and unlock keyboard input?

Recommended Posts

I have 2 Multiline mode textBoxes containing text files and 4 buttons in my application. The 2 textBoxes are Source textBox and Destination textBox. The Source textBox is read-only. The buttons are Edit, Append, Insert and Delete. If users click Append, the selected text in Source textBox will be appended to the last line of Destination textBox. If users click Insert then the selected text in Source textBox will be inserted on the active cursor or on the selected part of Destination textBox. If users click Delete the selected text in Destination textBox will be deleted. BEFORE USERS CLICK EDIT, THEY CANNOT TYPE ANYTHING FROM KEYBOARD OR IN OTHER WORD THE KEYBOARD IS LOCKED. AFTER THEY CLICK EDIT, THE KEYBOARD IS UNLOCKED AND THEY ARE FREE TO GIVE INPUT FROM KEYBOARD. 1. How to do that? 2. After I insert on the active cursor / selected text, how to update the cursor position to be placed right after the insertion? Here is the code for insert on selected text or just on active cursor: if (textBox2->SelectedText != String::Empty) { textBox1->Copy(); //copy dari source textBox2->SelectedText = textBox2->SelectedText->Replace(textBox2->SelectedText,Clipboard::GetText()); textBox2->Focus(); textBox2->Select(textBox2->SelectionStart + Clipboard::GetText()->Length, 0); } else { textBox2->Text = textBox2->Text->Insert(textBox2->SelectionStart, textBox1->SelectedText); textBox2->Focus(); textBox2->Select(textBox2->SelectionStart,0); } Both textBox2->Focus(); textBox2->Select(textBox2->SelectionStart + Clipboard::GetText()->Length, 0) and textBox2->Focus; textBox2->Select(textBox2->SelectionStart,0) are not working. Thank you very much.

Share this post


Link to post
Share on other sites
Quote:
Original post by chrisliando
1. How to do that?

Set the Destination textbox to ReadOnly = true before the Edit button is clicked and false when it's clicked.

Quote:
Original post by chrisliando
2. After I insert on the active cursor / selected text, how to update the cursor position to be placed right after the insertion?

I think this covers all the possibilities:


private void button1_Click(object sender, EventArgs e)
{
int val = textBox2.SelectionStart;

if (textBox2.SelectionLength > 0)
{
string before = textBox2.Text.Substring(0, val);
string after = textBox2.Text.Substring(val + textBox2.SelectedText.Length);

if (textBox1.SelectionLength > 0)
{
textBox2.Text = before + textBox1.SelectedText + after;
textBox2.SelectionStart = val + textBox1.SelectedText.Length;
textBox2.SelectionLength = 0;
textBox2.Focus();
}
else
{
textBox2.Text = before + textBox1.Text + after;
textBox2.SelectionStart = val + textBox1.Text.Length;
textBox2.SelectionLength = 0;
textBox2.Focus();
}
}
else
{
if (textBox1.SelectionLength > 0)
{
textBox2.Text = textBox2.Text.Insert(textBox2.SelectionStart, textBox1.SelectedText);
textBox2.SelectionLength = 0;
textBox2.SelectionStart = val + textBox1.SelectedText.Length;
textBox2.Focus();

}
else
{
textBox2.Text = textBox2.Text.Insert(textBox2.SelectionStart, textBox1.Text);
textBox2.SelectionLength = 0;
textBox2.SelectionStart = val + textBox1.Text.Length;
textBox2.Focus();
}

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this