开发者

Dynamically created trackbars and Labels and linking them together in C#

I am trying to create trackbars and labels dynamically. In which if a user inputs a number like 4, it creates 4 trackbars and 4 labels. Then if the user moves any of the dynamically created trackbar moves it and updates its associated label. Then adds the numbers in all the labels and stores it in another label call total label.

Here is another way of explaining it. The user enters the number 3. The system creates 3 trackbars and 3 labels (next to the trackbars). The user moves first track bar to 5, the first label is automatically updated with 5. The user moves the second track bar to 3, the second label is automatically updated with 3. finally the user moves the third track bar to position 9 and the label is automatically updated with 9.

On the right side there is another label that shows 17 = (5+3+9).

I found a few websites ons creating dynamically controls but I don't know how to link a dynamically created trackbar to the dynamically created label. Then adding those dynamically added labels.

ALL this in C# on a windows form.

I did something very simliar to the below when creating my labels and trackbars.

for (int i = 0; i < 10; i++)
{
    Label label = new Label();
    label.Text = i.ToString();
    PlaceHolder1.Controls.Add(label);
}

Thanks in advances

------------------------Update-----------------

void CreateLabeledTrackBars(Control host, int n)

how do I use this, I was hoping that when I start a new form

that all I would have to is开发者_运维技巧 this..that way the form already has the in n, but it seems not to work..i am confused on how the control works. can you please explain

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {

     public static Form2 myNewForm = new Form2();

           private TrackBar[] _trackBars;
           private Label[] _labels;
   public Form3(int n)
         {
CreateLabeledTrackBars(new Label (), n);
}

//Then the rest of the code 


You need to handle ValueChanged event of each TrackBar you create. To calculate sum of all values, store created controls in arrays.

private TrackBar[] _trackBars;
private Label[] _labels;

void CreateLabeledTrackBars(Control host, int n)
{
    const int trackBarWidth = 150;
    const int minValue = 0;
    const int maxValue = 10;
    const int defaultValue = 0;

    _trackBars = new TrackBar[n];
    _labels = new Label[n];
    int y = 0;
    for(int i = 0; i < n; ++i)
    {
        var label = new Label()
        {
            Top     = y,
            Left    = trackBarWidth,
            Text    = defaultValue.ToString(),
            Parent  = host,
        };
        var trackBar = new TrackBar()
        {
            Top     = y,
            Width   = trackBarWidth,
            // save associated label
            Tag     = label,
            Minimum = minValue,
            Maximum = maxValue,
            Value   = defaultValue,
            Parent  = host,
        };
        // handle ValueChangedEvent
        trackBar.ValueChanged += OnTrackBarValueChanged;
        // apply vertical offset for next trackbar
        y += trackBar.Height;
        _trackBars[i] = trackBar;
        _labels[i] = label;
    }
}

void OnTrackBarValueChanged(object sender, EventArgs e)
{
    // get trackbar, which generated event
    var trackBar = (TrackBar)sender;
    // get associated label
    var associatedLabel = (Label)trackBar.Tag;
    associatedLabel.Text = trackBar.Value.ToString();
    // recalculate sum of all values and update other label here
}

Now, when you have an array of trackbars, getting sum of all values should be trivial.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜