C# 为什么这个字幕代码不起作用?
我使用这段代码是为了制作一个从右向左滚动的字幕标签C# 为什么这个字幕代码不起作用?,c#,C#,我使用这段代码是为了制作一个从右向左滚动的字幕标签 private int xPos = 651; private void timer1_Tick(object sender, EventArgs e) { if (this.Width == xPos) { //repeat marquee xPos = 651; this.Label7.Location = ne
private int xPos = 651;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Width == xPos)
{
//repeat marquee
xPos = 651;
this.Label7.Location = new System.Drawing.Point(651, 334);
xPos--;
}
else
{
this.Label7.Location = new System.Drawing.Point(xPos, 334);
xPos--;
}
}
651是窗体的宽度
此代码使标签从右向左移动,像它应该的那样滚动表单,但不会再次从右侧重新启动。我假设
此宽度是一个正值,因此如果您重复执行减法,您只能在开始时达到此值。
如果(xPos==0),请尝试
,我很确定您可能已经看到了这一点:
阅读第一个答案。我觉得这个代码毫无意义
试试这个,想怎么做就怎么做
private bool goLeft;
private void timer1_Tick(object sender, EventArgs e)
{
if (Label7.Width + Label7.Left >= this.Width)
{
goLeft = true;
}
else if (Label7.Left < 0)
{
goLeft = false;
}
Label7.Left += goLeft ? -10 : 10;
}
私人布尔高尔夫球场;
私有无效计时器1_刻度(对象发送方,事件参数e)
{
如果(Label7.Width+Label7.Left>=此.Width)
{
高尔夫=真;
}
else if(标签7.Left<0)
{
高尔夫=假;
}
标签7.左+=高尔夫球场?-10:10;
}
虽然我同意从客户端使用字幕会更容易,但他并不是这么要求的
您正在以此.Width启动xPos并将其递减,因此,当xPos达到0而不是此.Width时,您需要重置xPos。xPos在每次运行的第一次迭代中仅等于此.Width
private int xPos = this.Width;
private void timer1_Tick(object sender, EventArgs e) {
if (xPos == 0) { xPos = this.Width; }
this.Label7.Location = new System.Drawing.Point(xPos, 334);
xPos--;
}
标题中的双重否定:)出于好奇:为什么不在客户端使用jquery或纯javascript进行选框?@Dimitri,我认为这是winform,我做了太多webform:-p基于函数名(可能由设计器自动生成)和计时器名(也可能仅通过拖放生成),我认为可以安全地假设这是一个winforms应用程序。@Brook webforms自动生成的名称也完全相同。我只是假设这是webforms,因为我做了太多的webforms,就像FreduoThis一样,但是有没有办法调整它,使它在标签的末尾撞到表单的左侧时重置,而不是开始?而不是什么的开始?