-
C#添加toolstripstatuslabel
2017-07-20 11:14:021 在工具箱中添加statusStrip1 2 选中statusStrip1,右键 编辑项,可以添加toolstripstatuslabel、ProgressBar...splitButton、DropdownButtono1 在工具箱中添加statusStrip1
2 选中statusStrip1,右键 编辑项,可以添加toolstripstatuslabel、ProgressBar...splitButton、DropdownButtono
-
ToolStripStatusLabel not displaying '&' chars
2021-01-07 13:42:44chars to a ToolStripStatusLabel, the string is displayed WITHOUT any '&' chars. <p><strong>Expected behavior:</strong></p> <p>For example: <p>toolStripStatusLabel.Text = "test&... -
toolstripstatuslabel 右对齐
2012-05-15 18:22:48在代码中设置toolstripstatuslabel 的属性Alignment 为 System.Windows.Forms.ToolStripItemAlignment.Right在代码中设置toolstripstatuslabel 的属性Alignment 为 System.Windows.Forms.ToolStripItemAlignment.Right
-
ToolStripStatusLabel 没有 InvokeRequired 属性的解决办法
2017-12-10 19:31:34ToolStripStatusLabel 没有 InvokeRequired 属性的解决办法当编写多线程程序时,你希望在线程中修改 Form 窗体上的控件的文本等属性,但你会得到一个错误:线程间操作无效: 从不是创建控件“xxx”的线程访问它。...ToolStripStatusLabel 没有 InvokeRequired 属性的解决办法
当编写多线程程序时,你希望在线程中修改 Form 窗体上的控件的文本等属性,
但你会得到一个错误:线程间操作无效: 从不是创建控件“xxx”的线程访问它。
引发了“Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException”类型的异常.
这时有一个解决办法就是使用委托来进行 Invoke 调用,
但是你会发现 ToolStripStatusLabel 没有 InvokeRequired 属性!
这个问题存在的原因是什么呢?
我们看看 Textbox 的继承关系:
public abstract class TextBoxBase : Control
再看看 ToolStripStatusLabel 的继承关系:
再看看 ToolStripStatusLabel 的容器 StatusStrip 的继承关系:public abstract class ToolStripItem : Component, IDropTarget, IComponent, IDisposable
我们会发现,这是因为ToolStripItem 是一个组件 Component,而不是一个控件 Control。public class ScrollableControl : Control, IComponent, IDisposable
解决办法:方法其实也比较简单,尝试在拥有它们的工具条、状态栏(StatusStrip)上调用扩展方法,并调整委托方法。
实例一:
public class MyForm : System.Windows.Forms.Form { //UI 元素 private Label lblStatus; private ProgressBar progressBar1; //Delegate private delegate void MyProgressEventsHandler( object sender, MyProgressEvents e); private void UpdateUI(object sender, MyProgressEvents e) { lblStatus.Text = e.Msg; myProgressControl.Value = e.PercentDone; } //ShowProgress 现在可以记录为可从任何线程调用的公共方法。 public void ShowProgress(string msg, int percentDone) { if(InvokeRequired) { System.EventArgs e = new MyProgressEvents(msg, percentDone); object[] pList = { this, e }; BeginInvoke(new MyProgressEventsHandler(UpdateUI), pList); } else { UpdateUI(this, new MyProgressEvents(msg, PercentDone)); } } private void btnStart_Click(object sender, EventArgs e) { //启动线程 Thread t = new Thread(new ParameterizedThreadStart(RunsOnWorkerThread)); t.IsBackground = true; t.Start(input); } //线程执行函数 private void RunsOnWorkerThread() { int i = 0; while(...) //loop { DoSomethingSlow(); ShowProgress("test",i); ++i; } } }
上面的代码,我们看到什么了?
啥,直接使用 “InvokeRequired”,不用使用“ToolStripStatusLabel.InvokeRequired”这样的格式!
对的,这样就可以了。
当然你也可以使用“StatusStrip.InvokeRequired”这样的形式!
实例二:
上面的例子代码可能不太好理解,我们看看微软官方的例子:
下面的代码示例是一个完整的 Windows 窗体应用程序,它包含一个带有三个按钮和一个文本框的窗体。 第一个按钮演示不安全的跨线程访问,第二个按钮演示使用 Invoke 实现的安全访问,而第三个按钮演示使用 BackgroundWorker 实现的安全访问。
using System; using System.ComponentModel; using System.Threading; using System.Windows.Forms; namespace CrossThreadDemo { public class Form1 : Form { // This delegate enables asynchronous calls for setting // the text property on a TextBox control. delegate void SetTextCallback(string text); // This thread is used to demonstrate both thread-safe and // unsafe ways to call a Windows Forms control. private Thread demoThread = null; // This BackgroundWorker is used to demonstrate the // preferred way of performing asynchronous operations. private BackgroundWorker backgroundWorker1; private TextBox textBox1; private Button setTextUnsafeBtn; private Button setTextSafeBtn; private Button setTextBackgroundWorkerBtn; private System.ComponentModel.IContainer components = null; public Form1() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } // This event handler creates a thread that calls a // Windows Forms control in an unsafe way. private void setTextUnsafeBtn_Click( object sender, EventArgs e) { this.demoThread = new Thread(new ThreadStart(this.ThreadProcUnsafe)); this.demoThread.Start(); } // This method is executed on the worker thread and makes // an unsafe call on the TextBox control. private void ThreadProcUnsafe() { this.textBox1.Text = "This text was set unsafely."; } // This event handler creates a thread that calls a // Windows Forms control in a thread-safe way. private void setTextSafeBtn_Click( object sender, EventArgs e) { this.demoThread = new Thread(new ThreadStart(this.ThreadProcSafe)); this.demoThread.Start(); } // This method is executed on the worker thread and makes // a thread-safe call on the TextBox control. private void ThreadProcSafe() { this.SetText("This text was set safely."); } // This method demonstrates a pattern for making thread-safe // calls on a Windows Forms control. // // If the calling thread is different from the thread that // created the TextBox control, this method creates a // SetTextCallback and calls itself asynchronously using the // Invoke method. // // If the calling thread is the same as the thread that created // the TextBox control, the Text property is set directly. private void SetText(string text) { // InvokeRequired required compares the thread ID of the // calling thread to the thread ID of the creating thread. // If these threads are different, it returns true. if (this.textBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.textBox1.Text = text; } } // This event handler starts the form's // BackgroundWorker by calling RunWorkerAsync. // // The Text property of the TextBox control is set // when the BackgroundWorker raises the RunWorkerCompleted // event. private void setTextBackgroundWorkerBtn_Click( object sender, EventArgs e) { this.backgroundWorker1.RunWorkerAsync(); } // This event handler sets the Text property of the TextBox // control. It is called on the thread that created the // TextBox control, so the call is thread-safe. // // BackgroundWorker is the preferred way to perform asynchronous // operations. private void backgroundWorker1_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) { this.textBox1.Text = "This text was set safely by BackgroundWorker."; } #region Windows Form Designer generated code private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.setTextUnsafeBtn = new System.Windows.Forms.Button(); this.setTextSafeBtn = new System.Windows.Forms.Button(); this.setTextBackgroundWorkerBtn = new System.Windows.Forms.Button(); this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(240, 20); this.textBox1.TabIndex = 0; // // setTextUnsafeBtn // this.setTextUnsafeBtn.Location = new System.Drawing.Point(15, 55); this.setTextUnsafeBtn.Name = "setTextUnsafeBtn"; this.setTextUnsafeBtn.TabIndex = 1; this.setTextUnsafeBtn.Text = "Unsafe Call"; this.setTextUnsafeBtn.Click += new System.EventHandler(this.setTextUnsafeBtn_Click); // // setTextSafeBtn // this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55); this.setTextSafeBtn.Name = "setTextSafeBtn"; this.setTextSafeBtn.TabIndex = 2; this.setTextSafeBtn.Text = "Safe Call"; this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click); // // setTextBackgroundWorkerBtn // this.setTextBackgroundWorkerBtn.Location = new System.Drawing.Point(177, 55); this.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn"; this.setTextBackgroundWorkerBtn.TabIndex = 3; this.setTextBackgroundWorkerBtn.Text = "Safe BW Call"; this.setTextBackgroundWorkerBtn.Click += new System.EventHandler(this.setTextBackgroundWorkerBtn_Click); // // backgroundWorker1 // this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted); // // Form1 // this.ClientSize = new System.Drawing.Size(268, 96); this.Controls.Add(this.setTextBackgroundWorkerBtn); this.Controls.Add(this.setTextSafeBtn); this.Controls.Add(this.setTextUnsafeBtn); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } }
相关参考: -
paip C NET多线程访问 toolStripStatusLabel
2018-11-17 22:42:53paip C NET多线程访问 toolStripStatusLabel分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
paip.C#.NET多线程访问 toolStripStatusLabel
作者Attilax , EMAIL:1466519819@qq.com
toolStripStatusLabel控件比较特殊,无法定义invoke来线程调用。。只好使用原生委托..代码稍微多一些..
delegate void clsC417();
xxx()
{
//setStatubarCount c4g
clsC417 clsobj = new clsC417(
delegate
{
toolStripStatusLabel3.Text = "/" + ct.loadRecNum;
});
clsobj();
//end c4g
}
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
-
ToolStripStatusLabel设置时间自动更新
2016-02-22 10:06:46在使用委托设置界面上ToolStripStatusLabel类型的控件时间是,发现不能使用自定义的委托方法,在往上查找了一下发现不能使用involve来线程调用。因此只能使用原生委托方法。 //代理public delegate void Set... -
通过StatusStrip访问 toolStripStatusLabel的属性
2016-08-25 21:56:07Form直接访问toolStripStatusLabel的属性,不能范围。报错。后来参考 http://stackoverflow.com/questions/29829936/how-to-update-toolstripstatuslabel-text-from-a-method-passing-all-the-controls-o ... -
vs2010 c# toolStripStatusLabel1显示tooltiptext的方法
2017-04-20 21:39:20vs2010 c# toolStripStatusLabel1显示tooltiptext的方法 程序症状: 在toolStripStatusLabel1(状态栏上的标签)的tooltiptext设置了要显示的文件,运行程序发现并不能显示tooltiptext设置的文本。 解决办法: 在... -
winform控件statusStrip1上的toolStripStatusLabel1如何靠右对齐
2019-12-04 22:41:29以下代码为正确实现。 this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow... this.toolStripStatusLabel1.Alignment = System.Windows.Forms.ToolStripIte... -
StatusStrip控件中,ToolStripStatusLabel的位置
2015-01-05 20:14:13在c#中用到了状态栏控件Status...按照MSDN中的办法,是设置ToolStripStatusLabel的Alignment属性为Right。不过我在设计界面的属性窗口中找不到Alignment。 就算加入代码toolStripStatusLabel2.Alignment = ToolStri -
设置StatusStrip控件中,ToolStripStatusLabel的位置
2017-08-24 11:06:00设置StatusStrip控件中,ToolStripStatusLabel的位置 在c#中用到了状态栏控件StatusStrip,但当我想把StatusStrip上某个StatusLabel靠右对齐时出了问题。 按照MSDN中的办法,是设置ToolSt... -
状态栏lable居中或两边对齐问题(ToolStripStatusLabel 的spring属性)
2020-02-21 16:55:58MSDN :spring属性获取或设置一个值,该值指示在调整窗体大小时, ToolStripStatusLabel是否自动填充StatusStrip上的可用空间 可使得ToolStrip上的label控件在其余部分居中显示 ... -
设置StatusStrip控件中ToolStripStatusLabel的位置
2014-04-01 15:51:11在学习过程中,发现了三种方法可以调整StatusStrip控件中ToolStripStatusLabel的位置,具体如下: 方法1: 设置ToolStripStatusLabel的padding属性,通过改变第一个和第三个参数值可以调整其左右位置。 方法2... -
paip.C#.NET多线程访问 toolStripStatusLabel
2013-04-17 20:27:00paip.C#.NET多线程访问 toolStripStatusLabel 作者Attilax , EMAIL:1466519819@qq.com toolStripStatusLabel控件比较特殊,无法定义invoke来线程调用。。只好使用原生委托..代码稍微多一些.. ... -
设置ToolStripStatusLabel的Text属性后,文本内容很快就消失。
2015-05-26 01:14:33设置ToolStripStatusLabel的Text属性后return,但在界面上该文本内容很快就消失。代码: ``` private void setTsslReStatus(string text, Color color) { tsslReStatus.Text = text; tsslReStatus.... -
paip.C#.NET多线程访问 toolStripStatusLabel VC421
2013-04-21 18:00:01paip.C#.NET多线程访问 toolStripStatusLabel VC421 作者Attilax , EMAIL:1466519819@qq.com toolStripStatusLabel控件比较特殊,无法定义invoke来线程调用。。 原以为只好使用原生委托..代码... -
C# toolStripStatusLabel 文本不更新/文本显示不正确
2011-11-17 13:39:37今天做项目时碰到个问题,就是toolStripStatusLabel文本不更新的问题,代码如下 toolStripStatusLabel1.Text = "开始"; …… …… toolStripStatusLabel1.Text = "结束"; 但运行时只有“结束”显示出来了,... -
关于toolStripStatusLabel1
2015-12-18 12:39:36Form1中 Form2 Form2 = new Form2(); Form2.Owner = this; Form2.Show(); ...Form1.toolStripStatusLabel3.Text = "123";...大神帮忙看下,该怎么改,在Form中点button,Form1中的toolStripStatusLabel3显示 -
关于toolStripStatusLabel2
2015-12-18 14:07:13toolStripStatusLabel1.Text = "时间: " + DateTime.Now.ToString(); } Form3中 public void button1_Click(object sender, EventArgs e) { Form1.toolStripStatusLabel2.Text = "XXXXXXX"; //这里有... -
statusStrip 状态条 toolStripStatusLabel 居右显示
2010-09-22 11:20:00statusStrip1.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;toolStripStatusLabel1.Alignment = ToolStripItemAlignment.Right; -
多线程定义paip.C#.NET多线程访问 toolStripStatusLabel VC421
2013-04-21 21:31:00最近应用开发的过程中出现...paip.C#.NET多线程拜访 toolStripStatusLabel VC421 作者Attilax , EMAIL:1466519819@qq.com toolStripStatusLabel件控较比特别,法无定义invoke来线程用调。。 原以为只好应用原生... -
子窗口关闭以后,刷新父窗口的toolStripStatusLabel1的Text属性,怎么办
2012-07-09 21:16:15C#里面,子窗口关闭以后,刷新一个父窗口的控件的属性,怎么办 2011-10-25 16:24 提问者: 52104 | 浏览次数:731次 ...子窗口关闭以后,刷新父窗口的toolStripStatusLabel1的Text属性,怎么办 我来帮他解答 -
ToolStripStatusLabel控件Text显示问题
2013-04-16 11:18:21FROM中有“转换”(Name:btnConvert)、“写入”(Name:button1)两个按钮控件,底部有ToolStripStatusLabel(Name:stpbSM)、ToolStripProgressBar(Name:stpbJD) [size=16px]我想实现当点击“转换”按钮后出现...