Tabcontrol and Text Placement using VB.net Form - vb.net

I am trying to build out an application that has tabs on the left, but I want the text to be horizontal and not vertical. I have seen many forum posts for WPF and C#, but nothing specific to VB.net.
Is there a specific property I can use to have the text change from vertical to horizontal? How do I implement this type of change? I know this seems novice like to be asking, but I feel I have hit a brick wall. Any help would be greatly appreciated.

I was able to find the following on the .Net Resource page for Microsoft.
https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-display-side-aligned-tabs-with-tabcontrol
The only item I now want to be able to do is add in padding to the text so that its not right against the dialog box. If anyone has any ideas, please advise.
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
' Get the item from the collection.
Dim _TabPage As TabPage = TabControl1.TabPages(e.Index)
' Get the real bounds for the tab rectangle.
Dim _TabBounds As Rectangle = TabControl1.GetTabRect(e.Index)
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.Gray, e.Bounds)
Else
_TextBrush = New System.Drawing.SolidBrush(e.ForeColor)
e.DrawBackground()
End If
' Use our own font.
Dim _TabFont As New Font("Arial", 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

Related

Is it possible to create a dashed border around text boxes in visual basic.net windows form application?

I've been working with a code that allows me to draw lines around text boxes in vb.net but these lines are solid lines. I'd like to have dashed or even dotted lines to dress the application up a little. Is there a way to make dashed lines around text boxes like you can do on the form? The current code I'm using is as so..
Dim g As Graphics = e.Graphics
Dim pen As New Pen(Color.Aqua, 2.0)
Dim txtBox As Control
For Each txtBox In Me.Controls
If TypeOf (txtBox) Is TextBox Then
g.DrawRectangle(pen, New Rectangle(txtBox.Location, txtBox.Size))
End If
Next
pen.Dispose()
I wanted to also mention I am able to used this code in the paint event to get a dashed line around my form. It would look really nice to get it around the text boxes too I hope this is possible!
ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle,Color.Aqua, ButtonBorderStyle.Dashed)
EDIT: I've just tried this code it seems to be drawing dashed rectangles but they are not going around my textboxes so not sure how to fix it.
Dim txtBox As Control
For Each txtBox In Me.Controls
If TypeOf (txtBox) Is TextBox Then
ControlPaint.DrawBorder(e.Graphics, txtBox.ClientRectangle, txtBox.ForeColor, ButtonBorderStyle.Dashed)
End If
Next
like this
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
For Each txtBox As Control In Me.Controls
If TypeOf (txtBox) Is TextBox Then
Dim borderRectangle As Rectangle = New Rectangle(txtBox.Location, txtBox.Size)
borderRectangle.Inflate(1, 1)
ControlPaint.DrawBorder(e.Graphics, borderRectangle, txtBox.ForeColor, ButtonBorderStyle.Dashed)
End If
Next
End Sub

How to place a transparent picture box above a web browser control

I want a webbrowser control on the background and a picturebox above it where I can draw and then it will appear above the webbrowser control. It's like I am writing on some paper which already has something written on it. I have placed a webbrowser control and a picture box above it both with same dimensions.
I know similar question has been asked a lot of time on this website in different forms but none of the solutions mentioned are working for me.
The solutions mentioned are usually for picturebox over picturebox not picturebox over webbrowser control. simply putting webbrowser control instead of picture does not work. Here is the code I used. The square that should have been formed was not formed.
Public Class Form1
Dim BMP As New Drawing.Bitmap(640, 480)
Dim GFX As Graphics = Graphics.FromImage(BMP)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Controls.Add(WebBrowser1)
PictureBox1.Location = New Point(0, 0)
PictureBox1.BackColor = Color.Transparent
Dim blackPen As New Pen(Color.Black, 3)
Dim point1 As New Point(100, 100)
Dim point2 As New Point(100, 200)
Dim point3 As New Point(200, 200)
Dim point4 As New Point(200, 100)
Dim curvePoints As Point() = {point1, point2, point3, point4}
GFX.FillRectangle(Brushes.White, 0, 0, PictureBox1.Width, PictureBox1.Height)
GFX.DrawPolygon(blackPen, curvePoints)
PictureBox1.Image = BMP
End Sub
End Class
It did not work the other way around either, I mean drawing first and than making the picturebox transparent.

How do I change the color of each tab?

I have a form that has four tabs on it I would like each tab to be a different color. The only thing I have been able to find on the internet is how to change the color of the selected tab and the rest of the tabs stay the original color. I have not found anything to give each tab its own color. The code I currently have is.
Private Sub TabControl1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
Dim g As Graphics = e.Graphics
Dim tp As TabPage = TabControl1.TabPages(e.Index)
Dim br As Brush
Dim sf As New StringFormat
Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)
sf.Alignment = StringAlignment.Center
Dim strTitle As String = tp.Text
If TabControl1.SelectedIndex = e.Index Then
'this is the background color of the tabpage header
br = New SolidBrush(Color.LightSteelBlue) ' chnge to your choice
g.FillRectangle(br, e.Bounds)
'this is the foreground color of the text in the tab header
br = New SolidBrush(Color.Black) ' change to your choice
g.DrawString(strTitle, TabControl1.Font, br, r, sf)
Else
'these are the colors for the unselected tab pages
br = New SolidBrush(Color.Blue) ' Change this to your preference
g.FillRectangle(br, e.Bounds)
br = New SolidBrush(Color.Black)
g.DrawString(strTitle, TabControl1.Font, br, r, sf)
End If
End Sub
There are two things that you need to do:
First is to change the DrawMode of the TabControl and set it to OwnerDrawFixed
And the second is to handle the TabControl DrawItem event
Here is an example:
Private Sub TabControl1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles TabControl1.DrawItem
Select Case e.Index
Case 0
e.Graphics.FillRectangle(New SolidBrush(Color.Red), e.Bounds)
Case 1
e.Graphics.FillRectangle(New SolidBrush(Color.Blue), e.Bounds)
Case 2
e.Graphics.FillRectangle(New SolidBrush(Color.Magenta), e.Bounds)
End Select
Dim paddedBounds As Rectangle = e.Bounds
paddedBounds.Inflate(-2, -2)
e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, Me.Font, SystemBrushes.HighlightText, paddedBounds)
End Sub
And here is what it looks like (I change the tab colors of the first three tab pages only, the others can be done easily by adding new cases to select case)
Is TabControl1 a Tab control you are adding to the form via the designer? Why not just set the TabBackColor property for each tab when you create it there?
If not (you do have to do it via code), just use a loop to cycle through each tab in the TabControl1's collection of tab pages (TabControl1.TabPages) and set the TabBackColor property for each there.

Capture Image of Entire Panel Control In Vb.net

I have added some richtextboxes and some picture boxes in a panel control with scrolling option enabled. I want to capture image of Panel control along with all its child controls. I tried various solutions available on net but still not able to find perfect solution to do my job. The best one available (which dose not capture what is off the scroll bars) is given below. Please help me to do this.
Dim bmp As New Bitmap(Panel1.Width, Panel1.Height)
Using gr As Graphics = Graphics.FromImage(bmp)
gr.CopyFromScreen(Panel1.PointToScreen(Point.Empty), Point.Empty, Panel1.Size)
End Using
Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
Dim tmpImg As New Bitmap(Control.Width, Control.Height)
Using g As Graphics = Graphics.FromImage(tmpImg)
G.CopyFromScreen(Panel1.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(Panel1.Width, Panel1.Height))
End Using
Return tmpImg
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TakeScreenShot(Panel1).Save("D:\Screenshot.png", System.Drawing.Imaging.ImageFormat.Png)
End Sub

Winform vb.net - how to change color of Tab?

How can I change the color of the Tab Page Header, the text that appears on the part of the Tab that is always visible?
Maybe not the most elegant solution but.. Change the DrawMode of the TabControl to OwnerDrawFixed and handle the drawing yourself. Check MSDN for examples.
In addition to coding the DrawItem Event, followup with this event to change the tabpage text whenever you want to.
Private Sub ChangeTabPageTextColor(ByVal TabPageIndex As Integer, ByVal ForeColor As Color)
' Get the area of the header of this TabPage
Dim HeaderRect As Rectangle = TabControl1.GetTabRect(TabPageIndex)
' Identify which TabPage is currently selected
Dim SelectedTab As TabPage = TabControl1.TabPages(TabPageIndex)
' Create a Brush to paint the Text
Dim TextBrush As New SolidBrush(ForeColor)
' Declare the text alignment variable
Dim sf As New StringFormat()
' Set the Horizontal Alignment of the Text
sf.Alignment = StringAlignment.Center
' Set the Verticle Alignment of the Text
sf.LineAlignment = StringAlignment.Near
' Declare a Font
Dim newfont As New Font(TabControl1.Font.Name, TabControl1.Font.Size, FontStyle.Regular)
' Draw the text
TabControl1.CreateGraphics().DrawString(SelectedTab.Text, newfont, TextBrush, HeaderRect, sf)
' Job done - dispose of the Brush
TextBrush.Dispose()
End Sub