ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
Go Back  Xtreme Visual Basic Talk > > > ShowDialog not working on a sub form - VB2010


Reply
 
Thread Tools Display Modes
  #1  
Old 05-12-2015, 05:22 AM
TimMakins TimMakins is offline
Newcomer
 
Join Date: May 2006
Posts: 3
Default ShowDialog not working on a sub form - VB2010


I have 2 forms: a main-form and a sub-form. Both forms has some buttons, a colordialog, and a fontdialog. The sub-form is opened via a menustrip item on the main-form using this code:

Code:
    Private Sub ChangeColorsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeColorsToolStripMenuItem.Click
        select_colors.ShowDialog()
    End Sub
When the sub-form is opened, neither of the showdialog items work:

Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If FontDialog1.ShowDialog = DialogResult.OK Then
            Me.Font = FontDialog1.Font
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        ColorDialog1.Color = Me.BackColor
        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.BackColor = ColorDialog1.Color
        End If
    End Sub
Clicking button 1 or two on the sub-form produces no result at all, whereas if I put the same code and buttons on the main form, both font and color dialogs show when a button is clicked.

I have tried opening the sub-form with select_colors.Show() as well, but it makes no difference.

How can I fix this, please?
Reply With Quote
  #2  
Old 05-12-2015, 08:47 AM
TimMakins TimMakins is offline
Newcomer
 
Join Date: May 2006
Posts: 3
Default

After trying to change any line to see what the difference was, I discovered that my sub-form contained the line: 'InitializeComponent()' in its load event. If I removed this line, the color and font dialogs both started working.

No doubt someone can explain why?!
Reply With Quote
  #3  
Old 05-13-2015, 05:54 PM
hDC_0ShowDialog not working on a sub form - VB2010 hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 559
Default A little "behind-the-scenes" info on InitializeComponent() ..

Quote:
Originally Posted by TimMakins
No doubt someone can explain why?!
Just in case no one else notices this thread I thought I would chime in with a little info.

Here is a social.msdn forum thread that talks a little about calling the InitializeComponent() method.

This is the paragraph that is most important in that thread:
Quote:
Originally Posted by kaymaf
To my understanding from VS2005 and up, you need to call InitializeComponent() method
when you add your own constructors to your windows form project.
Also this only apply to windows form application but not console. By the default,
the project will automatically called InitializeComponent() from designer.vb page
if there are no constructors added by developer.
Do you understand what this means?
There are actually two different places where the InitializeComponent() can (may) happen.
One is inside the "Form1.Designer.vb" file associated with Form1

You normally won't see the "Form1.Designer.vb" file in the Solution Explorer panel unless you click on the "Show All Files" icon.

As it says on this page it is different if you are working in C# instead of VB,
Quote:
In C# access to this method is always visible through form constructor.
Simply position the cursor in InitializeComponent method call and press F12 in VS.NET
and Form .designer file will open to show the implementation of the InitializeComponent method.

In VB.NET the Visual Studio hides the access to InitializeComponent method.
Actual method implementation is in Form.designer.vb file for that form.
File is accessible via Solution Explorer once you have clicked on the Show All Files toolbar button
which you can find on top of the Solution Explorer window.
So if InitializeComponent is in the Form Designer file why could/would it (also) be in the New sub for the form? (you might be wondering..)

Take a look at these two threads (1, 2).

What is happening with the code in those two threads is called "overloading"
(specifically overloading the form constructor):
Code:
'You can overload New for a form by adding another method 
'named 'New' with different parameters than the original:
Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Public Sub New(ByVal strMessage As String)
Me.New()

' Add any additional code here
'for instance
'Populate the textbox
'TextBox1.Text = strMessage
End Sub
So what about your case where you put (probably accidentally)
the 'InitializeComponent()' in the form load event.

In that case .Net probably thought you were try going to do an overload,
and probably ignored the InitializeComponent() in your form's form.designer.vb file
(which also has code to provide the support for the event handling of controls)
and would have been ready to run you own custom overloaded version of
InitializeComponent() --however you didn't add any special code after the
InitializeComponent() call in your form load so it did nothing,
(executed no overloading).

Or in other words..you basically just "faked out" the normal InitializeComponent() in the form.designer.vb
by replacing it with an unneeded/unnecessary InitializeComponent() overloading "stub".

Once the extra InitializeComponent() was removed from the form load code/event,
.Net returned to it's normal way of doing things --using
the other InitializeComponent() sub code in the form.designer.vb file.

This msdn blog has some additional info (read the part about how the designer deserializes the code in InitializeComponent..)

There is also something special you have to do when you have some form inheritance going on.

Read section 6 ("How to change the properties of the inherited controls at design time?") on this code project tutorial
that explains what must be done to change the access modifiers inside the InitializeComponent() sub for the inheritance to work correctly.

Hopefully these few tiny tidbits of information dispel a little of the mystery behind what you experienced.

Last edited by hDC_0; 05-13-2015 at 06:16 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010 ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
 
ShowDialog not working on a sub form - VB2010
ShowDialog not working on a sub form - VB2010
 
-->