Main forms, ShowDialog and MessageBox

  • Thread starter Thread starter Paul
  • Start date Start date
P

Paul

Hi all

Have scoured the groups, but can't find a match on my problem, so I've
finally decided to post.
I have an app thats start-up object is 'frmMain'. From that form other
forms are called via 'ShowDialog', as the application represents sections on
a questionaire. All this works OK. Trouble starts when using 'MessageBox'
to display messages to the user regarding invalid entries etc - when the OK
on the message box is tapped, the new form gets killed off too. Is this my
error, or a bug? Have also tried using application .run to fire the
sub-sections (though I feel this is not best) but this has other isses with
execptions getting raised due to incorrect parameters.

Any info would be apprieciated.
 
Paul,

Without code it is kind of hard to find out where your
problem is. I just wrote a little demo scenario to show
you how you can deal with MessageBoxes in your sub-
sections without closing them. Assume you have an "OK"
button in your subform in which you validate input and
return to the main form or show an error message. Here is
a code snippet that would do that for you:

private void button1_Click(object sender,
System.EventArgs e)
{
if (textBox1.Text.Length == 0)
{
// show error + stay in sub form
MessageBox.Show("Need a value");
}
else
{
// input ok, show frmMain again.
this.DialogResult = DialogResult.OK;
}
}

Hope this helps you.

Regards,
Maarten Struys
PTS Software
 
Maarten,

Thanks for the reply, and appreiciate about lack of code - have pasted below
a cut down version of my validation code. All works fine as long as the
messagebox doesn't fire. If it does, in stead of exiting the sub, is closes
the form. As it goes, I circumvented the problem by checking the validation
flag in the Deactivate event. This probably isn't textbox, but it did
provide me with a viable work-around.

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOK.Click
Dim AdddressArray(3) As String

'// if not readonly
If Not p_ReadOnly Then

'// validate data

'// set validation flag
p_Validating = True

'// check for postcode
If Len(txtPostcode.Text) = 0 Then
MessageBox.Show(Captions(CaptionKey.msg_AddressError),
Captions(CaptionKey.title_AddressError), MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)

Exit Sub
End If

'// unset validating flag
p_Validating = False

End If

Me.Close()

End Sub



Private Sub frmSection02_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate
If Not p_Validating Then
Me.Close()
End If

End Sub





Many thanks

Paul
 
Back
Top