How do I properly apply padding to a group box control? - vb.net

I have a class that inherits a panel which I am adding a group box to. This group box contains for now a couple text boxes. I would like to have the text boxes centered horizontally and vertically within the group box by using the AutoSize property of the group box and the Padding property of the group box. Here is my attempt:
Imports System.Drawing
Public Class pnlItemMstr_A_OSI
Inherits Panel
Public Sub New(ByRef ItemMstr_DS As DataSet, ByVal padding As Integer)
MyBase.New()
Dim drItemMstr As DataRow = ItemMstr_DS.Tables(0)(0)
Dim txtHeight As Integer = 26
' article numbers
Dim grpArtNum As New GroupBox
Dim txtARTC_NUM_DOM_C As New TextBox
Dim txtARTC_NUM_CAN_C As New TextBox
With txtARTC_NUM_DOM_C
.Text = drItemMstr("ARTC_NUM_DOM_C").ToString
.Size = New Size(200, txtHeight)
.Location = New Point(0, 0)
End With
With txtARTC_NUM_CAN_C
.Text = drItemMstr("ARTC_NUM_CAN_C").ToString
.Size = New Size(200, txtHeight)
.Location = New Point(0, txtHeight)
End With
With grpArtNum
grpArtNum.Text = "Article Number"
grpArtNum.Padding = New Padding(padding)
grpArtNum.Controls.Add(txtARTC_NUM_DOM_C)
grpArtNum.Controls.Add(txtARTC_NUM_CAN_C)
End With
Me.Controls.Add(grpArtNum)
End Sub
End Class
What I am ending up with is ugly and not what I would expect to happen, notice how the group box text is cut off:
It seems the padding is not being applied properly, but I am sure I am just doing something wrong.

The Padding property is used primarily for Dock Styled child controls, so try adding a dock filled panel into your GroupBox to contain those TextBoxes:
With grpArtNum
grpArtNum.Text = "Article Number"
grpArtNum.Padding = New Padding(padding)
Dim innerPanel As New Panel With {.Dock = DockStyle.Fill}
innerPanel.Controls.Add(txtARTC_NUM_DOM_C)
innerPanel.Controls.Add(txtARTC_NUM_CAN_C)
grpArtNum.Controls.Add(innerPanel)
End With

Related

ComboBox DrawItem event handling for Font Combo Box - WinForms, VB.NET

I am creating a combobox which shows fonts in the list, with every font item displayed in its own font style. The combobox loads without errors but the list that shows is all in just one font. I have set DrawMode = OwnerDrawVariable and IntegralHeight = false, but still no result. The DrawItem code that you can see here is from the MSDN website, with a few changes as per my requirement. Here is my code:
BindCOmboboxes() is called when the form loads.
Private Sub w_BindComboBoxes() (StyleControl is a User Control containing the combobox)
'Set Properties
Dim tFont As Font = Nothing
StyleControl.TS1Font.DrawMode = DrawMode.OwnerDrawVariable
For Each fntfam As FontFamily In FontFamily.Families
If fntfam.IsStyleAvailable(FontStyle.Regular) Then
tFont = New Font(fntfam, 12, FontStyle.Regular)
StyleControl.TS1Font.Items.Add(tFont.Name & ",Regular")
End If
If fntfam.IsStyleAvailable(FontStyle.Italic) Then
tFont = New Font(fntfam, 12, FontStyle.Italic)
StyleControl.TS1Font.Items.Add(tFont.Name & ",Italic")
End If
If fntfam.IsStyleAvailable(FontStyle.Bold) Then
tFont = New Font(fntfam, 12, FontStyle.Bold)
StyleControl.TS1Font.Items.Add(tFont.Name & ",Bold")
End If
Next
End Sub
DrawItem function for TS1Font combobox
Private Sub FontComboBoxDrawItems(sender As Object, e As DrawItemEventArgs)
Dim size As Integer = 12
Dim myFont As System.Drawing.Font
Dim family As FontFamily = new FontFamily("Arial", 12) <-- to avoid crash
Dim fntStyle As FontStyle = FontStyle.Regular <-- To avoid crash
Dim animalColor As System.Drawing.Color = Color.Black
Dim str As String = DirectCast(sender, ComboBox).Items(e.Index).ToString.Trim
Dim brk() As String = str.Split(",")
For Each fam As FontFamily In FontFamily.Families
If fam.Name = brk(0) Then family = fam
Next
Select Case brk(1)
Case "Regular"
fntStyle = FontStyle.Regular
Case "Bold"
fntStyle = FontStyle.Bold
Case "Italic"
fntStyle = FontStyle.Italic
End Select
' Draw the background of the item.
e.DrawBackground()
' Draw each string in the array, using a different size, color,
' and font for each item.
myFont = New Font(family, size, fntStyle)
e.Graphics.DrawString(DirectCast(sender, ComboBox).Items(e.Index).ToString, myFont, System.Drawing.Brushes.Black,
New PointF(e.Bounds.X, e.Bounds.Y))
' Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle()
End Sub
On debugging in the DrawItem function, the values in the statement e.Graphics.DrawString = shows proper font family, style and size values. So why doesn't it draw the items that way?
Result:

vb.net how can zoom image

I have a Custom Control for viewing images with zooming facility but without scrollBar, and its working condition is good. Actually The Custom Control is a Panel with a Picutrbox on it. I also using a TrackBar for zooming in/out the image. It is also working better.
But I am not fully satisfied, even it is covering the purpose of my App because I need zooming PictureBox based on center point. Now it is anchoring Top Left.
Another One is when Zooming out the image, the image goes to zero size at TrackBar's Zero. Even I limited zoom level to the panel size and working good, my unsatisfaction taking place here also, as it is not responding at zero level of TrackBar. Here I need, the original size of image loaded in picture box have to go for 100% of TrackBar and when image reached at Custom controle size have to go for 0% of TrackBar. Then I will fullfilled.
I figuring my code here.........
My Custom Control is a User Control Inheriting from Panel.
code for cutom control :
Public Class ImageViewer
Inherits Panel
Dim AutoScaleDimensions As SizeF
Dim AutoScaleMode As AutoScaleMode
Protected Overrides Sub DefWndProc(ByRef m As Message)
If m.Msg <> 131 Then
MyBase.DefWndProc(m)
End If
End Sub
End Class
On Form1, I Placed my Custom Control- ImageViewer1 and also placed PicutreBox1 with in ImageViewer1, Palced Button1 and TrackBar1 on Form1
Changed Properties as follows
ImageViewer1 - AutoScroll=True
PicutreBox1 - SizeMode=Zoom
TrackBar1- Maximum=100
My Declared Variables are
Dim imgName As String
Private SliderCenter As Integer = 50
Private originalImg As Bitmap
Code for Button1.Click
Try
Dim inputImg As FileDialog = New OpenFileDialog()
inputImg.Filter = "Image File (*.Jpg;*.Bmp;*.Png;*.Gif;*.Tiff;*.Tif;*.PDF)|*.Jpg;*.Bmp;*.Png;*.Gif;*.Tiff;*.Tif;*.PDF"
If inputImg.ShowDialog() = DialogResult.OK Then
imgName = inputImg.FileName
originalImg = New Bitmap(inputImg.FileName)
Dim newImg As New Bitmap(imgName)
PictureBox1.Image = DirectCast(newImg, Image)
End If
inputImg = Nothing
Catch ae As System.ArgumentException
imgName = ""
MessageBox.Show(ae.Message.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
Code for TrackBar1.ValueChanged
If originalImg IsNot Nothing Then
If TrackBar1.Value > 0 Then
Dim scale As Double = TrackBar1.Value
Dim height As Integer = Convert.ToInt32((scale / SliderCenter) * originalImg.Height)
Dim width As Integer = Convert.ToInt32((scale / SliderCenter) * originalImg.Width)
PictureBox1.Size = New Size(width, height)
If PictureBox1.Width <= ImageViewer1.Width Then
PictureBox1.Size = New Size(PictureBox1.Width + (ImageViewer1.Width - PictureBox1.Width), ImageViewer1.Height)
End If
If PictureBox1.Height <= ImageViewer1.Height Then
PictureBox1.Size = New Size(ImageViewer1.Width, PictureBox1.Height + (ImageViewer1.Height - PictureBox1.Height))
End If
End If
End If
Please help me in your kind ........ Thank You.

List view sort arrow vb.net

i am trying to draw sort arrows on list view column header along with the default visual styles
so far i have got this
Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader
e.DrawDefault = True
If e.ColumnIndex = selectedIndex Then
e.Graphics.DrawImage(ImageList1.Images(1), CType(e.Bounds.Left + e.Bounds.Width / 2, Single) - 5, -2)
End If
End Sub
but the visual style is drawn over the arrow somehow
so i figured i could try this :
Private Sub List_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles List.DrawColumnHeader
e.DrawDefault = True
If lastDrawn.ColumnIndex = selectedIndex Then
e.Graphics.DrawImage(ImageList1.Images(1), CType(lastDrawn.Bounds.Left + lastDrawn.Bounds.Width / 2, Single) - 5, -2)
End If
lastDrawn=e
End Sub
and it draws the arrow when the next corresponding column is being drawn
but with this i cant get it to draw for the last column
Screenshots:
In order to use the .NET build in solution for showing a custom icon for a list view column header you need to:
create an ImageList
add three images (up / down arrow and empty) to it
bind the image list to the ListView control
bind to the ColumnClick event of the ListView control
when sorting the columns set the ImageKey property of the currently sorted column depending on the sorting direction
This example class (a simple form) shows how to set the images correctly not using custom drawing for the ListView header columns.
It does not implement any sort! (how to implement a ListViewSorter is shown in this MSDN article)
You need to implement a custom ListView-Sorter class and retrieve the image or the image key from it, after a column is sorted.
Public Class SimpleForm
Inherits Form
Private sortItems = New ImageList()
Dim lv As ListView = New ListView()
Dim so = System.Windows.Forms.SortOrder.Ascending
Public Sub New()
' create columns, items and ListView
Dim columns = New List(Of ColumnHeader)
Dim c1 = New ColumnHeader()
c1.Name = "c1"
c1.Text = "Name"
Dim c2 = New ColumnHeader()
c2.Name = "c2"
c2.Text = "Type"
columns.Add(c1)
columns.Add(c2)
Dim items = New List(Of ListViewItem)
Dim i1 = New ListViewItem("Terminator")
i1.SubItems.Add("T1000")
Dim i2 = New ListViewItem("Terminator")
i2.SubItems.Add("T10")
Dim i3 = New ListViewItem("J.C.")
i3.SubItems.Add("Human")
items.Add(i1)
items.Add(i2)
items.Add(i3)
' init and bind column click
lv.Columns.AddRange(columns.ToArray())
lv.Items.AddRange(items.ToArray())
lv.SmallImageList = sortItems
lv.View = View.Details
lv.Dock = DockStyle.Fill
Controls.Add(lv)
AddHandler lv.ColumnClick, AddressOf clickEventHandler
' init images list
sortItems.TransparentColor = System.Drawing.Color.Transparent
sortItems.Images.Add("up", Image.FromFile("d:\temp\32\arrow_up.gif"))
sortItems.Images.Add("down", Image.FromFile("d:\temp\32\arrow_down.gif"))
sortItems.Images.Add("empty", Image.FromFile("d:\temp\32\check.gif"))
End Sub
Private Sub clickEventHandler(ByVal o As Object, ByVal e As ColumnClickEventArgs)
' Implement a custom ListViewItemSorter and fetch the icon from it!
' Set the ListViewItemSorter property to a new ListViewItemComparer
' object. Setting this property immediately sorts the
' ListView using the ListViewItemComparer object.
' THIS CODE SHOWS ONLY HOW TO SET THE SORT ICON!
For i As Integer = 0 To lv.Columns.Count - 1
If (i = e.Column) Then
Select Case (so)
Case System.Windows.Forms.SortOrder.Ascending
lv.Columns(i).ImageKey = "up"
so = System.Windows.Forms.SortOrder.Descending
Case System.Windows.Forms.SortOrder.Descending
lv.Columns(i).ImageKey = "down"
so = System.Windows.Forms.SortOrder.Ascending
Case Else
lv.Columns(i).ImageKey = "empty"
so = System.Windows.Forms.SortOrder.None
End Select
Else
lv.Columns(i).ImageKey = "empty"
End If
Next i
End Sub
End Class
The output looks like this:

Controls are not added to tabpage VB.NET

I run the following code in the constructor of a window. The "label" gets added but none of the other controls are shown on screen. If I debug the newTab.Controls there are several controls in it. Why don't they show up on the screen and I only see the "label" control.
Thanks
Dim graphlist As ArrayList = New ArrayList
For Each funct As TL_FUNCTION In functionlist
If (funct.functionname = functi) Then
If Not (graphlist.Contains(funct.picture)) Then
graphlist.Add(funct.picture)
End If
End If
Next
For Each picture In graphlist
Dim NewTab As New TabPage
NewTab.Name = picture
NewTab.Text = NewTab.Name
Me.TabControl1.Controls.Add(NewTab)
Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
For Each func As TL_FUNCTION In functionlist
If (func.picture = picture) Then
Dim label As Label = New Label
label.Text = func.curve.ToString
NewTab.Controls.Add(label) 'This label shows up
Dim key As String
Dim values() As String
For Each key In func.values.Keys
values = func.values.GetValues(key)
For Each value As String In values
Dim label2 As New Label
label2.Text = key.ToString
Dim textb As TextBox = New TextBox
textb.Text = value
NewTab.Controls.Add(label2) 'this one is not shown on the tab
NewTab.Controls.Add(textb) 'this one is not shown on the tab
Next value
Next key
End If
Next
Next
You are placing the new labels and textboxes underneath the new label that you see in the TabPage because you never set their location, so it defaults to point (0, 0).
Try setting the location for the controls:
For Each value As String In values
Dim label2 As New Label
label2.Text = key.ToString
label2.Location = New Point(10, NewTab.Controls.Count * 24)
Dim textb As TextBox = New TextBox
textb.Text = value
textb.Location = New Point(label2.Right + 4, label2.Top)
NewTab.Controls.Add(label2)
NewTab.Controls.Add(textb)
Next value

How to change Tab Control Background Color (VB.NET)

how can I change the grey part into white color?
I want my tabcontrol filled with full white color.
So far what i did is like this:
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 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 the current index is the Selected Index, change the color
If TabControl1.SelectedIndex = e.Index Then
'this is the background color of the tabpage header
br = New SolidBrush(Color.White) ' 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.White) ' 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
and I also put this at PageLoad function:
TabControl1.DrawMode = TabDrawMode.OwnerDrawFixed
For Each tg As TabPage In TabControl1.TabPages
tg.BackColor = Color.White
Next
There is no property to do this. However it is possible by using something like this
http://dotnetrix.co.uk/tabcontrol.htm
All controls on this site are freely available under MIT license.
Spending fair time to research if someone find a solution; There could be some work around. But according to MSDN Ref.
TabControl.BackColor Property
NET Framework (current version) This API supports the product
infrastructure and is not intended to be used directly from your code.
This member is not meaningful for this control.
Namespace: System.Windows.Forms Assembly: System.Windows.Forms (in
System.Windows.Forms.dll)
As far as I understand, this is adjustable from user's windows setting. (Highlight Color) of TabControl, Forms and other controls; otherwise MS could simply turn this property on.
I make a trick to get around this, I put on the gray part a white label and I have the following result:
Since that color segment of the tabcontrol is unpaintable and cannot be controlled, you have to use a panel or label, etc. to cover up the background color where there is no tabpage header. I use a panel to do this.
This statement working correctly:
Dim PutBackColor As Boolean = False
Private Sub TabControl1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
If Me.PutBackColor = False Then
e.Graphics.FillRectangle(New SolidBrush(<YourColor>), 0 , 0, e.Bounds.Width + 2, e.Bounds.Height + 2)
Me.PutBackColor = True
End If
e.Graphics.FillRectangle(New SolidBrush(<YourColor>), 0 , 0, e.Bounds.Width + 2, e.Bounds.Height + 2)
e.Graphics.DrawString(Me.TabControl1.TabPages(e.In dex).Text, e.Font, Brushes.White, e.Bounds.X + 5, e.Bounds.Y + 5)
If e.State = DrawItemState.Selected Then
Me.PutBackColor = False
End If
End Sub