本文介绍了从另一个WindowsForm更新ProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好, 我有一个问题。我想从另一种形式更新ProgressBar。 我的项目是这样的: Form1 - 调用Form2(使用Form2.Show) Form2 - 做一些工作并且必须在Form3中更新我的ProgressBar Form3 - 这是我的ProgressBar 我尝试过以下方法: Form 3: public int ProgressBarValue { set { this .progressBar_Status.Value = value ; } } public int IncrementProgessBar { set {progressBar_Status.Increment( value ); } public int ProgressBarMaximum { set {ConfigProgressBar( value ); } } private void ConfigProgressBar( int intMax) { this .progressBar_Status.Visible = 真; this .progressBar_Status.Minimum = 0 ; this .progressBar_Status.Maximum = intMax; this .progressBar_Status.Value = 1 ; this .progressBar_Status.Step = 1 ; } Form2: public Form3 myOtherForm = new Form3(); public doWork() { foreach (元素zenVar in lstAlarms) { // Increment ToolStrip this .myOtherForm.IncrementProgessBar = 1 ; // 我的作品 } } 这不起作用!我不知道为什么。 你能告诉我在哪里错了吗?或者你能告诉我哪种方式可以完成这项任务吗? 谢谢。 Cis 解决方案 你试图通过form2对象(myOtherForm)从form3调用IncrementProgressBar 你应该有form3实例并调用它的更新方法(希望form3在某处可见或没有意义) 你好, Quote: Form1用作WeifenLuo.WinFormsUI.Docking的Mdi容器,所以我在那里显示Form2和Form3。 你做错了。您正在Form1(MDI)中创建Form2和Form3实例。然后在Form2中,您将创建另一个Form3实例。这是完全错误的。 我认为Form3只是带有进度条控件的表单,只是为了向用户显示操作进度而没有其他用途。 1.在Form1中,您只需要创建和显示Form2: var form2 = new Form2(); form2.Show(); 2.然后在Form2中创建Form3: private Form3 myOtherForm = new Form3(); public doWork() { this .myOtherForm.Show(); foreach (元素zenVar in lstAlarms) { // Increment ToolStrip this 。 myOtherForm.IncrementProgessBar = 1 ; // 我的工作 } this .myOtherForm.Hide(); } 第二种解决方案是在Form1中创建Form2和Form3,然后通过引用将创建的Form3实例直接传递给Form2构造函数。 但在我看来,这两种解决方案都是糟糕的设计。对于具有4-5个表单的简单家庭项目,它可以是可接受的,但对于中型/大型项目,您应该考虑使用一些设计模式。例如: 1.模型 - 视图 - 演示者 - 它可以帮助您分离您的应用程序层 2.创建一些AppHost / Shell来管理对象通信,尤其是当你正在处理许多表格,并希望在它们之间传递数据。 3.最重要的是使用SOLID编程:) 你可以在CP上找到很多关于SOLID和设计模式的文章。祝你好运。 我希望能帮到你。 我解决了它EventHandler,像这样: Form3 public Form3() { InitializeComponent(); Form.UpdateProgressBar + = new ProgressBarEventHandler(OnProgressBarUpdate); } public void OnProgressBarUpdate( object sender,ProgressBarEventArgs e) { if (e.ProgressBarIncrement!= 0 ) progressBar_Status.Increment(e.ProgressBarIncrement); } public delegate void ProgressBarEventHandler( object sender,ProgressBarEventArgs e); public class ProgressBarEventArgs { public ProgressBarEventArgs( int intIncrement) { ProgressBarIncrement = intIncrement; } public int ProgressBarIncrement {获得; private set ; } } 在Form2中: public static event ProgressBarEventHandler UpdateProgressBar; public doWork() { foreach (元素zenVar in lstAlarms) { // 递增进度条 UpdateProgressBar(此, new ProgressBarEventArgs( 0 , 1 , false )); // 我的作品 } } Hello,I have a question. I would like to update a ProgressBar from another form.My project works like this:Form1 - Calls Form2 (with Form2.Show)Form2 - Makes some work and has to update my ProgressBar in Form3Form3 - Here is my ProgressBarI have tried the following:In Form3:public int ProgressBarValue{ set { this.progressBar_Status.Value = value; }}public int IncrementProgessBar{ set { progressBar_Status.Increment(value);}public int ProgressBarMaximum{ set { ConfigProgressBar(value); }}private void ConfigProgressBar(int intMax){ this.progressBar_Status.Visible = true; this.progressBar_Status.Minimum = 0; this.progressBar_Status.Maximum = intMax; this.progressBar_Status.Value = 1; this.progressBar_Status.Step = 1;}In Form2:public Form3 myOtherForm = new Form3();public doWork(){ foreach (Element zenVar in lstAlarms) { //Increment ToolStrip this.myOtherForm.IncrementProgessBar = 1; //my work }}This does not work! I don't know why. Can you tell me where am I wrong? or could you tell me which other way there is to accomplish this task?Thank you.Cis 解决方案 You're trying to call IncrementProgressBar from form3 by form2 object (myOtherForm)You should have form3 instance and call its update method (hopefully, form3 will be visible somewhere or there is no point)Hello,Quote:The Form1 is used as a Mdi container with WeifenLuo.WinFormsUI.Docking, so I am showing there the Form2 and Form3.You are doing it wrong. You are creating both Form2 and Form3 instances in Form1 (MDI). Then in Form2 you are creating another instance of Form3. It's completely wrong.I suppose that Form3 is just form with progress bar control only to show operation progress to the user and there is no other use of this form.1. In Form1 you need to create and show Form2 only:var form2 = new Form2();form2.Show();2. Then create Form3 inside Form2:private Form3 myOtherForm = new Form3();public doWork(){ this.myOtherForm.Show(); foreach (Element zenVar in lstAlarms) { //Increment ToolStrip this.myOtherForm.IncrementProgessBar = 1; //my work } this.myOtherForm.Hide();}Second solution is to create Form2 and Form3 in Form1 and then pass reference to created Form3 instance directly to Form2 via constructor.But in my opinion both solutions are a bad design. For simple, home project with 4-5 forms it can be 'acceptable' but for medium/large size projects you should consider using some Design Patterns. For instance:1. Model-View-Presenter - which can help you separate your app layers2. Create some kind AppHost/Shell to manage object communication, especially when you're dealing with many forms and want to pass data between them.3. And most important use S.O.L.I.D. programming :)You can find many articles about SOLID and Design Patterns here on CP. Good Luck.I hope that help you.Hi, I resolved it with EventHandler, like this:Form3public Form3(){ InitializeComponent(); Form.UpdateProgressBar += new ProgressBarEventHandler(OnProgressBarUpdate);}public void OnProgressBarUpdate(object sender, ProgressBarEventArgs e){ if (e.ProgressBarIncrement != 0) progressBar_Status.Increment(e.ProgressBarIncrement );}public delegate void ProgressBarEventHandler(object sender, ProgressBarEventArgs e);public class ProgressBarEventArgs{ public ProgressBarEventArgs(int intIncrement) { ProgressBarIncrement = intIncrement; } public int ProgressBarIncrement { get; private set; }}In Form2:public static event ProgressBarEventHandler UpdateProgressBar;public doWork(){ foreach (Element zenVar in lstAlarms) { //Increment Progressbar UpdateProgressBar(this, new ProgressBarEventArgs(0, 1, false)); //my work }} 这篇关于从另一个WindowsForm更新ProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-24 18:06