User Controls and Form Button Interaction

E

Edwin

I will do my best to try an explain this as accurately and as briefly as
possible.

I am attempting to write a Windows application. In it I have one form that
uses panels; a Top Panel, Left Panel and Right Panel. There are no other
forms. The form itself contains buttons, that when pressed, determine which
UserControl will be loaded into the Right Panel. The buttons are NOT part
of the UserControl. As I press the correct buttons on my Windows form, the
corresponding UserControl is correctly displayed in my Right Panel.
Everything up to this point in my application works great!

Each UserControl is intended to collect information and obviously store them
for later use. As of right now there are only TextBox and Label controls in
the UserControls that are loaded. Make sense so far? Good!

Below I have pasted what I hope to be sufficient code from what I have so
far in an attempt to help understand what I am asking and trying to do. The
"Windows Form" is what contains the NEXT button control that allows a user
to move ahead. When the next UserControl is loaded the NEXT button is
disabled because the fields in that UserControl need to be validated first.
The OnPaint method of each UserControl uses the WizardData class to help me
determine where I am in the entire process.

If you notice in the UserControl sets the NEXT button to disabled. I do not
want the user to move ahead until all TextBox control fields are valid.
When they are valid I would like to have the NEXT button enabled.

It is easy for the NEXT button to be disabled because it is set as part of
the UserControl being loaded in the UpdateWizardControl() method. I think
what I need is a delegate or event when all the TextBox controls on
UserControl are validated.

I hope that this all makes sense! Thank you all for your assistance.

//////////////////////// Windows Form ///////////////////////
public static System.Boolean IsNextButtonEnabled;

private void buttonNext_Click(object sender, EventArgs e)
{
System.Int32 CurrentWizardStep =
WizardControls.WizardData.CurrentWizardStep;
UpdateWizardControl(++CurrentWizardStep);
}

private void UpdateWizardControl(System.Int32 WizardStep)
{
System.String ErrorString;

// Verify that the wizard step needed is not out of the enumeration
range used.
if (
WizardStep <=
System.Enum.GetValues(typeof(WizardControls.WizardData.WizardSteps)).Length
&&
WizardStep >=
(System.Int32)WizardControls.WizardData.WizardSteps.Step1)
{
/// Determine which user control is to be displayed.
switch (WizardStep)
{
case 1:
this.panelRight.Controls.Add(new Default());
break;
case 2:
this.panelRight.Controls.Add(new AccountInformation());
break;
case 3:
this.panelRight.Controls.Add(new TestType());
break;
case 4:
this.panelRight.Controls.Add(new TestOptions());
break;
}

// Remove the user control that is no longer needed.
this.panelRight.Controls.Remove(WizardControls.WizardData.ActiveUserControl);

// Update the top panel control properties.
this.labelTopPanelTitle.Text = WizardControls.TopPanel.Title;
this.labelTopPanelDescription.Text =
WizardControls.TopPanel.Description;

// Set the user's option to move forward or backward depending on
the current wizard step.
if (WizardStep ==
(System.Int32)WizardControls.WizardData.WizardSteps.Step1)
{
this.buttonBack.Enabled = false;
this.buttonNext.Enabled = true;
}
else
{
this.buttonBack.Enabled = true;
this.buttonNext.Enabled = IsNextButtonEnabled; // Determined by
the loaded user control.
}

// Disable the ability to move forward if the user is at the end of
the wizard.
if (WizardStep ==
(System.Int32)WizardControls.WizardData.WizardSteps.Step4)
{
this.buttonNext.Enabled = false;
}
}
// The wizard step needed is out of the enumeration range used.
else
{
ErrorString = "The wizard step provided is out of range.";
this.CustomException(this, ErrorString);
return;
}
}

//////////////////////// UserControl ///////////////////////
private void AccountInformation_Load(object sender, EventArgs e)
{
// Disable the Next button until all fields are validated.
InheritedForm.IsNextButtonEnabled = false;

.....
}

protected override void OnPaint(PaintEventArgs e)
{
WizardControls.WizardData.ActiveUserControl = this;
WizardControls.WizardData.CurrentWizardStep =
(System.Int32)WizardControls.WizardData.WizardSteps.Step2;
base.OnPaint(e);
}

//////////////////////// Properties ///////////////////////
namespace WizardControls
{
public class WizardData
{
public enum WizardSteps : int
{
Step1 = 1, /// The start of the wizard application launch
introduction.
Step2 = 2,
Step3 = 3,
Step4 = 4 /// The end of the wizard application.
}

private static System.Int32 myCurrentWizardStep;
private static System.Windows.Forms.UserControl myActiveUserControl;

public static System.Windows.Forms.UserControl ActiveUserControl
{
get { return myActiveUserControl; }
set { myActiveUserControl = value; }
}
}
}
 
J

joecool1969

I will do my best to try an explain this as accurately and as briefly as
possible.

Well, you got kinda long-winded anyway. :)

But if your basic question is how do you enable/disable controls on
the app's main form from a usercontrol that has been loaded onto the
form, then yes, you need to set up an event to do that.

In the usercontrol:

public event EventHandler EnableNextButton;

protected virtual void OnEnableNextButton()
{
EventHandler handler = EnableNextButton;

if (handler != null)
{
handler(this, new EventArgs());
}
}

in the event that needs to enbale the main form's next button:

this.OnEnableNextButton();

in the app's main form:

public void OnEnableNextButtonEvent(object sender, EventArgs e)
{
this.buttonNext.Enabled = true;
}

In the app's main form shortly after the custom usercontrol is loaded
to the Panel's control collection:

this.myusercontrol.EnableNextButtonEvent += new EventHandler
(OnEnableNextButtonEvent);

NOTE: If you have a custom usercontrol that is added in the designer,
you can set this event subscription in the designer.

ANOTHER NOTE: Keyed in on the fly, totally untested.

If you want to make a single event that can either enable ot disable
the button, then you will need a custom event args class that has a
boolean property. I'll leave that exercise up to you. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top