Nona Urbiz
Nona Urbiz

Reputation: 5003

C# using the scrollbar control / event (without textbox or window scroll)

I need to allow a long label to be scrolled through on it's own. I do not want a text-box of any sort. I would like to be able to format the text inside. It definitely needs to scroll own its own, not with the window. I have added a scrollbar successfully, but I have no idea how to begin to use it's event/s.

thanks

i tried using a panel? I will again, perhaps I made an error. :: yeah I tried that again, it simply cuts off my label.

Upvotes: 1

Views: 4669

Answers (2)

Aert
Aert

Reputation: 2049

Add a label (here label1) and a scrollbar (here hScrollBar1) and deal with the event in this fashion (assuming hScrollBar1.Maximum = 100 and hScrollBar1.Minimum = 0):

 private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
    {
        const int labellength = 10;
        String thetext = "Ozzie ozzie ozzie! OI OI OI! And then some...";
        int offset = (int)((double)e.NewValue / 100 * (thetext.Length - labellength));
        label1.Text = thetext.Substring(offset, labellength);
    }

Naturally you would have to specify the 'amount' of text to appear in the label by changing labellength. If you find that you can not scroll to the very end, lower hScrollBar1.LargeChange to 1.

Upvotes: 0

Phoexo
Phoexo

Reputation: 2556

Place the label inside a Panel and set AutoScroll to true.

Upvotes: 6

Related Questions