我有一个带有两个自定义面板的表单,我正在为它们添加一些标签,最后我想让它们从右向左滚动,但它们只是在相同的位置。 有我的截图 有两个面板和4个标签,他们只是留在那里... 这是我表格中的代码:
private MyPanel topPanel; // the red bar
private MyPanel botPanel; // the white bar
public SmsBar()
{
InitializeComponent();
this.Width = 500000;
this.Location = new Point(x, y);
topPanel = new MyPanel(System.Drawing.Color.White, System.Drawing.Color.Red, (new Font("Tahoma", 10, FontStyle.Bold)));
botPanel = new MyPanel(System.Drawing.Color.Black, System.Drawing.Color.White, (new Font("Tohama", 10, FontStyle.Regular)));
msgPanel.Controls.Add(topPanel);
adPanel.Controls.Add(botPanel);
botPanel.addMsg(new MsgForDisplay(0, AppData.getInstance().getAdMsg()));
botPanel.addMsg(new MsgForDisplay(0, AppData.getInstance().getAdMsg2()));
topPanel.addMsg(new MsgForDisplay(0, "test top"));
topPanel.addMsg(new MsgForDisplay(0, "test top 2"));
adPanel.Refresh();
msgPanel.Refresh();
}
我的自定义面板(构造函数)有一些代码:
public MyPanel(Color corLabel, Color back, Font text){
this.color = corLabel;
this.backg = back;
this.textFont = text;
this.Width = 500000;
txt= new LinkedList<string>();
msgs = new LinkedList<MsgForDisplay>();
labels = new LinkedList<Label>();
var it = labels.GetEnumerator();
var it2 = msgToRemove.GetEnumerator();
this.FlowDirection = FlowDirection.LeftToRight;
this.BackColor = backg;
this.Size = new Size(500000, 35);
this.Refresh();
}
正在运行:
public void run() {
while (true) {
var it = labels.GetEnumerator();
while(it.MoveNext()){
Label lb = it.Current;
if (lb.Location.X + lb.Width < 0) {
if (msgsRemover.Contains(lb.Text.ToString())) {
labels.Remove(lb);
this.Invoke(new MethodInvoker(() => { this.Controls.Remove(lb); }));
msgsRemover.Remove(lb.Text.ToString());
} else {
// if there is no message to be removed, they will just continue
// going to the end of the queue
this.Invoke(new MethodInvoker(() => { this.Controls.Remove(lb); }));
this.Invoke(new MethodInvoker(() => { this.Controls.Add(lb); }));
}
this.Invoke(new MethodInvoker(() => { this.Refresh(); }));
}
this.Invoke(new MethodInvoker(() => { lb.Location = new Point(lb.Location.X - 3, lb.Location.Y); }));
}
System.Threading.Thread.Sleep(30);
}
}
addMsg 功能:
public void addMsg(MsgForDisplay msg) {
Label lbl = new Label();
lbl.Text = ("- " + msg.getText() + " -");
lbl.ForeColor = color;
lbl.Font = textFont;
lbl.BackColor = backg;
lbl.Visible = true;
lbl.AutoSize = true;
labels.AddLast(lbl);
this.Controls.Add(lbl);
}
我猜问题必须与布局有关,但我真的不知道应该用什么...... 提前感谢您的帮助!
答案 0 :(得分:1)
如果我理解正确你的问题是..标签从右向左移动然后卡在左侧。
您正在删除并添加控件。但它不会重置控制位置。您需要在再次添加之前将Control.Location.X设置为800或某个值。
答案 1 :(得分:1)
为了解决这个问题,我删除了flowlayout并将面板作为面板而不是FlowLayoutPanel,这样我就可以移动标签,不同之处在于我必须创建一个算法来在面板中添加每个标签
答案 2 :(得分:0)
@JoãoSilva:刚提醒...... C#是服务器端,当您查看页面时,它位于客户端。 (如果你在谈论ASP.NET)
注意:Jquery插件通常重量很轻,在性能方面也会更好。
如果您需要解决方案,请告诉我,我会尝试为您编写一些示例
参考链接以验证您是否需要:
文档和来源:http://remysharp.com/2008/09/10/the-silky-smooth-marquee/
演示:http://remysharp.com/demo/marquee.html