showdialog not working as expected

W

WvH

Hi,

When I create a new application, with just one button, then this code works
as expected:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim f As New ColorDialog
f.ShowDialog()
End Sub

As soon as I add some controls (a PictureBox, some NumericUpDown controls,
....) the ShowDialog does not work anymore: when I press the button, the
dialog is invisible. When I switch to another application and then back to
my app, the dialog box instantly becomes visible.

I can also press the <Alt> key to make the dialog visible. The same
behaviour goes for FontDialog, OpenFileDialog, ...

Thanks,

WvH
 
A

Angel J. Hernández M.

Pass a reference to the dialog's owner through the constructor in this case
Me.

Regards,
 
C

Cor Ligthert [MVP]

WvH,

Did you only drag those buttons or add as well something as topmost?

Cor
 
J

Jay B. Harlow [MVP - Outlook]

WvH,
As Angle suggests pass the "owner" to the ShowDialog.

Something like:

| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| Dim f As New ColorDialog
| f.ShowDialog(Me)
| End Sub

Note it goes on the call to ShowDialog, not the constructor.

Hope this helps
Jay

| Hi,
|
| When I create a new application, with just one button, then this code
works
| as expected:
|
| Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles Button1.Click
| Dim f As New ColorDialog
| f.ShowDialog()
| End Sub
|
| As soon as I add some controls (a PictureBox, some NumericUpDown controls,
| ...) the ShowDialog does not work anymore: when I press the button, the
| dialog is invisible. When I switch to another application and then back to
| my app, the dialog box instantly becomes visible.
|
| I can also press the <Alt> key to make the dialog visible. The same
| behaviour goes for FontDialog, OpenFileDialog, ...
|
| Thanks,
|
| WvH
|
|
 
W

WvH

Jay and Angle,

Thanks for the suggestion, but unfortunately no difference.

I'm getting closer to the cause of the problem though: there is 1 PictureBox
on the form, which has only got this code:

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

Dim bit As Bitmap = New Bitmap(20, 20)
PictureBox1.Image = bit

End Sub

If I leave out the PictureBox1.Image = bit , the ColorDialog appears
correctly.

I'm obviously missing something...

Thanks,
WvH
 
W

WvH

Cor,

I just did some basic stuff:

New project, with 1 form, 1 button, 1 colordialog, 1 picturebox. There is
very little code until now (2 subs):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

ColorDialog1.ShowDialog(Me)

End Sub

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

Dim bit As Bitmap = New Bitmap(20, 20)
PictureBox1.Image = bit

End Sub

If I leave out PictureBox1.Image = bit, clicking the button does correctly
show the colordialog.

Thanks,
WvH
 
C

Cor Ligthert [MVP]

WvH,

Your program is terrible looping in the routine where it is painting the
picturebox

Maybe you can set that picturebox.Image in another place than where it tells
that it is painting it after that it is set.

I hope this helps,

Cor
 
J

Jay B. Harlow [MVP - Outlook]

WvH,
As Cor states:

You are setting the PictureBox1.Image which is causing the PictureBox1.Paint
event to be raised which sets the PictureBox1.Image property, which is
causing the PictureBox1.Paint event to be raised which sets the
PictureBox1.Image property, which is causing the PictureBox1.Paint event to
be raised, which continues until you terminate your app.

For a demonstration of what is happening, try the following code:

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Debug.WriteLine("Begin PictureBox1_Paint")

Debug.Indent()
Debug.WriteLine("Begin set image")
Dim bit As Bitmap = New Bitmap(20, 20)
PictureBox1.Image = bit
Debug.WriteLine("End set image")
Debug.Unindent()

Debug.WriteLine("End PictureBox1_Paint")
End Sub

Private Sub PictureBox1_Invalidated(ByVal sender As Object, ByVal e As
System.Windows.Forms.InvalidateEventArgs) Handles PictureBox1.Invalidated
Debug.WriteLine("PictureBox1_Invalidated")
End Sub



As Cor suggests you need to remove setting the PictureBox1.Image in the
PictureBox1.Paint event handler.

Hope this helps
Jay

| Jay and Angle,
|
| Thanks for the suggestion, but unfortunately no difference.
|
| I'm getting closer to the cause of the problem though: there is 1
PictureBox
| on the form, which has only got this code:
|
| Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
| System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
|
| Dim bit As Bitmap = New Bitmap(20, 20)
| PictureBox1.Image = bit
|
| End Sub
|
| If I leave out the PictureBox1.Image = bit , the ColorDialog appears
| correctly.
|
| I'm obviously missing something...
|
| Thanks,
| WvH
|
|
| "Jay B. Harlow [MVP - Outlook]" <[email protected]> schreef in
bericht
| | > WvH,
| > As Angle suggests pass the "owner" to the ShowDialog.
| >
| > Something like:
| >
| > | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| > | System.EventArgs) Handles Button1.Click
| > | Dim f As New ColorDialog
| > | f.ShowDialog(Me)
| > | End Sub
| >
| > Note it goes on the call to ShowDialog, not the constructor.
| >
| > Hope this helps
| > Jay
| >
| > | > | Hi,
| > |
| > | When I create a new application, with just one button, then this code
| > works
| > | as expected:
| > |
| > | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| > | System.EventArgs) Handles Button1.Click
| > | Dim f As New ColorDialog
| > | f.ShowDialog()
| > | End Sub
| > |
| > | As soon as I add some controls (a PictureBox, some NumericUpDown
| > controls,
| > | ...) the ShowDialog does not work anymore: when I press the button,
the
| > | dialog is invisible. When I switch to another application and then
back
| > to
| > | my app, the dialog box instantly becomes visible.
| > |
| > | I can also press the <Alt> key to make the dialog visible. The same
| > | behaviour goes for FontDialog, OpenFileDialog, ...
| > |
| > | Thanks,
| > |
| > | WvH
| > |
| > |
| >
| >
|
|
 
W

WvH

Jay & Cor,

After creating a seperate subroutine for assigning the image, everything is
working as expected.

Thanks for explaining and for my first debugging lesson ;-)

WvH



Jay B. Harlow said:
WvH,
As Cor states:

You are setting the PictureBox1.Image which is causing the
PictureBox1.Paint
event to be raised which sets the PictureBox1.Image property, which is
causing the PictureBox1.Paint event to be raised which sets the
PictureBox1.Image property, which is causing the PictureBox1.Paint event
to
be raised, which continues until you terminate your app.

For a demonstration of what is happening, try the following code:

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
Debug.WriteLine("Begin PictureBox1_Paint")

Debug.Indent()
Debug.WriteLine("Begin set image")
Dim bit As Bitmap = New Bitmap(20, 20)
PictureBox1.Image = bit
Debug.WriteLine("End set image")
Debug.Unindent()

Debug.WriteLine("End PictureBox1_Paint")
End Sub

Private Sub PictureBox1_Invalidated(ByVal sender As Object, ByVal e As
System.Windows.Forms.InvalidateEventArgs) Handles PictureBox1.Invalidated
Debug.WriteLine("PictureBox1_Invalidated")
End Sub



As Cor suggests you need to remove setting the PictureBox1.Image in the
PictureBox1.Paint event handler.

Hope this helps
Jay

| Jay and Angle,
|
| Thanks for the suggestion, but unfortunately no difference.
|
| I'm getting closer to the cause of the problem though: there is 1
PictureBox
| on the form, which has only got this code:
|
| Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As
| System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
|
| Dim bit As Bitmap = New Bitmap(20, 20)
| PictureBox1.Image = bit
|
| End Sub
|
| If I leave out the PictureBox1.Image = bit , the ColorDialog appears
| correctly.
|
| I'm obviously missing something...
|
| Thanks,
| WvH
|
|
| "Jay B. Harlow [MVP - Outlook]" <[email protected]> schreef in
bericht
| | > WvH,
| > As Angle suggests pass the "owner" to the ShowDialog.
| >
| > Something like:
| >
| > | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| > | System.EventArgs) Handles Button1.Click
| > | Dim f As New ColorDialog
| > | f.ShowDialog(Me)
| > | End Sub
| >
| > Note it goes on the call to ShowDialog, not the constructor.
| >
| > Hope this helps
| > Jay
| >
| > | > | Hi,
| > |
| > | When I create a new application, with just one button, then this
code
| > works
| > | as expected:
| > |
| > | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
| > | System.EventArgs) Handles Button1.Click
| > | Dim f As New ColorDialog
| > | f.ShowDialog()
| > | End Sub
| > |
| > | As soon as I add some controls (a PictureBox, some NumericUpDown
| > controls,
| > | ...) the ShowDialog does not work anymore: when I press the button,
the
| > | dialog is invisible. When I switch to another application and then
back
| > to
| > | my app, the dialog box instantly becomes visible.
| > |
| > | I can also press the <Alt> key to make the dialog visible. The same
| > | behaviour goes for FontDialog, OpenFileDialog, ...
| > |
| > | Thanks,
| > |
| > | WvH
| > |
| > |
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top