Add LineShape programatically in VB.NET WinForms - vb.net

How do you add a LineShape programmatically in VB.NET WinForms?
I'm looking to write something like you would for a Label , CheckBox or whatever else:
Dim somelabel as New Label
somelabel.Text = "Whatever"
somelabel.Location = New Point(200, 200)
Me.Controls.Add(somelabel)
Etc.
My purpose is to create thin dividing lines between the rows and columns of 16 Labels that form a 4x4 grid.
I appreciate that, since LineShape is a part of VB PowerPacks, this may present some difficulties, such as having to use Imports ... or, if really necessary, import a .dll. But I'd like to see all your ideas/solutions!

First, import the powerpacks namespace to give you access to the control:
Imports Microsoft.VisualBasic.PowerPacks
Then you could do it like this:
Dim startx As Integer
Dim starty As Integer
Dim endx As Integer
Dim endy As Integer
Dim yourline As New LineShape(startx, starty, endx, endy)
Where startx = the x starting position, starty = the y starting position, endx = the ending x position and endy = the ending y position.
If you want to put it into a canvas, simply:
Dim yourcanvas As ShapeContainer
canvas.Parent = formName
yourline.Parent = canvas
For more information and an API reference, go to:
http://msdn.microsoft.com/en-us/library/bb918067.aspx

Related

Window Media player adding Grids on playing video using VB.net

I have developed coding in vb.net to insert media player and also i am able to add all features to media player like time, seek bar but for my project, it is mandatory to show flexible grid over the playing video which requires advanced level of programming.I learned to add lines grids on a form but forming grids over playing video and transform it size graphically is difficult,i would be glad if you help me...thank you in advance
the following code help in forming grids but it cannot be transformed and also it cannot be placed over media player
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Sub DrawGrid(g As Graphics, origin As Point, rows As Integer, columns As Integer, cellSize As Size)
Dim gridWidth As Integer = columns * cellSize.Width
Dim gridHeight As Integer = rows * cellSize.Height
Dim left As Integer = origin.X - (gridWidth \ 2)
Dim right As Integer = origin.X + (gridWidth \ 2)
Dim top As Integer = origin.Y - (gridHeight \ 2)
Dim bottom As Integer = origin.Y + (gridHeight \ 2)
For y As Integer = top To bottom + 1 Step cellSize.Height
Dim pt1 As New Point(left, y)
Dim pt2 As New Point(right, y)
g.DrawLine(Pens.Black, pt1, pt2)
Next
For x As Integer = left To right + 1 Step cellSize.Width
Dim pt1 As New Point(x, top)
Dim pt2 As New Point(x, bottom)
g.DrawLine(Pens.Black, pt1, pt2)
Next
g.DrawEllipse(Pens.Red, New Rectangle(origin.X - 5, origin.Y - 5, 10, 10))
End Sub
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim origin As New Point(Me.ClientRectangle.Width \ 2, Me.ClientRectangle.Height \ 2)
Dim cellSize As New Size(10, 10)
Dim rowCount As Integer = 10
Dim columnCount As Integer = 10
DrawGrid(e.Graphics, origin, rowCount, columnCount, cellSize)
End Sub
End Class

Trying to fill Hexagon shape with color

I'm drawing a 2d grid of hexagons using the DrawPolygon function. Works great, but I'm trying to figure out how to fill them with a certain color. It would appear the FillEllipse function is only meant for rectangles. Is there something I could use to fill shapes made with DrawPolygon? Please note, the below code includes a function I'm too lazy to include called CreateHex, which passes 2 coordinates (x and y of course) which it uses to calculate the 6 points which CreateHex uses to return the 6 points needed for the Hexagon. Here's my code:
Dim myPen As New Pen(Color.Black)
Dim PicGraphics As Graphics = Graphics.FromImage(bmp)
Dim DrawFont As New Font("Arial", SizeSide / 3)
Dim drawFormat As New StringFormat
Dim drawBrush As New SolidBrush(Color.Black)
For lc = 1 To ColNum
For lc2 = 1 To RowNum
PicGraphics.DrawPolygon(myPen, CreateHex(lc, lc2))
PicGraphics.FillEllipse(New SolidBrush(Color.Red), CreateHex(lc, lc2)) ' <---- this doesn't work.
Next
Next
If I comment out the FillEllipse statement, it draws the polygons normally. But if its uncommented, it will draw nothing at all. Anyone know of a solution?

Position a textbox on a control in a tablelayoutpanel

I have a table layout panel on my form.
I have put a usercontrol in one if its cells.
Now I would like to position a textbox above this usercontrol, but I am not sure how to do the positioning.
The textbox is located on the form, not in the tablelayoutpanel.
I have tried a lot with .PointToScreen, but somehow I could not get it right.
The closest I could get was
Dim pt As Point
Me._ucGrid1.PointToScreen(Me._ucGrid1.Location)
Dim iGridOffsetTop As Integer = Me._ucGrid1.Top + pt.Y
Dim iGridOffsetLeft As Integer = Me._ucGrid1.Left + pt.X
But I guess that did not take the position of the table layout panel into account.
Could somebody please tell me where I went wrong?
Thank you!
Got it:
Dim iGridOffsetTop As Integer = Me._ucGrid1.Top
Dim iGridOffsetLeft As Integer = Me._ucGrid1.Left
Dim pt As Point
pt = Me.TableLayoutPanel1.Location
iGridOffsetLeft += pt.X
iGridOffsetTop += pt.Y

Detect width of text in vb.net

Is there a way to detect the actual width of text in a vb.net web app? It needs to be dependant upon its font-style and size.
In vb6 you could copy the text into a label and make it expand to fit then measure its width, but this won't work in vb.net.
Update: On further inspection, TextRenderer.MeasureText seems a better option:
Dim text1 As String = "Measure this text"
Dim arialBold As New Font("Arial", 12.0F)
Dim textSize As Size = TextRenderer.MeasureText(text1, arialBold)
See Graphics.MeasureString:
Measures the specified string when
drawn with the specified Font.
Dim myFontBold As New Font("Microsoft Sans Serif", 10, FontStyle.Bold)
Dim StringSize As New SizeF
StringSize = e.Graphics.MeasureString("How wide is this string?", myFontBold)
i have just recently done this in one of my projects here is how i did it
Dim textsize As Size = TextRenderer.MeasureText(cbx_Email.Text, cbx_Email.Font)
cbx_Email.Width = textsize.Width + 17
this is in a combobox.SelectedIndex changed sub.
The +17 is for the pixels that the dropdown arrow takes up in a combobox so it doesntcover text.
by using control.font it allows the code to dynamically change no matter what font is being used. Using Control.Text means you can use this on anything and wont have to change the code when changing the text of the control or page.
I wrote this low-end function to do just that without higher-level API's.
It creates a bitmap and graphics object, writes the string to the bitmap, scans backwards for the font edge and then returns the width in pixels
Private Function FontLengthInPixels(inputString As String, FontStyle As Font) As Integer
' Pick a large, arbitrary number for the width (500) in my case
Dim bmap As New Bitmap(500, 100)
Dim g As Graphics = Graphics.FromImage(bmap)
g.FillRectangle(Brushes.Black, bmap.GetBounds(GraphicsUnit.Pixel))
g.DrawString(inputString, FontStyle, Brushes.White, New Point(0, 0))
' Scan backwards to forwards, since we know the first pixel location is 0,0; we need to find the LAST and subtract
' the bitmap width from it to find the width.
For x = -(bmap.Width - 1) To -1
' Scan from the 5th pixel to the 10th, we'll find something within that range!
For y = 5 To 10
Dim col As Color = bmap.GetPixel(Math.Abs(x), y)
' Look for white (ignore alpha)
If col.R = 255 And col.G = 255 And col.B = 255 Then
Return Math.Abs(x) ' We got it!
End If
Next
Next
' Lets do this approx
Return 0
End Function

Ways to go about resizing controls in resizable windows?

I have two DataGridView controls, with three buttons in between them vertically, in a VB 2005 form that need to be resized and moved around on a form resize.
The code below works, and does what I want. It takes the difference between the new size and the default size, splits the height difference between the two DataGridViews, and moves things around correctly.
What bugs me about it is that I've hard-coded the defaults into the ResizeEnd handler. How would I go about passing the default size so that I don't repeat myself?
Even better, is there a way to do it with anchoring, docking, and other stuff, so that I don't even need to write code?
Thanks in advance!
Private Sub dlgShowAssets_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd
Dim defaultWindowSize As New System.Drawing.Size(759, 619)
Dim defaultAssetDGVSize As New System.Drawing.Size(730, 153)
Dim defaultAssetDGVPos As New System.Drawing.Point(9, 186)
Dim defaultButton1Pos As New System.Drawing.Point(9, 345)
Dim defaultButton2Pos As New System.Drawing.Point(100, 345)
Dim defaultButton3Pos As New System.Drawing.Point(191, 345)
Dim defaultDetailDGVSize As New System.Drawing.Size(730, 177)
Dim defaultDetailDGVPos As New System.Drawing.Point(9, 374)
Dim deltaX As Integer = Me.Size.Width - defaultWindowSize.Width
Dim deltaY As Integer = Me.Size.Height - defaultWindowSize.Height
Dim deltaSize As New System.Drawing.Size(deltaX, deltaY / 2)
Dim deltaPos As New System.Drawing.Point(0, deltaY / 2)
Me.AssetDataGridView.Size = defaultAssetDGVSize + deltaSize
Me.btnAddAsset.Location = defaultButton1Pos + deltaPos
Me.btnEditAsset.Location = defaultButton2Pos + deltaPos
Me.btnDeleteAsset.Location = defaultButton3Pos + deltaPos
Me.AssetIdentifierDataGridView.Size = defaultDetailDGVSize + deltaSize
Me.AssetIdentifierDataGridView.Location = defaultDetailDGVPos + deltaPos
End Sub
You can use the TableLayoutPanel control.