简体   繁体   中英

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.

将标签放在面板内 ,并将AutoScroll设置为true。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM