Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a level bar as a toolbox control to make it easy to use when needed for my game project. I mean level bar is a something like Level bar image
All level should be clickable and when clicked on any bar of the control on the design view, all of the bars should be selected and move together. The following code gives an error NullReference when dragged over the form on the design view.

C#
    public class LevelBar : Label
    {
    Label[] labels;
    int bars;
    public LevelBar()
    {
        BackColor = Color.Green;
        bars = 3;
        CreateBars();
    }
    public int Bars
    {
        get
        {
            return bars;
        }
        set
        {
            bars = value;
            CreateBars();
        }
    }
    void CreateBars()
    {
        int width = 10;
        int height = 20;
        labels = new Label[Bars];
        for (int i = 0; i < Bars; i++)
        {
            labels[i].Width = width;
            labels[i].Height = height + i * 10;
            labels[i].Left = Location.X + i * 20;
            labels[i].Top = Location.Y - i * 10;
            this.Parent.Controls.Add(labels[i]);
        }
    }
}
Posted
Updated 12-Sep-15 3:00am
v9
Comments
OriginalGriff 12-Sep-15 7:03am    
And?
What have you tried?
Where are you stuck?
What help do you need?
Amt-Coder 12-Sep-15 8:55am    
I have problem from beginning to the end. But in anyway, I have written my code I try to create LevelBar above
aarif moh shaikh 12-Sep-15 8:25am    
not Clarifying Questions....

Try adding:
C#
labels = new Label[Bars];
for (int i = 0; i < Bars; i++)
{
    labels[i] = new Label();  // ***ADD THIS LINE ***
    labels[i].Width = width;
 
Share this answer
 
Comments
Amt-Coder 12-Sep-15 13:03pm    
That was one of the problem. Thanks. I achieved it as a result of so many effort. The code is written below for people who have the same problem.
My solution like this.
C#
class LevelBar : Label
{
    Label[] labels;
    int bars, value;
    public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);
    public event ValueChangedEventHandler ValueChanged;
    public LevelBar()
    {
        bars = 5;
        this.Value = 2;
        labels = new Label[bars];
        this.Cursor = Cursors.Hand;
        BackColor = Color.Transparent;
    }
    public override string Text
    {
        get
        {
            return string.Empty;
        }
        set
        {
            base.Text = string.Empty;
        }
    }
    public override bool AutoSize
    {
        get
        {
            return false;
        }
        set
        {
            base.AutoSize = false;
        }
    }

    public int Value
    {
        get { return value; }
        private set
        {
            this.value = value;
            ValueChangedEventArgs e = new ValueChangedEventArgs(this.Value);
            if (ValueChanged != null)
            ValueChanged(this, e);
        }
    }
    [Browsable(true)]
    public int Bars
    {
        get { return bars; }
        set { bars = value; createBars(); }
    }

    protected override void OnCreateControl()
    {
        base.OnCreateControl();
        createBars();
    }
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        createBars();
    }
    int barWidth, barHeight;
    void createBars()
    {
        disposeAllandCreateNew();
        barWidth = 2 * this.Width / (3 * bars - 1);
        barHeight = 2 * this.Height / (bars + 1);
        for (int i = 0; i < labels.Length; i++)
        {
            labels[i].BorderStyle = BorderStyle.FixedSingle;
            labels[i].Click += LevelBar_Click;
            this.Controls.Add(labels[i]);
            labels[i].Cursor = Cursors.Hand;
            labels[i].AutoSize = false;
            labels[i].Width = barWidth;
            labels[i].Height = barHeight + (barHeight / 2) * i;
            labels[i].Left = (int)(i * (1.5 * barWidth));
            labels[i].Top = this.Height - labels[i].Height;
            labels[i].BackColor = Color.Green;
        }
        setBarColors();
    }
    void disposeAllandCreateNew()
    {
        for (int i = 0; i < labels.Length; i++)
        {
            if (labels[i] != null) labels[i].Dispose();
        }
        labels = new Label[bars];
        for (int i = 0; i < labels.Length; i++)
        {
            labels[i] = new Label();
        }
    }
    void LevelBar_Click(object sender, EventArgs e)
    {
        int val = (sender as Label).Left / (3 * barWidth / 2);
        this.Value = val + 1;
        setBarColors();
    }

    void setBarColors()
    {
        for (int k = 0; k < bars; k++)
        {
            if (k < this.Value)
                labels[k].BackColor = Color.Green;
            else
                labels[k].BackColor = this.BackColor;
        }
    }
}
class ValueChangedEventArgs
{
    int value;
    public ValueChangedEventArgs(int value)
    {
        this.value = value;
    }
    public int Value
    {
        get { return value; }
        set { this.value = value; }
    }
}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900