C# 创建用户控件以在数组中旋转并每x秒显示一次文本
我正在尝试将其转换为用户控件,以便在我的应用程序中将其用作标准组件 我想有一些参数,即显示每个项目的时间和字符串数组设置 我只需要在表单的某个地方显示一个自定义标签C# 创建用户控件以在数组中旋转并每x秒显示一次文本,c#,multithreading,winforms,user-controls,C#,Multithreading,Winforms,User Controls,我正在尝试将其转换为用户控件,以便在我的应用程序中将其用作标准组件 我想有一些参数,即显示每个项目的时间和字符串数组设置 我只需要在表单的某个地方显示一个自定义标签 public void rotateMarqueText(string text) { string[] result = text.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries); new Task(()
public void rotateMarqueText(string text)
{
string[] result = text.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
new Task(() =>
{
int i = result.Count();
while (true)
{
i++;
if (i > result.Count()) i = 0;
Task.Factory.StartNew(() =>
{
this.Invoke(new Action(() => DisplayText(result[i]))); //
});
Thread.Sleep(1000); // want to param this
}
}).Start();
}
private void DisplayText(string x)
{
marqueText.Text = x;
marqueText.Refresh();
}
我对用户控件和线程没有任何概念,从哪里开始呢
public partial class marqueText : UserControl
{
private Color colFColor;
private Color colBColor;
private int timeDelay = 1;
private string[] scrollText = new string[] { "" };
private int scrollTextCount;
private int scrollTextCurrentPosition;
public marqueText()
{
InitializeComponent();
scrollTextCurrentPosition = 0;
scrollTextCount = MarqueText.Count();
}
public void timer1_Tick(object sender, EventArgs e)
{
lblDisplay.Text = scrollText[scrollTextCurrentPosition];
scrollTextCurrentPosition++;
if (scrollTextCurrentPosition == scrollTextCount)
{
scrollTextCurrentPosition = 0;
}
}
public int MarqueTimeDelay
{
get
{
return timeDelay;
}
set
{
timeDelay = value;
this.timer1.Interval = value * 1000;
}
}
public string[] MarqueText
{
get
{
scrollTextCount = scrollText.Count();
return scrollText;
}
set
{
scrollTextCurrentPosition = 0;
scrollText = value;
scrollTextCount = scrollText.Count();
}
}
public Color MarqueBackColor
{
get
{
return colBColor;
}
set
{
colBColor = value;
lblDisplay.BackColor = colBColor;
}
}
public Color MarqueForeColor
{
get
{
return colFColor;
}
set
{
colFColor = value;
lblDisplay.ForeColor = colFColor;
}
}
}
似乎与计时器配合得很好。您可以使用
计时器
但不相关,但您不需要在内部执行其他任务。只需在UI线程上调用即可。如何在UI线程上调用?