زر انقر فوق الزر عند الضغط على إدخال عند تحديث حقل نص في C#

StackOverflow https://stackoverflow.com/questions/4279326

  •  28-09-2019
  •  | 
  •  

سؤال

حسنًا ، أريد أن يتمكن المستخدم من الضغط على Enter لبدء زر النقر أثناء إدخال مربع النص.

لدي الرمز التالي:

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
            if (e.KeyValue == 13)
            {
                button3_Click(sender, e);
            }
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
    }
    private void button3_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            MessageBox.Show("Please enter a value.", "No name entered", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else
        {
            if (listBox1.Items.Contains(textBox1.Text) == true)
            {
                MessageBox.Show("You have tried to enter a duplicate.", "No duplicates allowed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                listBox1.Items.Add(textBox1.Text);
                textBox1.Text = "";
            }
        }


    }

ومع ذلك ، عندما أضغط على إدخال القيمة ، فإن مربع الرسائل يأتي قائلاً "الرجاء إدخال قيمة" حوالي 4 مرات. كيف يمكنني جعل هذا الرمز يجعل button_click يحدث مرة واحدة فقط عند الضغط على إدخال؟

هل هناك طريقة أسهل للقيام بذلك؟

شكرًا لك!

هل كانت مفيدة؟

المحلول

//Create a new button
//Assuming you have a new button named "acceptButton"
//Assuming your form name is "FormName"
FormName.AcceptButton = acceptButton;
//this button automatically is triggered at enter key is pressed
acceptButton += new Click(acceptButton_Click);

void acceptButton_Click(object sender, EventArgs e) {
     button3_Click(sender, e);
}

أو

//Make button3 the one to be activated when enter key is pressed
FormName.AcceptButton = button3;
//so that when enter key is pressed, button3 is automatically fired

نصائح أخرى

أولاً ، أعلم أن هذا منشور قديم للغاية ، ثانياً لماذا يمكنك ببساطة استخدام حدث keyup في مربع النص واتصل بحدث النقر فوق الزر فقط:

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyValue == 13)
    {
        this.Accept_Click(null, null);
    }
}

ما لم أفتقد شيئًا ممكنًا تمامًا ؛-)

كان جيان على حق ، شكرًا لك. الرمز النهائي هو:

        private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyUp);
    }

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyValue == 13)
        {
            AcceptButton = button3;
        }
    }

لا أريد أن أجعلك تشعر بالسوء ، ولكن ماذا كنت تحاول القيام به في textbox_textChanged طريقة؟

أول شيء تريد القيام به هو إزالته. ما تفعله هو إضافة button3_Click إلى KeyUp حدث. في كل مرة يغير النص ، يضيفه مرة أخرى ، و button3_Click سيتم استدعاء الطريقة عدة مرات.

ما تحصل عليه هو على الأرجح رسالة "لقد حاولت إدخال نسخة مكررة". هذا لأن ال button3_Click تسمى الطريقة أكثر ثم مرة واحدة بنفس القيمة (تتم إضافة القيمة لأول مرة ، وعلى المكالمات التالية تحاول إضافة نفس القيمة مرة أخرى).

في أي حال ، حاول إضافة معلومات إلى سؤالك ، من غير الواضح جدًا (!) ويستغرق بعض الوقت لفهمه.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top