Need Help Setting a Parent Control Property from a DialogBox in VB.NET

Hello,

I am building a PC application in VB.NET using Visual Studio 2008 and the .NET 3.5 Framework.

I use the following code to pop dialogboxes when certain buttons are pressed:

Dim T As Type = Type.GetType(Application.ProductName & "." & dlgname, True, True)
Dim f As Form = CType(Activator.CreateInstance(T), Form)
f.BackgroundImage = Image.FromFile(apppath & "/images/popuptemplate.jpg")
f.ShowDialog(Me)

In the above, dlgname is a text string with the name of the dialogbox I wish to pop.  The above code works perfectly.

In one of my dialogbox's, I need to enable a disabled button on the tab control tab within the main window that contains the button which pops the dialogbox (by calling the above code).  I'm using the code below to try to enable the disabled button.  The code compiles, but the line below throws a generic runtime error without much diagnostic information (what little is provided seems to say that the entity referred to does not exist, probably because my syntax is wrong):

Me.ParentForm.Controls.Item("tcMain").Controls.Item("tabManageAssets").Controls.Item("btnMAEditAsset").Enabled = True

In the above, btnMAEditAsset is the button I wish to change from disabled to enabled state.  Both the popup launcher button and the button whose enabled state I wish to change are contained within tabManageAssets.  This command is my final line of the dialogbox code before Me.Close().  tcMain is the tab control on the main form, and tabManageAssets is the operant tab containing the two buttons.

Can anyone tell me what the correct syntax is to set the enabled state for btnMAEditAsset?

Thanks, Stu Engelman
stuengelmanAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You are displaying the form with ShowDialog():

    f.ShowDialog(Me)

So code in the main form stops until the dialog is closed right?

Why can't you simply enable the button from the main form itself AFTER the dialog is closed?

    f.ShowDialog(Me)
    btnMAEditAsset.Enabled = True

If this should only occur for a certain dialog, then you can base it off of "dlgname":

    f.ShowDialog(Me)
    If dlgname = "something" Then
        btnMAEditAsset.Enabled = True
    End If
0
 
Miguel OzSoftware EngineerCommented:
let me know if:
- Me.ParentForm is valid instance.
- Me.ParentForm.Controls is valid instance.
- btnMAEditAsset exist in desing mode or created at run time.
- Name of parent form class
0
 
stuengelmanAuthor Commented:
Hi Mas,

btnMAEditAsset is created at design time.

The parent form is named frmMain.

How do I check for the valid instances you mentioned?  Could I do something like:

msgbox(isdbnull(Me.ParentForm))
msgbox(isdbnull(Me.ParentForm.Controls))

The idea would be true=invalid and false=valid.

Thanks, Stu
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
stuengelmanAuthor Commented:
Hi Mas,

I tried:

MsgBox(Me.ParentForm Is Nothing) - returned true

MsgBox(Me.ParentForm.Controls Is Nothing) - abended with error message "object reference not set to an instance of an object"

Looks like both references are not recognized in the dialogbox.

Stu
0
 
stuengelmanAuthor Commented:
Hi Mas,

Another comment.  I just realized I need to refresh the main form just before or after closing the dialogbox (to update some comboboxes), so I'm thinking a better approach would be to just let the code for the tab in the main form handle the button enabling/disabling we talked about earlier.

I have a button created on the tab programmatically (i.e., created at run time) which handles the refresh function for the form/tab.  Sorry for the switch, but what I'd really like to do rather than enabling the design time button in the popup is programmtically click the runtime created refresh button on the form/tab that pops the dialogbox (it is named btnMAReset).

Sorry again for the switch.

Stu
0
 
stuengelmanAuthor Commented:
Hi Idle,

Thanks for the comment.  That approach won't work because I need to execute the line of code only if the user presses the OK button on the popup.  The line of code is actually in the handler for the OK button for the dialog, not the dialog's load event.

Secondly, I changed the specs for this ticket.  Kindly see my 2012-09-14 21:40:26 post.

Thanks again, Stu
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"I need to execute the line of code only if the user presses the OK button on the popup."

This is exactly what ShowDialog() is for.  In the OK button of your dialog, set DialogResult to "OK".

    Private Sub btnOK_Click(...) Handles btnOK.Click
        Me.DialogResult = DialogResult.OK
    End Sub

This will get RETURNED by the ShowDialog() call, which you can check:

    If f.ShowDialog(Me) = DialogResult.OK Then
        btnMAEditAsset.Enabled = True
    End If
0
 
stuengelmanAuthor Commented:
Hi Idle,

I think this will work.  I have a button handler for the popup button that has the following line: PopDialog("mdgAssets").  The PopDialog function has the lines:

Dim T As Type = Type.GetType(Application.ProductName & "." & dlgname, True, True)
Dim f As Form = CType(Activator.CreateInstance(T), Form)
f.BackgroundImage = Image.FromFile(apppath & "/images/popuptemplate.jpg")
f.ShowDialog(Me)

If I added my lines after PopDialog("mdgAssets") to perform the conditional processing (programmatically clicking a button per the spec revision in my 2012-09-14 21:40:26 post), is it correct that the system will wait to perform the if-then block until the dialog is closed?

Thanks, Stu
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yes...it will stop at the ShowDialog() line until that "dialog" is closed/dismissed.
0
 
stuengelmanAuthor Commented:
Great Idle.  Thank you.  Will test it out and report back.  I've been working for about 13 hours, so I've got to hit the hay, but I'll apprise tomorrow (oops, it's now tomorrow, so I guess I should say "later today").

Thanks again for your help.

Stu
0
 
stuengelmanAuthor Commented:
Hi Idle,

Works perfectly!

Thanks so much, Stu
0
 
stuengelmanAuthor Commented:
Solution was perfect, and very clearly explained.  Great job.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.