Reputation: 49
Im using a Label where text input from a text box is shown in that label. Now, I whant to make the label text scroll. I´ve looked around through the internet and I tried to write this into the code inside of the label:
private void label1_Click(object sender, EventArgs e)
{
int Scroll;
string strString = "This is scrollable text...This is scrollable text...This is scrollable text";
Scroll = Scroll + 1;
int iLmt = strString.Length - Scroll;
if (iLmt < 20)
{
Scroll = 0;
}
string str = strString.Substring(Scroll, 20);
label1.Text = str;
}
Does anybody see what Im doing wrong?
Upvotes: 4
Views: 18736
Reputation: 2337
This is possible using my library.
WinForm Animation Library [.Net3.5+]
A simple library for animating controls/values in .Net WinForm (.Net 3.5 and later). Key frame (Path) based and fully customizable.
https://falahati.github.io/WinFormAnimation/
var textToScroll = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
var durationOfAnimation = 5000ul;
var maxLabelChars = 20;
var label = label1;
new Animator(new Path(0, 100, durationOfAnimation))
{
Repeat = true,
ReverseRepeat = true
}.Play(
new SafeInvoker<float>(f =>
{
label.Text =
textToScroll.Substring(
(int) Math.Max(Math.Ceiling((textToScroll.Length - maxLabelChars)/100f * f) - 1, 0),
maxLabelChars);
}, label));
Upvotes: 0
Reputation: 61
//much easier:
private void timer2scroll_Tick(object sender, EventArgs e)
{
label10Info.Text = label10Info.Text.Substring(1, label10Info.Text.Length - 1) + label10Info.Text.Substring(0,1);
}
Upvotes: 6
Reputation: 1653
You need to declare the Scroll variable outside of you function call, it is reset every time you click it.
Here it the code with a timer on the form load to auto scroll the text:
private Timer tmr;
private int scrll { get; set; }
void Form1_Load(object sender, EventArgs e)
{
tmr = new Timer();
tmr.Tick += new EventHandler(this.TimerTick);
tmr.Interval = 200;
tmr.Start();
}
private void TimerTick(object sender, EventArgs e)
{
ScrollLabel();
}
private void ScrollLabel()
{
string strString = "This is scrollable text...This is scrollable text...This is scrollable text";
scrll = scrll + 1;
int iLmt = strString.Length - scrll;
if (iLmt < 20)
{
scrll = 0;
}
string str = strString.Substring(scrll, 20);
label1.Text = str;
}
private void label1_Click(object sender, EventArgs e)
{
ScrollLabel();
}
Upvotes: 2