Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Keeping a Windows Form dynamic at runtime

$
0
0

Hello All,

I could use some advice, for I've been trying to solve this issue for a long time and with multiple Windows Forms.

How can you keep a Windows Form dynamic and visually/audibly changing (e.g., with labels, pictures, sounds, etc)?

I'm trying to build a Windows Form that tracks and displays the time (from a timer and ticks) AND changes different labels and picture displays (also plays a sound file) at different times.

Here's a portion of the code I'm working with:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Media;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public SoundPlayer play = new SoundPlayer();
        Stopwatch stopWatch = new Stopwatch();
 
        public Form1()
        {
            InitializeComponent();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Start();
            pictureBox1.Show();
            pictureBox2.Hide();
            pictureBox3.Hide();
            label2.Hide();
            label3.Hide();
            textBox1.Hide();
        }

        private void button1_Click(object sender, EventArgs e)        //STARTS STOPWATCH
        {
            stopWatch.Start();
            Go();
        }

        public void Go()
        //WHERE PICTURES/LABELS CHANGE AND SOUNDS PLAY AT DIFFERENT TICKS
        //I'D LIKE FOR THE SOUND FILE TO STOP PLAYING AFTER EACH CLICK OF button_4
        //Button_4 acts like a "Snooze" button in this case
        {
            while (stopWatch.ElapsedTicks > 1)
            {
                if (stopWatch.ElapsedTicks == 100000000)
                {
                    play.Stream = Properties.Resources.alarm;
                    play.PlayLooping();
                }
                else if (stopWatch.ElapsedTicks == 10000000000)
                {
                    pictureBox1.Hide();
                    pictureBox2.Show();
                    label2.Show();
                    play.Stream = Properties.Resources.alarm;
                    play.PlayLooping();
                    pictureBox2.Hide();
                    label2.Hide();
                }
                else if (stopWatch.ElapsedTicks == 1000000000000)
                {
                    pictureBox1.Show();
                    play.Stream = Properties.Resources.alarm;
                    play.PlayLooping();
                }
                else if (stopWatch.ElapsedTicks == 100000000000000)
                {
                    pictureBox1.Hide();
                    play.Stream = Properties.Resources.alarm;
                    play.PlayLooping();
                }
                else if (stopWatch.ElapsedTicks == 10000000000000000)
                {
                    pictureBox2.Show();
                    label2.Show();
                    play.Stream = Properties.Resources.alarm;
                    play.PlayLooping();
                    pictureBox2.Hide();
                    label2.Hide();
                }
                else if (stopWatch.ElapsedTicks == 1000000000000000000)
                {
                    pictureBox3.Show();
                    label3.Show();
                    textBox1.Show();
                    play.Stream = Properties.Resources.alarm;
                    play.PlayLooping();
                    pictureBox3.Hide();
                    label3.Hide();
                    textBox1.Hide();
                }
                else if (stopWatch.ElapsedTicks > 1000000000000000000)
                {
                    pictureBox1.Show();
                    Close();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)        //STOPS STOPWATCH
        {
            stopWatch.Stop();
        }

        private void button3_Click(object sender, EventArgs e)        //RESETS STOPWATCH
        {
            stopWatch = Stopwatch.StartNew();
        }

        private void timer1_Tick(object sender, EventArgs e)        //DISPLAYS TIME FROM WATCH
        {
            label1.Text = stopWatch.ElapsedTicks.ToString();
            label1.Text = "Time elapsed: " + stopWatch.Elapsed;
        }

        private void button4_Click(object sender, EventArgs e)        //BUTTON TO STOP SOUNDS
        {
            play.Stop();
        }

    }
}

Does anyone have any tips for this structure?  I'm probably going about these things in an inefficient manner. 

Thank you in advance.

-AD-


AndrewDen


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>