单击按钮后关注textBox

min*_*234 1 c# textbox winforms

好的.因此,对于我的大学课,我必须使用Windows Forms(使用更高要求的版本.Split())在C#中创建一个简单的计算器.得到了textBox一些buttons.所以我可以将数字3插入到a中textBox然后我想按下一个button+在我的数字后添加一个符号.是的,它做到了.但是,如果我想回去写东西,我textBox必须再次点击.

现在我找到了使用.Focus()或使用的答案.Select(),但它们有效,但并不像我希望他们那样做,因为它们还标记整体,textBox.text就像你用鼠标选择它一样,蓝色,如果我按另一个数字然后我会删除所有内容textBox.有没有办法在不标记整个文本的情况下执行此操作?

C4d*_*C4d 5

而不仅仅是使用.Focus()go来进行这种组合:

// Set focus to control
txtbox.Focus();
// Set text-selection to end
txtbox.SelectionStart = txtbox.Text.Length == 0 ? 0 : txtbox.Text.Length -1;
// Set text-selection length (in your case 0 = no blue text)
txtbox.SelectionLength = 0
Run Code Online (Sandbox Code Playgroud)

要么

// Set focus to control
txtbox.Focus();

// Check if text is longer then 0
if(txtbox.Text.Length > 0)
{
    // Set text-selection to end
    txtbox.SelectionStart = txtbox.Text.Length -1;
    // Set text-selection length (in your case 0 = no blue text)
    txtbox.SelectionLength = 0
}
Run Code Online (Sandbox Code Playgroud)

两种方式都是一样的.在第一行中,我text-length == 0在第二行检查就地.

在第二个中,我使用了经典if陈述.