簡體   English   中英

如果光標位於特定的文本框中,請單擊按鈕僅刪除該文本框文本

[英]If the cursor is in a specific textBox, delete only that textBox text with a button click

我正在構建一個簡單的計算器。 我有三個textBox:textBox1(第一個操作數),textBox2(第二個操作數)和textBox3(結果)。 我有許多可以執行的操作數函數。 我也有一個按鈕,它將清除所有字段以及其他功能。

僅當光標位於該文本框中時,我很難使用按鈕刪除特定文本框中的文本。

例如:如果光標位於textBox1中,則按鈕僅清除該文本框。

任何幫助深表感謝。

謝謝。

Button被點擊的時候,會獲得焦點。

因此,您需要跟蹤哪個TextBoxes 最后獲得了焦點。

為此創建一個類級變量:

TextBox focusedTextBox = null;

現在, 掛鈎與此事件Enter 所有三個事件TextBoxes

private void textBoxes_Enter(object sender, EventArgs e)
{
    focusedTextBox = sender as TextBox;
}

然后,這只會清除您的用戶所在的位置:

private void buttonClearCurrent_Click(object sender, EventArgs e)
{
    if (focusedTextBox != null) focusedTextBox.Text = "";
}

在這種情況下,您必須在文本框中使用Focused屬性。 但是您需要循環以確定哪個文本框是焦點。

喜歡:

var focusedControl;
foreach(var control in this.Controls)
{
    if(control is TextBox)
    {
        if(control.Focused)
        {
           focusedControl = control;
           break;
        }
    }
}

您可以使用事件:“ MouseHover”或“ MouseClick”並設置textBox1.Text =“”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM