locked
trackbar help RRS feed

  • Question

  • I have a form with three textboxes, three trackbars, and a button.  The idea is to have the user enter a value into the three textboxes and click the button at which time each of the trackbars would slide to appropriate value based on the numbered entered into the textbox.  However, I haven't been able to achieve that.  When the user clicks the button the slider remains at zero.   How do I get it to slide when the user clicks the button? 


       private void button19_Click(object sender, EventArgs e)
            {

           
                    Double number;
                    Double max = 50000d;
                    Double.TryParse(test4.Text, out number);

                    Double result = number / 3;


                    textBox1.Text = String.Format("{0:0}", max);
                    Double remainder = Double.Parse(test4.Text) - (Double.Parse(textBox1.Text) * 2);
                    textBox1.Text = String.Format("{0:0}", remainder);
                                            

                }
                   
                }
                textBox2.Text = textBox3.Text;
                       }
      
               private void trackBar2_Scroll_1(object sender, EventArgs e)

                {

                          textBox1.Text = "" + trackBar2.Value;

     

                }

                          private void trackBar3_Scroll_1(object sender, EventArgs e)

                {

                          textBox2.Text = "" + trackBar3.Value;

                 }

                          private void trackBar1_Scroll_1(object sender, EventArgs e)

                 {

                          textBox3.Text= "" + trackBar1.Value;

                 }

     

    Saturday, September 12, 2009 1:48 PM

Answers

  • You first of all need to put your TryParse in an if statement because what happens it it doesn't parse out to a double? Then you are in trouble.

    //example

    if (Double.TryParse(test4.Text, out number))
    {
         .........


    Fix that first, and make the rest of the functionality is in that if statement.

    Here is an example of a Trackbar functionality. It has a label, three Trackbar, and a pictureBox. When you adjust the TrackBar, it adjusts the RGB color on the pictureBox

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                tbBlue.Value = 0;
                tbGreen.Value = 0;
                tbRed.Value = 0;
                UpdateColor();
            }
    
            private void Scoll(object sender, EventArgs e)
            {
                UpdateColor();
            }
          
    
            private void UpdateColor()
            {
                Color c = Color.FromArgb(tbRed.Value, tbGreen.Value, tbBlue.Value);
                colorBox.BackColor = c;
                lblCurrColor.Text = "Current color is: (" + tbRed.Value + ", " + tbGreen.Value + " ," + tbBlue.Value + ")";           
            }
    
        }
    }

    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by KINGM56 Sunday, September 13, 2009 6:06 PM
    Saturday, September 12, 2009 2:32 PM