为什么应该使两个按钮可见的C#KeyDown键事件不起作用?

时间:2020-04-04 21:02:55

标签: c# keydown visible

在我的表单上,我有两个按钮(3和4),一旦按下键H或R,这些按钮应该可以显示。但是它不起作用,什么也没发生。我想念什么?

这是我尝试过的。我在另一个项目中使用了这种精确方法,并且效果很好,在这里看不到我的错误。抱歉,如果不清楚,真的很累。

private void Form1_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.H)
                 button3.Visible = button3.Enabled = true;

            if (e.KeyCode == Keys.R)
                 button4.Visible = button4.Enabled = true;

         }

完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int brojk;
        int uvecanje = 1;
        int counter = 20;

        private System.Windows.Forms.Timer timer1;

         private void Form1_Load(object sender, EventArgs e)
        {
            label2.Text = "20";
            counter = 20;
            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 1000;
            timer1.Start();
            label2.Text = counter.ToString();
            textBox1.Text = "20";



        }

         private void button1_Click(object sender, EventArgs e)
         {
             brojk= Convert.ToInt32(label1.Text);
             brojk= brojk + uvecanje;
             label1.Text = brojk.ToString();
         }

         private void button2_Click(object sender, EventArgs e)
         {
             brojk = 0;
             label1.Text = brojk.ToString();

             button4.Visible = false;
             button3.Visible = false;
             timer1.Interval = 1000;
             timer1.Start();
             label2.Text = counter.ToString();

             timer1.Enabled = true;


             string b = textBox1.Text;
             if (b.Length < 1)
             {
                 textBox1.Text = "20";
                 counter = 20;
             }
             else
             {

                 int a = Convert.ToInt32(textBox1.Text);

                 if (a < 0)
                 {
                     counter = 20;
                     textBox1.Text = "20";
                     MessageBox.Show("Uneseno vreme mora biti pozitivno.");

                 }
                 else
                 {
                     counter = a + 1;

                 }
             }
         }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
         {
             if (e.KeyCode == Keys.H)
                 button3.Visible = button3.Enabled = true;

            if (e.KeyCode == Keys.R)
                 button4.Visible = button4.Enabled = true;

         }

         private void button3_Click(object sender, EventArgs e)
         {
             uvecanje = 150;
         }


         private void button4_Click(object sender, EventArgs e)
         {
             Random rand = new Random();
             int x = rand.Next(1, 10000);
             uvecanje = x;
         }

         private void button5_Click(object sender, EventArgs e)
         {
             MessageBox.Show("Od pokretanja ove aplikacije treba da kliknete kolacic sto vise puta i dobijete sto veci rezultat. Ako imate poteskoca mozete pritisnuti h da bi otkljucali hak koji vasim kliktajima dodaje 150 ili r da bi otkljucali hak kojim uvecavate rezultat za random broj. Srecno!");
         }
         private void timer1_Tick(object sender, EventArgs e)
         {

             counter--;
             if (counter == 0)
                 timer1.Stop();
             label2.Text = "0";
             if (counter == 0)
                 MessageBox.Show("Kraj! Vas rezultat: " + brojk.ToString());
             label2.Text = counter.ToString();
             if (counter == 0)
                 button1.Enabled = false;
             if (counter == 0)
                 counter = 20;
            }

    }
}

0 个答案:

没有答案