[RESOLVED] [2005] Form Show and Showdialog
hi im running this code to load a form and call a public method inside it:
vb Code:
[B] newForm.Show()[/B]
Call newForm.editToolStripButton_Click(sender, e)
but when i use form SHOWDIALOG method it doesnt execute
Call newForm.editToolStripButton_Click(sender, e)
Re: [2005] Form Show and Showdialog
ShowDialog is for external files, not internal forms.
Re: [2005] Form Show and Showdialog
I think you're slightly confused Hack. At least, I can't understand what you are trying to say.
jlbantang, when you call a form's ShowDialog() method, your code will not continue to execute until the form that you have shown as a dialog is closed. Here is a quick test you can use to show the concept:
Code:
Private Sub btnShowDialog_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnShowDialog.Click
Using frm As New newForm()
frm.ShowDialog()
MessageBox.Show("You have reached this line of code.")
End Using
End Sub
Private Sub btnShowRegular_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles btnShowRegular.Click
Using frm As New newForm()
frm.Show()
MessageBox.Show("You have reached this line of code.")
End Using
End Sub
You will notice that in the first instance, the message box only pops up when you close the newForm, whereas in the second instance, it pops up immediately.
You are having a problem because after you use the ShowDialog method, your code does not execute the
Code:
Call newForm.editToolStripButton_Click(sender, e)
until the form is closed, at which point it will fail because there is no longer a form.
Re: [2005] Form Show and Showdialog
Quote:
Originally Posted by Hack
ShowDialog is for external files, not internal forms.
a bit confuse tho. I believed SHOWDIALOG is the same as showing a MODAL FORM in VB6. please correct me if im wrong.
thnx
Re: [2005] Form Show and Showdialog
Yes as Max explained, ShowDialog will block the calling thread until the form shown by the ShowDialog call is closed.
I should also point out that it is considered bad practise to call eventhandlers directly, you should avoid that.
Re: [2005] Form Show and Showdialog
Code that will be called from multiple locations should be wrapped in its own method.
Re: [2005] Form Show and Showdialog
thnx. so i wrapped the codes in its own method and still nothing seems to work. what would you suggest experts? :D
Re: [2005] Form Show and Showdialog
what does not seem to work and how are you trying to do it?
Re: [2005] Form Show and Showdialog
Well things are wrong with calling into a sub that listens to a UI elements events. You are simulating that the relevant object sent the message. Objects are supposed to tell the outside world that something happened to the object, not the outside world telling the rest of the program that an object raised an event when it didn't. You can do it, but your screwing with the way OOP was meant to operate.
What to do?
Just make another sub that contains the code you want to execute and call it instead.
Re: [2005] Form Show and Showdialog
you don't need to call the form's
vb Code:
editToolStripButton_Click(sender, e) sub
. if you do than you need to provide the event args for the click and that is what i don't see you doing. Instead call the button's performClick method.
vb Code:
newForm.Show()
newForm.editToolStripButton.PerformClick()
Re: [2005] Form Show and Showdialog
Even then the code is a little screwball because you are using the sub both to catch events from within an object, and for external use.
That sort of muddies the water of the purpose of event handlers.
And before you say it...
Remember "Goto"
Re: [2005] Form Show and Showdialog
i supposed to used SHOWDIALOG but since the required step wont allow me to perform a method local to the external form i will use SHOW method instead and that fix all issues.
tnx.