从子表单控制父表单的进度条。 [英] Controlling progress bar of parent form from the child form.
问题描述
Hello Everyone,
我想从子表单控制父表单上的进度条。父表单有一个按钮和一个进度条。单击该按钮时,将打开子窗体,子窗体将执行其任务一段时间。当任务完成时,我希望进度显示在父表单的进度条上。
我已将修饰符设置为Public并写入这些行代码仍然无法实现。
表格1:
Hello Everyone,
I want to control a progress bar on the parent form from the child form. The parent form has one button and a progress bar. On clicking that button a child form is opened and the child form does its task for some time. As the task is being done, I want the progress to be displayed on the progress bar of the parent form.
I have set the modifiers to Public and wrote these lines of code. Still, couldn't make it possible.
Form 1:
private void button1_Click(object sender, EventArgs e){
Form2 f2 = new Form2();
f2.Show();
}
表格2:
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Form1 f1 = new Form1();
int j;
for (j = 0; j < 100; j++)
{
f1.progressBar1.Value = j;
f1.progressBar1.Refresh();
f1.progressBar1.Update();
Console.WriteLine("j={0}", j);
}
}
}
我尝试了什么:
还尝试在按钮事件中关联表单2代码。
What I have tried:
Also tried associating the form 2 code in a button event.
public partial class Form2 : Form
{
Form1 f1 = new Form1();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int j;
for (j = 0; j < 100; j++)
{
f1.progressBar1.Value = j;
f1.progressBar1.Refresh();
f1.progressBar1.Update();
Console.WriteLine("j={0}", j);
}
}
}
推荐答案
看看你的代码:
Look at your code:
public Form2()
{
InitializeComponent();
Form1 f1 = new Form1();
当你创建一个 new
的任何实例时,这正是你得到的:a'获得新对象。你没有得到现有的副本,你没有得到现有的副本 - 你得到一个全新的。
你不能这样做!
你想要做的是错误的,非常错误的 - 孩子形式不应该;甚至知道父存在:这就是为什么控制私人
开始 - 这样你的外面的世界就无法与它们一起玩!
使Form1进展再次禁用私有,并在Form2中创建一个事件,Form1处理该事件以指示进度:转移两种表格之间的信息,第2部分:儿童到父母 [ ^ ]应该提供帮助并提供一个示例。
When you create a new
instance of anything, that is exactly what you get: a 't get new object. You don't get a copy of an existing one, you don't get a reference to an existing one - you get an entirely new one.
You can't do it like that!
And what you are trying to do is wrong, very wrong - the "child" form shouldn;t even know that the "parent" exists: that's why controls are private
to start with - so that your "outside world" can't play with them!
Make the Form1 progress bar private again, and create an event in Form2 which Form1 handles to indicate progress: Transferring information between two forms, Part 2: Child to Parent[^] should help and provides an example.
尝试
表格1
try
Form 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void UpdateProgress(int j) { // public method to invoke from the child form
this.progressBar1.Value = j;
this.progressBar1.Refresh();
this.progressBar1.Update();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ParentFormObject = this; // pass the parent form object
f2.Show();
}
}
表格2
Form 2
public Form1 ParentFormObject { get; set; } // to access parent form object
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int j;
for (j = 0; j < 100; j++)
ParentFormObject.UpdateProgress(j); // access the parent form public method
}
这篇关于从子表单控制父表单的进度条。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!