PictureBox within a tab

tomhosking

Well-known member
Joined
Aug 4, 2005
Messages
49
Programming Experience
1-3
I have a picture box in a tab that draws a diagram of some data. What I want is for the sub that does the drawing to be triggered when the tab is selected. Using .Click or .GotFocus events for the tab doesnt seem to work... Any ideas?
 
You sort of understood... There are around6 tabs in tabcontrol1, I only want it to draw when I select the tab with the picturebox in, otherwise thats just a complete waste of CPU time.

This mainly stems from the fact that the picturebox seems to clear if I change tab and then change back to the one containing the picturebox. It would be fine to have the diagram draw on a different event (button or whatever), IF the drawing remained in the picturebox.
 
You can find which tab was selected by many criterias like index, text etc.

This is how you can find which tabPage is selected by its text property:

VB.NET:
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
 
If Me.TabControl1.SelectedTab.Text = "Page1" Then
 
MessageBox.Show("the first one was selected")
 
ElseIf Me.TabControl1.SelectedTab.Text = "Page2" Then
 
MessageBox.Show("the second one was selected")
 
ElseIf Me.TabControl1.SelectedTab.Text = "Page3" Then
 
DrawFunction()
'and so on
'
'
End If
 
End Sub

Cheers ;)
 
Ah hadnt thought of that :)

When I now select the tab, the diagram appears for a split second, then disappears. Presumably it's redrawing it or something, but how can I stop it doing it?
 
Without some idea of how your tabControl and drawFunc are structured, it's very hard to guess how you might do this. The only hint I can offer is that you can check that draw finction has nothing connected to tab's selectedindexchanged event.
if i knew what is the code for drawing a diagram maybe we can fix up ... in this case i'm without suggestion. Maybe if you post the code

Cheers ;)
 
The draw function looks like this:
VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] g [/size][size=2][color=#0000ff]As[/color][/size][size=2] Graphics

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] mypen [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] Pen(Color.Black)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myfont [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] Font(Font.Name = "Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] mybrush [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] Drawing2D.LinearGradientBrush(ClientRectangle, _

Color.Red, Color.Red, Drawing2D.LinearGradientMode.Horizontal)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] mybrush2 [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] Drawing2D.LinearGradientBrush(ClientRectangle, _

Color.Blue, Color.Blue, Drawing2D.LinearGradientMode.Horizontal)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] x1a, x1b, y1a, y1b [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Double

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] x1ai, x1bi, y1ai, y1bi [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer

[/color][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub[/color][/size][size=2] drawMapFunc([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] TabControl1.SelectedIndexChanged

[/size][size=2][color=#0000ff]If[/color][/size][size=2] lstStations.SelectedIndex() <> -1 [/size][size=2][color=#0000ff]And[/color][/size][size=2] TabControl1.SelectedIndex() = 5 [/size][size=2][color=#0000ff]Then

[/color][/size][size=2]g = drawMap.CreateGraphics

g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias

x1ai = [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](drawMap.Width / 2)

y1ai = [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](drawMap.Height / 2)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] q [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer

[/color][/size][size=2][/size][size=2][color=#0000ff]For[/color][/size][size=2] q = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] diamondMeasure.stationSet(lstStations.SelectedIndex()).numSubTargets

x1bi = [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](-8 * (diamondMeasure.stationSet(lstStations.SelectedIndex()).targetSet(q).myNomXYZ.X - diamondMeasure.stationSet(lstStations.SelectedIndex()).myNomXYZ.X))

y1bi = [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](8 * (diamondMeasure.stationSet(lstStations.SelectedIndex()).targetSet(q).myNomXYZ.Y - diamondMeasure.stationSet(lstStations.SelectedIndex()).myNomXYZ.Y))

g.DrawLine(mypen, x1ai, y1ai, x1bi + x1ai, y1bi + y1ai)

g.DrawString(diamondMeasure.stationSet(lstStations.SelectedIndex()).targetSet(q).name, myfont, mybrush, x1bi + x1ai + 1, y1bi + y1ai + 1)

[/size][size=2][color=#0000ff]Next

[/color][/size][size=2]g.DrawString(diamondMeasure.stationSet(lstStations.SelectedIndex()).name, myfont, mybrush2, x1ai + 2, y1ai + 2)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]If

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Sub

[/color][/size]

It appears, then disappears straight away...

Everything else is fairly standard stuff I think. drawMap is within tabMap, which is tab 5 of tabcontrol1.
 
Well, i'm not sure what is what there and how it works (scenarion) but i "Guess" the issue comes while you are checking lstStations.SelectedIndex. However, take a look at the attached project below to see how it works just fine if you select certain tabPage (2nd in my case). I hope you will find a bit sense in order to resolve your problem


Cheers ;)
 

Attachments

  • SelectedTab.zip
    30.3 KB · Views: 30
Last edited:
If the problem was with it checking the tabpage, why would it show for a moment when I selected the right tab, then disappear?
 
If I move the picturebox out of the tab, it works fine when I select the tab. Unfortunately, thats not a solution as theres no room left on the gui :(
 
No, because that exact same code works fine if the picturebox is not contained in the tab. When you select the right tab, with something in lstStations selected, it draws it perfectly.

When this then is moved to within the tab, it disappears a split second after it is drawn.

Im afraid my file downloading isnt working (some kind of restriction on work internet or something), would you be able to post the source code?
 
You posted a project in a zip file, I cant seem to download it.


Trust me, it doesnt work if its within the tab. I see it appear for just a split second, then the display goes blank again. Ive tried all sorts of stuff, it dosent work!
 
Hmm ... i just tried to get the zip file and it works fine for me ... never mind ... this is the source code:
Place the tab control and add two pages to it "page1" and "page2" actually i will send you entire code including Windows Form Designer generated code :)

VB.NET:
Public Class Form1
 
Inherits System.Windows.Forms.Form
 
#Region " Windows Form Designer generated code "
 
Public Sub New()
 
MyBase.New()
 
'This call is required by the Windows Form Designer.
 
InitializeComponent()
 
'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 AsBoolean)
 
If disposing Then
 
IfNot (components IsNothing) Then
 
components.Dispose()
 
End If
 
End If
 
MyBase.Dispose(disposing)
 
EndSub
 
'Required by the Windows Form Designer
 
Private components As System.ComponentModel.IContainer
 
'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.
 
FriendWithEvents TabControl1 As System.Windows.Forms.TabControl
 
FriendWithEvents TabPage1 As System.Windows.Forms.TabPage
 
FriendWithEvents TabPage2 As System.Windows.Forms.TabPage
 
FriendWithEvents drawMap As System.Windows.Forms.PictureBox
 
<System.Diagnostics.DebuggerStepThrough()> PrivateSub InitializeComponent()
 
Me.TabControl1 = New System.Windows.Forms.TabControl
 
Me.TabPage1 = New System.Windows.Forms.TabPage
 
Me.TabPage2 = New System.Windows.Forms.TabPage
 
Me.drawMap = New System.Windows.Forms.PictureBox
 
Me.TabControl1.SuspendLayout()
 
Me.TabPage2.SuspendLayout()
 
Me.SuspendLayout()
 
'
 
'TabControl1
 
'
 
Me.TabControl1.Controls.Add(Me.TabPage1)
 
Me.TabControl1.Controls.Add(Me.TabPage2)
 
Me.TabControl1.Location = New System.Drawing.Point(8, 8)
 
Me.TabControl1.Name = "TabControl1"
 
Me.TabControl1.SelectedIndex = 0
 
Me.TabControl1.Size = New System.Drawing.Size(552, 368)
 
Me.TabControl1.TabIndex = 0
 
'
 
'TabPage1
 
'
 
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
 
Me.TabPage1.Name = "TabPage1"
 
Me.TabPage1.Size = New System.Drawing.Size(272, 246)
 
Me.TabPage1.TabIndex = 0
 
Me.TabPage1.Text = "Page1"
 
'
 
'TabPage2
 
'
 
Me.TabPage2.Controls.Add(Me.drawMap)
 
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
 
Me.TabPage2.Name = "TabPage2"
 
Me.TabPage2.Size = New System.Drawing.Size(544, 342)
 
Me.TabPage2.TabIndex = 1
 
Me.TabPage2.Text = "Page2"
 
'
 
'drawMap
 
'
 
Me.drawMap.Location = New System.Drawing.Point(8, 16)
 
Me.drawMap.Name = "drawMap"
 
Me.drawMap.Size = New System.Drawing.Size(528, 320)
 
Me.drawMap.TabIndex = 0
 
Me.drawMap.TabStop = False
 
'
 
'Form1
 
'
 
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
 
Me.ClientSize = New System.Drawing.Size(568, 382)
 
Me.Controls.Add(Me.TabControl1)
 
Me.Name = "Form1"
 
Me.Text = "Form1"
 
Me.TabControl1.ResumeLayout(False)
 
Me.TabPage2.ResumeLayout(False)
 
Me.ResumeLayout(False)
 
End Sub
 
#EndRegion
 
Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
 
If Me.TabControl1.SelectedTab.Text = "Page1" Then
 
MessageBox.Show("the first one was selected")
 
ElseIf Me.TabControl1.SelectedTab.Text = "Page2" Then
 
Dim G As Graphics
 
G = GetGraphicsObject()
 
G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
 
G.FillRectangle(Brushes.Silver, ClientRectangle)
 
G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality
 
Dim points() As Point = {New Point(20, 50), New Point(220, 190), _
 
New Point(330, 80), New Point(450, 280)}
 
G.DrawCurve(Pens.Blue, points, 0.1)
 
G.DrawCurve(Pens.Red, points, 0.5)
 
G.DrawCurve(Pens.Green, points, 1)
 
G.DrawCurve(Pens.Black, points, 2)
 
End If
 
End Sub
 
Function GetGraphicsObject() As Graphics
 
Dim bmp As Bitmap
 
bmp = New Bitmap(drawMap.Width, drawMap.Height)
 
drawMap.Image = bmp
 
Dim G As Graphics
 
G = Graphics.FromImage(bmp)
 
Return G
 
End Function
 
End Class



Cheers ;)

p.s. let me know if you got the code to remove it ... it looks very odd to me (so much code in single post :D)
 
Back
Top