Code39 printed on labeler is unreadeable - vb.net

I'm trying to print a code39 with a labeler (argox x-2300e) so the code is this (i deleted all the stuff to print just a barcode to test):
Imports System.ComponentModel
Imports System.Drawing.Printing
Public Class Form1
Dim linea1 As String = "*A0-121244$K0001196AA-UK*"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim psz2 As New Printing.PaperSize
psz2.RawKind = Printing.PaperKind.Custom
psz2.Width = 217
psz2.Height = 314
PrintDocument2.DefaultPageSettings.PaperSize = psz2
PrintDocument2.DefaultPageSettings.Margins = New Margins(0, 0, 0, 0)
PrintDocument2.DefaultPageSettings.Landscape = False
PrintDocument2.PrinterSettings.Copies = 1
PrintDocument2.PrinterSettings.PrinterName = "\\ETIC1\Argox X-2300E series PPLB"
PrintPreviewDialog2.Size = New System.Drawing.Size(500, 750)
PrintPreviewDialog2.ShowDialog()
End Sub
Private Sub PrintDocument2_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument2.PrintPage
Dim drawFormat As New StringFormat
drawFormat.Alignment = StringAlignment.Center
e.Graphics.ScaleTransform(1, 3)
e.Graphics.DrawString(linea1, New Font("IDAHC39M Code 39 Barcode", 6, FontStyle.Regular), Brushes.Black, New Rectangle(0, 2, 217, 50), drawFormat)
e.Graphics.DrawString(linea1, New Font("Free 3 of 9", 14, FontStyle.Regular), Brushes.Black, New Rectangle(0, 55, 217, 50), drawFormat)
e.Graphics.ScaleTransform(1, 1 / 3)
e.Graphics.DrawLine(New Pen(Brushes.Black, 0.5), New Point(15, 250), New Point(202, 250))
End Sub
Private Sub PrintPreviewDialog2_Closing(sender As Object, e As CancelEventArgs) Handles PrintPreviewDialog2.Closing
Application.Exit()
End Sub
End Class
now, the problem is this:
I print the label but our scanner fails to read the barcode (the scanner is the same of our biggest costumer)
Using the "free 3 to 9" font at size 16 is the minimum size for the code to be readable every time, but this exceeds the dimensions of the label.
Using the "IDAHC39M Code 39 Barcode" font, it must be at least at size 8 to be readable. (but this still exceeds the dimensions of the label)
I draw a line under the 2 code it represent the width of the same bar code generated using the labeler software that came with printer itself, and even though it is smaller, it is fully readable
the label is 55mm x 80mm
How can i get the code inside the label be readable? What am I missing?

Related

Circular Hitbox in Visual Basic WinForm

How can I change the hitbox of a PictureBox where the image is circular? By hitbox, I mean where the ClickEvent registers.
You can use the Control.Region property, to create a Region using a circular GraphicsPath. This Region is used for the Click event, as well as the MouseEnter, MouseHover, and MouseLeave events.
Example with a PictureBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim path As New Drawing2D.GraphicsPath()
path.AddArc(0, 0, PictureBox1.Width, PictureBox1.Height, 0, 360) 'A circle at 0,0 relative to the PictureBox, sharing its Width and Height
PictureBox1.Region = New Region(path)
End Sub
If you want a circle in the middle (instead of an oval), then use something like:
Dim path As New Drawing2D.GraphicsPath()
Dim radius As Integer = Math.Min(PictureBox1.Width - 1, PictureBox1.Height - 1) / 2
Dim rc As New Rectangle(New Point(PictureBox1.Width / 2, PictureBox1.Height / 2), New Size(1, 1))
rc.Inflate(radius, radius)
path.AddEllipse(rc)
PictureBox1.Region = New Region(path)

Random picturebox arrangement

I am new to visual basics and was wondering how to do the following program: I have 9 picture boxes and a button "arrange". For my program, I would like that all picture boxes come together like a puzzle randomly to make a square that has a width and height of three picture boxes. The square made would have all nine picture boxes in one and every time you click the button "arrange" the picture boxes would change to a random location within the square. So far, I have written so that all the picture boxes become the same size but i don't know how to make them come together in a square. Thanks in advance.
Public Class frm1
Dim Placement As Integer
Private Sub btnArrange_Click(sender As Object, e As EventArgs) Handles btnArrange.Click
picDeux.Size = picgris.Size
picTrois.Size = picgris.Size
picQuatre.Size = picgris.Size
picCinq.Size = picgris.Size
picSix.Size = picgris.Size
picSept.Size = picgris.Size
picHuit.Size = picgris.Size
picNeuf.Size = picgris.Size
lstNum.Items.Clear()
For i = 1 To 3
For j = 1 To 3
Dim L As New Point(picgris.Width * j + 100, picgris.Height * i)
lstNum.Items.Add(L)
Next
Next
For i = 1 To 3
For j = 1 To 3
Placement = Int(Rnd() * (lstNum.Items.Count))
Next
Next
End Sub
End Class
I created nine pictures boxes at design time. You would assign a different image to each picture box. They are all square and the same size. Mine are 100 x 100 to make the arithmetic easy.
I made an array of points as a form level variable. These point will form a 300 x 300 square with the picture boxes. I also declared an array of PictureBox. In the Form.Load I added the pictures boxes to the array.
To reposition the picture boxes assigned the array to a list. Items in this list will be removed because we don't want to assign the same location to more the one picture box. This will not effect the original array.
Looping through the picture boxes we assign a random position to the box then remove that point from the list.
Public Class PictureSort
Private Rand As New Random()
Private PointArray As Point() = {New Point(100, 100), New Point(200, 100), New Point(300, 100), New Point(100, 200), New Point(200, 200), New Point(300, 200), New Point(100, 300), New Point(200, 300), New Point(300, 300)}
Private PictureBoxArray(8) As PictureBox
Private Sub PictureSort_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBoxArray = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9}
End Sub
Private Sub RepositionPictureBoxes()
Dim lst = PointArray.ToList
For Each pb In PictureBoxArray
Dim index = Rand.Next(0, lst.Count)
pb.Location = lst(index)
lst.RemoveAt(index)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RepositionPictureBoxes()
End Sub
End Class
My advice is to use a control array - you have an example here that should help: VB.NET Control Array- Declaration and Application.
You just need to initiate that array of controls once, this can be done at form load.
The next step is to sort that array in a random manner. Finally, loop on the array and every time your current loop index modulo 3 = 0, then you increase the Y coordinates and reset the X position.
Here is an example. You can see that each time you click on the button, the picture boxes are rearranged on the form in random order using an ad hoc function. For each picture box, a bitmap is generated on the fly to show the index of the control.. this is for demonstration purposes.
Public Class frmPics
Private pics As New List(Of PictureBox)
Private Const picture_width As Integer = 100, picture_height As Integer = 50
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' instantiate controls
Dim font As New Font("Arial", 20, FontStyle.Regular, GraphicsUnit.Pixel)
For i As Integer = 1 To 9
Dim pic As New PictureBox
pic.Visible = False
pic.Name = "pic" & i
pic.Text = i.ToString
Console.WriteLine("Create control: name: " & pic.Name)
' generate an ad-hoc bitmap image showing the index of the control
Dim bitmap As New Bitmap(picture_width, picture_height)
Using g As Graphics = Graphics.FromImage(bitmap)
Dim width As Integer = CInt(g.MeasureString(Text, font).Width)
Dim height As Integer = CInt(g.MeasureString(Text, font).Height)
End Using
Using g As Graphics = Graphics.FromImage(bitmap)
g.Clear(Color.Blue)
g.DrawString(i.ToString, font, New SolidBrush(Color.White), 0, 0)
End Using
pic.Image = bitmap
pics.Add(pic)
Me.Controls.Add(pic)
Next
End Sub
Private Sub btnShuffle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShuffle.Click
Dim x As Integer = 10, y As Integer = picture_height
Dim counter As Integer = 1
Dim rnd As New Random()
' show controls on form
Console.WriteLine("Show controls on form")
Me.SuspendLayout()
For Each item In pics.OrderBy(Function() rnd.Next)
item.Width = picture_width
item.Height = picture_height
item.Location = New Point(x, y)
item.BorderStyle = BorderStyle.FixedSingle
item.Visible = True
Console.WriteLine("counter: " & counter & " - control name" & item.Name & " - position: " & item.Location.X & "/" & item.Location.Y & " text: " & item.Text)
' reset X position every 3 iterations
If counter Mod 3 = 0 Then
x = 10
y += item.Height
Else
x += item.Width
End If
counter += 1
Next
Me.ResumeLayout()
End Sub
End Class

System.ArgumentException: Parameter is not valid. When entering a value in a text box

I'm trying to create a simple program that will allow you to enter a degree and have a dial fill to that exact degree, and then also display the degree inside of the dial. The project builds without errors, and will display correctly until a value is entered into the textbox, and then I will get the error on the line
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
If I comment out that line it will go down to the line below it. This is my first time using vb.net, so I'm guessing there is something small I'm just doing incorrectly. My full code is:
Public Class Form1
Dim degree As Double
Dim ge As Graphics
Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
ge = e.Graphics
DrawProgress(ge, New Rectangle(150, 25, 400, 400), degree)
End Sub
Private Sub DrawProgress(g As Graphics, rect As Rectangle, degree As Double)
'work out the angles for each arc
Dim progressAngle = CSng(degree)
Dim remainderAngle = 360 - progressAngle
'create pens to use for the arcs
Using progressPen As New Pen(Color.LightSeaGreen, 2), remainderPen As New Pen(Color.LightGray, 2)
'set the smoothing to high quality for better output
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
'draw the blue and white arcs
g.DrawArc(progressPen, rect, -90, progressAngle)
g.DrawArc(remainderPen, rect, progressAngle - 90, remainderAngle)
End Using
'draw the text in the centre by working out how big it is and adjusting the co-ordinates accordingly
Using fnt As New Font(Me.Font.FontFamily, 50)
Dim text As String = degree.ToString
Dim textSize = g.MeasureString(text, fnt)
Dim textPoint As New Point(CInt(rect.Left + (rect.Width / 2) - (textSize.Width / 2)), CInt(rect.Top + (rect.Height / 2) - (textSize.Height / 2)))
'now we have all the values draw the text
g.DrawString(text, fnt, Brushes.Black, textPoint)
End Using
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Dim input_s As String
input_s = TextBox1.Text
If (String.IsNullOrEmpty(input_s)) Then
degree = 0
Else
degree = CDec(input_s)
End If
DrawProgress(ge, New Rectangle(150, 25, 400, 400), degree)
End Sub
End Class

VB 2013 Creating 100 ovalshapes using a loop to simulate a complex network

I am trying to simulate a complex network. I would like to represent 100 network nodes using OvalShape. (I plan to later connect these with lines according to a network connection alogirthm) I have the code below that creates 5 nodes just fine. However, I need to make 100 nodes. Can I somehow put this in a loop to create new OvalShapes and name them theNode1, theNode2, theNode3,... theNode100?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim canvas As New ShapeContainer
' Set the form as the parent of the ShapeContainer.
canvas.Parent = Me
Dim theNode1 As New OvalShape
Dim theNode2 As New OvalShape
Dim theNode3 As New OvalShape
Dim theNode4 As New OvalShape
Dim theNode5 As New OvalShape
' Set the ShapeContainer as the parent of the OvalShape.
theNode1.Parent = canvas
theNode2.Parent = canvas
theNode3.Parent = canvas
theNode4.Parent = canvas
theNode5.Parent = canvas
theNode1.SetBounds(100, 100, 50, 50)
theNode2.SetBounds(100, 200, 50, 50)
theNode3.SetBounds(100, 100, 50, 50)
theNode4.SetBounds(200, 200, 50, 50)
theNode5.SetBounds(200, 100, 50, 50)
End Sub
Instead of using a separate variable for each node, you can have a List of them:
Private Sub CreateOvals()
Dim canvas As New ShapeContainer
' Set the form as the parent of the ShapeContainer.
canvas.parent = Me
Dim nodes As New List(Of OvalShape)
' create 5 nodes and set the .parent for each one
For i = 0 To 4
nodes.Add(New OvalShape With {.parent = canvas})
Next
' call SetBounds as required
nodes(0).SetBounds(100, 100, 50, 50)
nodes(1).SetBounds(100, 200, 50, 50)
nodes(2).SetBounds(100, 100, 50, 50)
nodes(3).SetBounds(200, 200, 50, 50)
nodes(4).SetBounds(200, 100, 50, 50)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CreateOvals()
End Sub
It is usually better to call separate Subs from the Form1_Load handler - it makes it easier to modify a particular part without geting bogged down trying to figure out which section does what, and using meaningful names for methods makes it more readable.
Also, in case you haven't discovered it yet, I strongly recommend using Option Strict On - and set it as the default in Visual Studio.

How to Add Custom Objects in to listbox of VB 2010

I am developing one vb application. In that I have one list box. I want to add different types of Items. Like Different Colored and differently aligned text(Like one item item is right aligned and one more is left aligned). Can you please tell me how can i do the same.
Thanks.
This is how I did it in one of my projects (the original code is in c#):
Public Class ColoringStepCommandListBox
Inherits CommandListBox
Const ItemHeight As Integer = 20
Public Sub New()
listBox.ItemHeight = ItemHeight
listBox.DrawMode = DrawMode.OwnerDrawFixed
End Sub
Protected Overrides Sub OnDrawItem(sender As Object, e As DrawItemEventArgs)
Const textFormatFlags__1 As TextFormatFlags = TextFormatFlags.EndEllipsis Or TextFormatFlags.PreserveGraphicsClipping Or TextFormatFlags.VerticalCenter
Const colorRectangleWidth As Integer = 100, textLeft As Integer = 110
If e.Index >= 0 Then
'Cast the listbox item to your custom type (ColoringStep in my example).
Dim coloringStep = TryCast(listBox.Items(e.Index), ColoringStep)
e.DrawBackground()
'Do custom coloring and rendering, draw icons etc.
Dim colorRect = New Rectangle(2, e.Bounds.Top + 2, colorRectangleWidth, ItemHeight - 5)
Dim innerRect = New Rectangle(colorRect.Left + 1, colorRect.Top + 1, colorRect.Width - 1, colorRect.Height - 1)
e.Graphics.DrawRectangle(Pens.Black, colorRect)
DrawingHelper.DrawGradient(coloringStep, e.Graphics, innerRect, LinearGradientMode.Horizontal)
'Draw the text (this does not happen automatically any more with owner draw modes).
Dim textRect = New Rectangle(textLeft, e.Bounds.Top, e.Bounds.Width - textLeft, e.Bounds.Height)
TextRenderer.DrawText(e.Graphics, coloringStep.ToString(), e.Font, textRect, e.ForeColor, textFormatFlags__1)
e.DrawFocusRectangle()
End If
End Sub
End Class
I got simple solution for this. Below is the code
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim textFont As New Font(e.Font.FontFamily, e.Font.Size * 4)
e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), textFont, New SolidBrush(Color.BlueViolet), RectangleF.op_Implicit(e.Bounds))
e.DrawFocusRectangle()
End Sub
Private Sub listBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
e.ItemHeight = e.ItemHeight * 4
End Sub
You can add extra code inside ListBox1_DrawItem method to customize Items