picturebox bug?

S

Sai

Hi,

I have had this problem for a while now.Its kind of strange and I created a
simple demo scenario to illustrate the problem (source code at the bottom of
this post). I'd greatly appreciate it if others could confirm that this is
indeed a bug and suggestions on how I can work around this.

I have a simple compact framework form with a tabcontrol on it. There are
two tab pages on this control. The first tab page has a treeview control on
it with a few dummy entries. The tree view takes up the entire tab page. On
the
second tab page, I have a big picturebox that takes up the whole tab page. I
define the picturebox click event like so :
Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles PictureBox1.Click

MessageBox.Show("picture clicked")

End Sub


The treeview_afterselect event simply loads the second tabpage like so:

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

Me.TabControl1.SelectedIndex = 1

End Sub



Now, if I select any node on the tree, the second tab page loads as expected
but the picturebox's click event always gets fired. I tested this hundreds
of times to see if my shaky hands werent the reason but I am pretty sure
thats not it. If I replace the picturebox with an ordinary button, the click
event does not get fired !

I'd greatly appreciate any ideas.

Thanks

Sai Ramani

FULL SOURCE CODE:



Public Class Form1

Inherits System.Windows.Forms.Form

Friend WithEvents TabControl1 As System.Windows.Forms.TabControl

Friend WithEvents btnQuit As System.Windows.Forms.Button

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'SAISAISAI Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

MyBase.Dispose(disposing)

End Sub

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents TabPage1 As System.Windows.Forms.TabPage

Friend WithEvents TabPage2 As System.Windows.Forms.TabPage

Friend WithEvents TreeView1 As System.Windows.Forms.TreeView

Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

Private Sub InitializeComponent()

Dim TreeNode1 As System.Windows.Forms.TreeNode = New
System.Windows.Forms.TreeNode

Dim TreeNode2 As System.Windows.Forms.TreeNode = New
System.Windows.Forms.TreeNode

Dim TreeNode3 As System.Windows.Forms.TreeNode = New
System.Windows.Forms.TreeNode

Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))

Me.TabControl1 = New System.Windows.Forms.TabControl

Me.TabPage1 = New System.Windows.Forms.TabPage

Me.TreeView1 = New System.Windows.Forms.TreeView

Me.TabPage2 = New System.Windows.Forms.TabPage

Me.PictureBox1 = New System.Windows.Forms.PictureBox

Me.btnQuit = New System.Windows.Forms.Button

'

'TabControl1

'

Me.TabControl1.Controls.Add(Me.TabPage1)

Me.TabControl1.Controls.Add(Me.TabPage2)

Me.TabControl1.SelectedIndex = 0

Me.TabControl1.Size = New System.Drawing.Size(240, 224)

'

'TabPage1

'

Me.TabPage1.Controls.Add(Me.TreeView1)

Me.TabPage1.Location = New System.Drawing.Point(4, 4)

Me.TabPage1.Size = New System.Drawing.Size(232, 198)

Me.TabPage1.Text = "Tab 1"

'

'TreeView1

'

Me.TreeView1.ImageIndex = -1

Me.TreeView1.Location = New System.Drawing.Point(4, 0)

TreeNode1.Text = "PATIENT 1 PATIENT 1 PATIENT 1"

TreeNode2.Text = "PATIENT 2 PATIENT 2 PATIENT 2"

TreeNode3.Text = "PATIENT 3 PATIENT 3 PATIENT 3"

Me.TreeView1.Nodes.Add(TreeNode1)

Me.TreeView1.Nodes.Add(TreeNode2)

Me.TreeView1.Nodes.Add(TreeNode3)

Me.TreeView1.SelectedImageIndex = -1

Me.TreeView1.Size = New System.Drawing.Size(224, 192)

'

'TabPage2

'

Me.TabPage2.Controls.Add(Me.PictureBox1)

Me.TabPage2.Location = New System.Drawing.Point(4, 4)

Me.TabPage2.Size = New System.Drawing.Size(232, 198)

Me.TabPage2.Text = "Tab 2"

'

'PictureBox1

'

Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"),
System.Drawing.Image)

Me.PictureBox1.Location = New System.Drawing.Point(12, 12)

Me.PictureBox1.Size = New System.Drawing.Size(204, 168)

Me.PictureBox1.SizeMode =
System.Windows.Forms.PictureBoxSizeMode.StretchImage

'

'btnQuit

'

Me.btnQuit.Location = New System.Drawing.Point(44, 240)

Me.btnQuit.Size = New System.Drawing.Size(124, 24)

Me.btnQuit.Text = "Exit"

'

'Form1

'

Me.Controls.Add(Me.btnQuit)

Me.Controls.Add(Me.TabControl1)

Me.Text = "Form1"

End Sub

#End Region

Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnQuit.Click

Application.Exit()

End Sub



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)

MessageBox.Show("Button 2 click")

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

Me.TabControl1.SelectedIndex = 1

End Sub

Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles PictureBox1.Click

MessageBox.Show("picture clicked")

End Sub

Private Sub TreeView1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TreeView1.Click

'MessageBox.Show("Treeview clicked")

End Sub

End Class
 
J

Jon Brunson

Sai said:

<snip>

Hello Sai,

Although this doesn't answer your question, it may be a suitable
work-around. Change the content of the following procedure...
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

Me.TabControl1.SelectedIndex = 1

End Sub

....to...

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

RemoveHander PictureBox1.Click, AddressOf PictureBox1_Click

Me.TabControl1.SelectedIndex = 1

AddHander PictureBox1.Click, AddressOf PictureBox1_Click

End Sub


I've not actually tried this particular code, but it works for me on
many other event-fires-for-no-good-reason problems :)
 
S

Sai

Thanks for the idea, Jon. I hope somebody from Microsoft sees this thread
and posts about a fix/solution.
 

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