hey i've created 2 labels(label1 & label2) with 2 key handlers (LEFT arrow key & RIGHT arrow key)...........here when i press RIGHT key, a number counting is displayed in label1 ..........

<<<<<here i need help>>>>
suppose imagine that the count reached 20 at label1.....then when i press LEFT key the count should decrement to 0, which is also should displayed at label1 and then starts the left count from 0, that should be displayed at label2................just like the SEA-SAW game.............

can anyone hep me>>>>>>>>>>>>>>>>

Recommended Answers

All 4 Replies

btw, why do you need a timer in the code?
I did what I thought you want. Tell me if this is it.

int sRight;
        int sLeft;
        public MainForm()
        {
            InitializeComponent();
            this.KeyDown += new KeyEventHandler(MainForm_KeyDown);
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right)
            {
                if (sRight == 0)
                {
                    sLeft = 0;
                    sRight = 0;
                }
                sRight++;
                timer1.Enabled = true;
                timer1.Interval = (200);

            }
            if (e.KeyCode == Keys.Left)
            {
                if (sLeft == 0)
                {
                    sRight = 0;
                    sLeft = 0;
                }
                sLeft++;
                timer1.Enabled = true;
                timer1.Interval = (200);
            }
        }     

        private void timer1_Tick(object sender, EventArgs e)
        {
            if ((sLeft >= 0 && sLeft <= 30) && (sRight >= 0 && sRight <= 30))
            {
                if (sRight > 0)
                    label1.Text = sRight.ToString();
                else
                    label1.Text = "";
                if (sLeft > 0)
                    label2.Text = sLeft.ToString();
                else
                    label2.Text = "";
                timer1.Enabled = false;
            }
        }

BTW: How wil the left (label2) count up? With pressing left arrow key?

PS: Edited the code... now its better working! tell me what to you think...

Mitja

hey here when i press the right key the count that is at the label2 should starts decremented to zero digital by digit which should be displayed at the label2 itself.........and after when it reach zero the left count should starts likewise........imagine when left count is at 20 again we press right key then left count should decrement digit by digit which should be displayed at label1 and when it reach zero then at that time right count should begin............


i put the timer to slow down the counting speed......

Did you want it like a clock, where you count down the right side and when it reaches zero the left side decreases by one then the right side starts counting down from 20 again, etc.?

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
//using System.Linq;
using System.Text;
using System.Threading;
using System.Resources;
namespace MultipleKeys
{
    public partial class MainForm : Form
    {
        int s = 0;
        int lefts = 0;
        int rights = 0;

        public MainForm()
        {
            InitializeComponent();

            //textBox1.SetWatermark("This is the watermark");
        }
        private void MainFormKeyUp(object sender, KeyEventArgs e)
        {   
            
            if (e.KeyCode == Keys.Right)
            {
               
            }
            if (e.KeyCode == Keys.Left)
            {
               
            }

        }

        private void MainFormKeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Right)
            {
                if (lefts < 30 && lefts > -30)
                {
                    lefts++;
                    rights--;
                    timer1.Enabled = true;
                }
                else
                {

                    lefts=0;
                    rights=0;
                    timer1.Enabled = true;
                }

              
            }
            if (e.KeyCode == Keys.Left)
            {
                if (rights  < 30 && rights  > -30)
                {
                    lefts--;
                    rights++;
                    timer1.Enabled = true;
                }
                else
                {

                    lefts = 0;
                    rights = 0;
                    timer1.Enabled = true;
                }

            }
            
        }
        
        //public void SetCount(int s)
        //{
        //    label1.Text = "" + s;
        //    label1.Refresh();
        //}

        private void timer1_Tick(object sender, EventArgs e)
        {
            //++s;

            timer1.Interval = (200); 
                         
                label1.Text = "" + lefts ;
                label1.Refresh();
                label2.Text = "" + rights;
                label2.Refresh();
                timer1.Enabled = false;
           
            
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

      
    }
}

IF I UNDERSTAND YOUR IT WILL, THIS CODE WILL HELP YOU OTHERWISE FEEL FREE ASK YOUR QUERY WITH YOUR PROBLEM

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.