?
Solved

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

Posted on 2012-09-14
12
Medium Priority
?
752 Views
Last Modified: 2012-09-15
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
0
Comment
Question by:stuengelman
  • 8
  • 3
12 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 38400888
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
 

Author Comment

by:stuengelman
ID: 38400900
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
 

Author Comment

by:stuengelman
ID: 38400953
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

Author Comment

by:stuengelman
ID: 38400958
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38400967
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
 

Author Comment

by:stuengelman
ID: 38401006
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38401019
"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
 

Author Comment

by:stuengelman
ID: 38401025
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38401028
Yes...it will stop at the ShowDialog() line until that "dialog" is closed/dismissed.
0
 

Author Comment

by:stuengelman
ID: 38401077
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
 

Author Comment

by:stuengelman
ID: 38402526
Hi Idle,

Works perfectly!

Thanks so much, Stu
0
 

Author Closing Comment

by:stuengelman
ID: 38402529
Solution was perfect, and very clearly explained.  Great job.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question