Flicker while resizing form (runtime)

tek_man
04-17-2006, 11:37 PM
I have small (experimental) project that consists of one main form, with another borderless form containing a tabcontrol "hidden" behind it with the tabs exposed at the bottom of the main form. The first tab is selected at startup and the secondary form is completely hidden except for the tabs.

When the user selects another tab, the form resizes itself to expose additional controls. With a little searching through these forums (there may be a couple of you that recognize some code) this is all working. I set a tranparencyKey for the form so that the tabs "float" under the main form. (and change colors when selected) The form that contains the tabcontrol is "pinned" at a specific position on the main form by having the main form move it (triggered by a LocationChanged Event)

The only problem ( not really a problem, just an annoyance) is that during the resizing, the form flickers. Any ideas on how I can smooth the transition?

Here is the code for the Mainform (which moves the secondary form around)


Public Class MainForm
Inherits System.Windows.Forms.Form
Dim mytab As TabForm


#Region " Windows Form Designer generated code "


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

mytab = New TabForm
Me.Location = New Point(400, 400)

TextBox1.Text = Me.Location.X.ToString
TextBox2.Text = Me.Location.Y.ToString
mytab.Show()
mytab.Location = New Point(Me.Location.X + 10, Me.Location.Y + 195)
Me.TopMost = True

End Sub

Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged

TextBox1.Text = Me.Location.X.ToString
TextBox2.Text = Me.Location.Y.ToString
mytab.Location = New Point(Me.Location.X + 10, Me.Location.Y + 195)
End Sub

End Class


And here is the TabForm



Public Class TabForm
Inherits System.Windows.Forms.Form


#Region " Windows Form Designer generated code "

Private Sub TabControl1_DrawItem _
(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem

Dim TabCounter As Integer ' Counter for loop
Dim rect As Rectangle ' Rectange to cover tabs
Dim newGraphic As Graphics = TabControl1.CreateGraphics

For TabCounter = 0 To TabControl1.TabCount - 1 ' Loop through all of the tabs

rect = TabControl1.GetTabRect(TabCounter) ' define current rectangle

If TabCounter <> TabControl1.SelectedIndex Then ' IF it is NOT the selected tab then
newGraphic.FillRectangle(Brushes.Beige, rect) ' Paint Beige
Else ' if tab IS the selected tab then
newGraphic.FillRectangle(Brushes.Aqua, rect) ' Paint Aqua
End If

newGraphic.DrawString(TabControl1.TabPages(TabCounter).Text, _
TabControl1.Font, Brushes.Black, rect.X + 2, rect.Y + 2) ' "Draw" the text on the tab

Next

End Sub

Private Sub TabControl1_SelectedIndexChanged _
(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

Dim TargetWidth As Integer ' width for form to resize to
Static OldTab As Integer = 0 ' This is the default selected tab at startup
Dim TargetTab As Integer ' index of newly selected tab
Dim PauseTime As Integer ' amount of time (in mS) tp pause
Dim SizeIncrement As Integer ' Increment for size change w/ each loop
Dim MinWidth, MaxWidth As Integer ' MAximum and minimum widths for the form

MinWidth = 320
MaxWidth = 600

TargetWidth = TabControl1.SelectedTab.Tag ' set target width from tag property of control
TargetTab = TabControl1.SelectedIndex ' set target index
PauseTime = 5 ' set pause time
SizeIncrement = 8 ' set size increment

' Tab 0 is an empty tab that is normally hidden, Tab 1 and 2 are fuctional

If (OldTab = 0 And (TargetTab = 1 Or TargetTab = 2)) Or _
((OldTab = 2 Or OldTab = 1) And TargetTab = 0) Then ' This will execute if there is a "single" change

ChangeSize(TargetWidth, PauseTime, SizeIncrement) ' If the form just needs to open or close
'
Else '_________________________________________________ ' Here the user is swapping active tabs

GroupBox1.Visible = False ' Hide the controls on the tabpage
GroupBox2.Visible = False ' while it is being "closed"
TargetWidth = MinWidth

ChangeSize(TargetWidth, PauseTime, SizeIncrement) ' "Close" the old one

GroupBox1.Visible = True ' Show the controls while it is being "opened"
GroupBox2.Visible = True ' and "Open" the new selection
TargetWidth = MaxWidth

ChangeSize(TargetWidth, PauseTime, SizeIncrement)

End If

OldTab = TargetTab

End Sub

Sub ChangeSize(ByVal TargetWidth As Integer, ByVal PauseTimer As Integer, ByVal sizeincrement As Integer)

If Me.Width < TargetWidth Then '_____________ Increase width of form

Do Until Me.Width >= TargetWidth ' Repeat until we have reached the target
Me.Width = Me.Width + sizeincrement
Pause(PauseTimer)
Loop
Me.Width = TargetWidth ' Correct for loop overruns

Else '_______________________________________ Decrease width of form

Do Until Me.Width <= TargetWidth
Me.Width = Me.Width - sizeincrement ' Repeat until we have reached the target
Pause(PauseTimer)
Loop
Me.Width = TargetWidth ' Correct for loop overruns

End If

End Sub

Public Sub Pause(ByVal Milliseconds As Integer)
Dim dTimer As Date
dTimer = Now.AddMilliseconds(Milliseconds)
Do While dTimer > Now
Application.DoEvents() ' Wait until current time + pause time has been reached
Loop
End Sub


Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class


Thanks for any ideas


Edit again: I cleaned up the code to make it easier to follow

Lee

Nylyst
04-18-2006, 12:42 PM
You may want to look into the .SetStyles property of the Control class. From what I gather, it won't show up in intellisense, but is indeed a member of the Form class. The possible values for this property are in ControlStyles enum

HTH,
Nylyst

P.S. This discussion (http://p2p.wrox.com/topic.asp?TOPIC_ID=14858) may be of some use to you as well.

tek_man
04-19-2006, 09:15 AM
Thanks, I will look into that.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum