Windows应用程序从子窗体启用父窗体按钮 [英] Windows application enable parent form button from child form

查看:57
本文介绍了Windows应用程序从子窗体启用父窗体按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试使用多个表单创建我自己的下一个/后退向导控件。

我在一个场景中遇到问题。

我有父母形式的下一个/后退按钮。默认情况下,下一个按钮将在子窗体中进行少量验证后启用false我试图启用父下一个按钮。

我无法实现它我尝试过下面的代码。



我的尝试:



默认我的父母表格

Step1 my儿童表格



我在我的孩子表格中尝试以下代码Step1:



默认d =新默认();

d.btnNext.Enabled = true;



--------------- -----------



(this.Owner默认).btnNext.Enabled = true;



--------------------------



/ /默认p =(默认)this.Owner;

控制[] c = p.Controls.Find(btnNext,true);

按钮b =(按钮)c [0];

b.Enabled = false;

解决方案

向导中控制流的本质 UI要求你:



1.保持可用的列表o f你的向导表格



2.跟踪哪个向导表格是当前的表格



3。提供一种方法让向导表单在单击下一个或上一个按钮时向主表单发出信号。



4.提供主表单中的逻辑以接收来自向导表单的信号,并按顺序显示正确的下一个向导表单。



那么,让我们来看看它是如何形成的示例完成:



假设这是一个带有主窗体的WinForms项目,它创建了向导窗体的实例:



I.在向导形式中:假设每个都有Buttons'GoNext和'GoPrevious

  public  Action GoNext; 
public Action GoPrevious;

private void btnNext_Click(对象发​​件人,EventArgs e)
{
if (GoNext!= null )GoNext();
}

private void btnPrevious_Click( object sender,EventArgs e)
{
if (GoPrevious!= null )GoPrevious();
}

在主窗体中:

  private   const   int  NWizardPages =  4 ; 

私人 WizardForm currentWizard;
private int currentWizardPage;

private readonly 列表< WizardForm> WzFormList = new List< WizardForm>();

private void Form1_Load(对象发​​件人,EventArgs e)
{
for var i = 0 ; i < NWizardPages; i ++)
{
var wz = new WizardForm();
wz.Text = string .Format( 向导页面:{0},i + 1 );
wz.ControlBox = false ;

wz.GoNext = OnGoNext;
wz.GoPrevious = OnGoPrevious;

WzFormList.Add(wz);
wz.Visible = false ;
}

currentWizard = WzFormList [ 0 ];
currentWizard.Show();
}

private void OnGoNext()
{
currentWizard.Hide();

currentWizardPage ++;

if (currentWizardPage == NWizardPages)currentWizardPage = 0 ;

currentWizard = WzFormList [currentWizardPage];
currentWizard.Show();
}

私有 void OnGoPrevious()
{
currentWizard.Hide();

currentWizardPage--;

if (currentWizardPage < 0
{
currentWizardPage = NWizardPages - 1 ;
}

currentWizard = WzFormList [currentWizardPage];
currentWizard.Show();
}

// 无法隐藏主表单,直到它出现之后'已显示
私有 void WizardBase_Shown( object sender,EventArgs e)
{
Visible = false ;
}

这个简单的例子省略了典型的任务......就像为用户提供退出向导并返回主窗体的方式......提供了必要的逻辑: br />


0.a.创建向导表单列表,以及指向当前向导表单的当前索引的变量,以及当前向导表单。



0.b.在每个WizardForm中提供公共代表(此示例使用'Action Type委托,作为定义事件委托的捷径),当单击每个向导表单上的下一个或上一个按钮时,将触发MainForm的信号。



1.在主窗体的加载事件中,向导表单中触发的代码将注入每个向导表单上的公共操作代理。 />


2.主表单中的'OnGoPrevious和'OnGoNext处理程序做正确的事情,在序列中显示下一个或以前的向导表单。



注意:在此示例中,通知按钮...'GoNext,'GoPrevious ...代码不会将信息传递给主窗体中的处理程序。在实际的用例中,您可能希望使用这些事件(按下按钮)将数据从向导表单传递到应用程序的中心逻辑;或者,您可能想使用其他方法。


请参阅向导表单实施 [ ^ ]。

Hello,
Im trying to create my own Next/back wizard control using multiple form's.
In that im facing a problem in one scenario.
I have my next/back button in parent form. Default the next button will be enabled false after few validation in child form im trying to enable parent Next button.
I cant able make it happen i have tried bellow code.

What I have tried:

Default My parent form
Step1 my child form

My trying this below code from my child form Step1:

Default d = new Default();
d.btnNext.Enabled = true;

--------------------------

(this.Owner as Default).btnNext.Enabled = true;

--------------------------

//Default p = (Default)this.Owner;
Control[] c = p.Controls.Find("btnNext", true);
Button b = (Button)c[0];
b.Enabled = false;

解决方案

The "essence" of the flow of control in a "Wizard" UI requires you to:

1. maintain a usable List of your Wizard Forms

2. keep track of which Wizard Form is the current Form

3. provide a way for the Wizard Form to signal the Main Form when its 'Next or 'Previous Button is clicked.

4. provide the logic in the Main Form to receive the signal from the Wizard Forms, and act to show the correct next Wizard Form in the sequence.

So, let's take a look at an example of how that can be done:

Assuming this is a WinForms project with a Main Form that creates the instances of the Wizard Forms:

I. in the Wizard forms: assuming each has Buttons 'GoNext and 'GoPrevious

public Action GoNext;
public Action GoPrevious;

private void btnNext_Click(object sender, EventArgs e)
{
    if (GoNext != null) GoNext();
}

private void btnPrevious_Click(object sender, EventArgs e)
{
    if (GoPrevious != null) GoPrevious();
}

In the Main Form:

private const int NWizardPages = 4;

private WizardForm currentWizard;
private int currentWizardPage;

private readonly List<WizardForm> WzFormList = new List<WizardForm>();

private void Form1_Load(object sender, EventArgs e)
{
    for (var i = 0; i < NWizardPages; i++)
    {
        var wz = new WizardForm();
        wz.Text = string.Format("Wizard Page: {0}", i + 1);
        wz.ControlBox = false;

        wz.GoNext = OnGoNext;
        wz.GoPrevious = OnGoPrevious;

        WzFormList.Add(wz);
        wz.Visible = false;
    }

    currentWizard = WzFormList[0];
    currentWizard.Show();
}

private void OnGoNext()
{
    currentWizard.Hide();

    currentWizardPage++;

    if (currentWizardPage == NWizardPages) currentWizardPage = 0;

    currentWizard = WzFormList[currentWizardPage];
    currentWizard.Show();
}

private void OnGoPrevious()
{
    currentWizard.Hide();

    currentWizardPage--;

    if (currentWizardPage < 0)
    {
        currentWizardPage = NWizardPages - 1;
    }

    currentWizard = WzFormList[currentWizardPage];
    currentWizard.Show();
}

// can't hide the Main Form until after it is 'Shown
private void WizardBase_Shown(object sender, EventArgs e)
{
    Visible = false;
}

This simple example, which omits typical tasks ... like providing a way for the user to exit the wizard and return to the Main Form ... provides the necessary logic by:

0.a. creating a List of Wizard Forms, and variables that point to the current Index of the current Wizard Form, and the current Wizard Form.

0.b. providing public Delegates in each WizardForm (this example uses the 'Action Type delegate, as a short-cut for defining an Event Delegate) that will trigger the signaling of the MainForm when the 'Next or 'Previous Buttons on each Wizard Form are clicked.

1. in the Load Event of the Main Form, the code to be triggered in the Wizard Forms is injected into the public Action delegates on each Wizard Form.

2. the 'OnGoPrevious and 'OnGoNext handlers in the Main Form do the right thing to show the next, or previous, Wizard Form in the sequence.

Note: In this example, the notification Buttons ... 'GoNext, 'GoPrevious ... code do not pass information to their handlers in the Main Form. In a real-world use-case, you will probably want to use those events (the button-press) to pass data from the wizard forms to the central logic of your app; or, you may want to use other methods.


See Wizard Form Implementation[^].


这篇关于Windows应用程序从子窗体启用父窗体按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆