|
-
Dec 21st, 2006, 04:10 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Tab Control questions.
I am wanting to do a couple things with a tab control.
1) Change the back/fore color of the actual tab in the tabcontrol, not the tabpage background.
2) Handle the MouseDown event of the actual tab, not the tabpage background.
When I do tabPage.ForeColor = Color.Red, it changes the background of the tabpage, and same thing with the MouseDown event. Is there a (somewhat) simple method of doing the above 2 things for the actual tab?
Thanks in advance.
Last edited by Tool; Dec 21st, 2006 at 02:42 PM.
Reason: Resolved (Thanks keinma and stanav)
-
Dec 21st, 2006, 09:39 AM
#2
Re: Tab Control questions.
1) You need to set the drawmode to owner draw, and draw the tabs yourself.
2) I am not sure what you mean by handle the MouseDown of the actual tab. Both the individual tabs, and the tabcontrol itself, have MouseDown events that you can write code in.
-
Dec 21st, 2006, 09:43 AM
#3
Re: Tab Control questions.
Use "owner draw" to do this.... This is a slightly modified version of the example found on MSDN. (You need to set your TabControl's DrawMode property to OwnerDrawFixed, and do the drawing in DrawItem event of the TabControl.)
VB Code:
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
Dim g As Graphics = e.Graphics
Dim _TextBrush As Brush
Dim pageNum As Integer = e.Index
' Get the item from the collection.
Dim _TabPage As TabPage = TabControl1.TabPages(pageNum)
' Get the real bounds for the tab rectangle.
Dim _TabBounds As Rectangle = TabControl1.GetTabRect(pageNum)
If (e.State = DrawItemState.Selected) Then
' Draw a different background color, and don't paint a focus rectangle.
_TextBrush = New SolidBrush(Color.Red)
g.FillRectangle(Brushes.Yellow, e.Bounds)
Else
'The TabControl has 2 TabPages, thus select case only goes to 1.
'Add more Case if you have more tabpages.
Select Case pageNum
Case 0
g.FillRectangle(Brushes.Brown, e.Bounds)
Case 1
g.FillRectangle(Brushes.Blue, e.Bounds)
Case Else
g.FillRectangle(Brushes.Green, e.Bounds)
End Select
_TextBrush = New System.Drawing.SolidBrush(e.ForeColor)
End If
' Use our own font.
Dim _TabFont As New Font("Times New Roman", 10.0, FontStyle.Bold, GraphicsUnit.Pixel)
' Draw string. Center the text.
Dim _StringFlags As New StringFormat()
_StringFlags.Alignment = StringAlignment.Center
_StringFlags.LineAlignment = StringAlignment.Center
g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, New StringFormat(_StringFlags))
End Sub
-
Dec 21st, 2006, 02:23 PM
#4
Thread Starter
Hyperactive Member
Re: Tab Control questions.
Thank you keinma and stanav, I guess ownerdraw is the only way to do it (was hoping it would be a bit easier). That answered my first question.
kleinma:
As for the second question, I want to have a context menu on the tab itself on right click, and close on middle click. Best example I can think of is almost any tabbed web browser.
I created my own class that inherits from TabPage. When I handle MouseDown on that, it only gets called when the TabPage gets the Mouse Down. I am guessing I will have to write my own code to figure out which tab is selected (similar to owner draw and changing the background color of the selected tab). Is this the case, or is there a simpler way of handling that?
-
Dec 21st, 2006, 02:30 PM
#5
Re: Tab Control questions.
Like I mentioned earlier, use the mousedown of the tab control itself, not the tabpage.
VB Code:
Private Sub TabControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TabControl1.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
ContextMenuStrip1.Show(TabControl1, e.X, e.Y)
End If
End Sub
the tabcontrol itself has its own full set of events, just like each tab page has a set of events. the mousedown of the tabcontrol only fires when the mouse is clicked on one of the tabs.
-
Dec 21st, 2006, 02:41 PM
#6
Thread Starter
Hyperactive Member
Re: Tab Control questions.
Sorry, misunderstood your first post. That is, indeed, what I was looking for. Thank you again for your help, much appreciated.
-
Dec 25th, 2006, 06:55 PM
#7
New Member
Re: [RESOLVED] Tab Control questions.
VB Code:
g.DrawString(_TabPage.Text, _TabFont, _TextBrush, _TabBounds, New StringFormat(_StringFlags))
(141): Overload resolution failed because no accessible 'DrawString' can be called with these arguments:
'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, layoutRectangle As System.Drawing.RectangleF, format As System.Drawing.StringFormat)': Value of type 'System.Drawing.Rectangle' cannot be converted to 'System.Drawing.RectangleF'.
'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, point As System.Drawing.PointF, format As System.Drawing.StringFormat)': Value of type 'System.Drawing.Rectangle' cannot be converted to 'System.Drawing.PointF'.
'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, x As Single, y As Single)': Value of type 'System.Drawing.Rectangle' cannot be converted to 'Single'.
'Public Sub DrawString(s As String, font As System.Drawing.Font, brush As System.Drawing.Brush, x As Single, y As Single)': Value of type 'System.Drawing.StringFormat' cannot be converted to 'Single'.
using vb2003 why am I getting this error? Used the code from above post.
-
Dec 26th, 2006, 09:43 AM
#8
Re: [RESOLVED] Tab Control questions.
just a guess, but maybe you are trying to pass in a type of "rectangle" when it wants a type of "rectangleF"
the difference between the 2 is that rectangleF uses singles and rectangle uses integer. That means a rectangleF can have a side that is 2.5 and not just 2 or 3
if _TabBounds is indeed a rectangle, you can use
VB Code:
RectangleF.op_Implicit(_TabBounds)
to convert a rectangle to rectangleF
-
Dec 27th, 2006, 08:09 AM
#9
New Member
Re: [RESOLVED] Tab Control questions.
Thanks Kleinma
got it with
VB Code:
Dim _TabBounds As RectangleF = RectangleF.op_Implicit(TabControl1.GetTabRect(pageNum))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|