VB .NET - Full text alignment title form at left - vb.net

I have this code to align the form's title but I can not totally align with it, when I put a space for it to go totally left it works but it adds 3 points at the end, does anyone have any idea with fixing this?
Form FIXED SINGLE STYLE
Thank you all for the time
Private Sub ALINHAMENTO()
Dim g As Graphics = Me.CreateGraphics()
Dim startingPoint As Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim, Me.Font).Width / 2)
Dim widthOfASpace As Double = g.MeasureString(" ", Me.Font).Width
Dim tmp As String = " "
Dim tmpWidth As Double = 0
Do
tmp += " "
tmpWidth += widthOfASpace
Loop While (tmpWidth + widthOfASpace) < startingPoint
Me.Text = Me.Text.Trim & tmp
End Sub
RUNNING
Solution based in idea of : Olivier Jacot-Descombes
Private Sub ALINHAMENTO()
Dim g As Graphics = Me.CreateGraphics()
Dim startingPoint As Double = (Me.Width / 1.3) - (g.MeasureString(Me.Text.Trim, Me.Font).Width / 1)
Dim widthOfASpace As Double = g.MeasureString(" ", Me.Font).Width
Dim tmp As String = " "
Dim tmpWidth As Double = 0
Do
tmp += " "
tmpWidth += widthOfASpace
Loop While (tmpWidth + widthOfASpace) < startingPoint
Me.Text = Me.Text.Trim & tmp
End Sub

Related

Incorrect values are written to the text file

I want to write the values of a sine function to a text file. The function is
In my graphing calculator, I also have to add π if I want to plot the function in radians.
How do I have to write this in the source code? Wrong values come out every time, regardless of whether I insert or leave out π.
I would like to have a y-value of 0 for t = 0 to 14400, and also from t = 69060 onwards. In between, according to its formula, the sine function of y = 0 should rise, reach 8, and fall again (second zero as said at 69060).
Private Sub ButtonStart_Click(sender As Object, e As EventArgs) Handles ButtonStart.Click
Dim Path As String = ""
Using SFD As New CommonSaveFileDialog
SFD.Title = "Ordner, in dem die Textdatei gespeichert werden soll, auswählen"
SFD.Filters.Add(New CommonFileDialogFilter("Textdateien", ".txt"))
Dim di As New IO.DirectoryInfo(Application.StartupPath)
If di.Parent.Name = "bin" Then
di = di.Parent.Parent.Parent ' AnyCPU
ElseIf di.Parent.Parent.Name = "bin" Then
di = di.Parent.Parent.Parent.Parent ' x64, x86
End If
If System.IO.Directory.Exists(di.FullName) Then
SFD.InitialDirectory = di.FullName
Else
SFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
End If
If SFD.ShowDialog() = CommonFileDialogResult.Ok Then
Path = SFD.FileName & ".txt"
Else
Return
End If
End Using
ButtonStart.BackColor = Color.FromArgb(255, 255, 0)
Application.DoEvents()
Using textfile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Path, False, System.Text.Encoding.UTF8)
textfile.WriteLine($"Time{Tab}V(OUT)")
For t As UInt32 = 0UI To 86400UI Step 1UI
If t < 14400UI OrElse (t >= 14400UI AndAlso t <= 69060UI) Then
textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & 0.0.ToString(Eng))
Else
Dim Value As Double = 8.0 * Math.Sin(1.0 * Math.PI / 54660.0 * t + 2.0 * Math.PI - 0.2634467618)
textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & Value.ToString(Eng))
End If
Next
textfile.Close()
End Using
ButtonStart.BackColor = Color.FromArgb(0, 255, 0)
End Sub
This should be your function
Function f(t As Double) As Double
Dim amplitude = 8
Dim period = 54660
Dim phaseDegrees = 177
Dim phaseRadians = phaseDegrees * Math.PI / 180
Dim vertical = 0
Dim a = amplitude
Dim b = 2 * Math.PI / period
Dim c = phaseRadians
Dim d = vertical
Return a * Math.Sin(b * (t + c)) + d
End Function
See image, from https://www.mathsisfun.com/algebra/amplitude-period-frequency-phase-shift.html
I found a solution. It has to be
Amplitude * sin(2πf*t + phase in rad) + offset
Using textfile As System.IO.StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Path, False, System.Text.Encoding.UTF8)
For t As UInt32 = 0UI To 86400UI Step 1UI
If t < 14400UI OrElse (t > 69060UI AndAlso t <= 86400UI) Then
textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & 0.0.ToString(Eng))
Else
Dim Value As Double = 4.0 * Math.Sin(2 * Math.PI * 1.0 / 54660.0 * t + 177.0 * Math.PI / 180.0) + 4.0
textfile.WriteLine(t.ToString(Eng).PadLeft(10, "0"c) & Tab & Value.ToString(Eng))
End If
Next
textfile.Close()
End Using

#XVAL shows data index (integer) instead of string of an array

I want to know why #XVAL shows the index (from 0 to 3 since there is 4 items in there) instead of ex:"< .50%" and so on. This will help me to change the desired output, which is "< .50%".
I've tried #AXISLABEL too and it doesn't work.
Here is a snippet of the code:
Private Sub LoadGraphicChart()
Try
C1Chart2.ChartGroups(0).ChartData.SeriesList.Clear()
' Data
Dim items As String() = New String() {"< 0.50%", "0.50% - 1.00%", "1.00% - 2.00%", "> 2.00%"}
Dim unitHData As Double() = New Double() {7 / 100, 0, 0, 1 / 100}
'first try, populating the series
Dim unitH As C1.Win.C1Chart.ChartDataSeriesCollection = C1Chart2.ChartGroups(0).ChartData.SeriesList
'unitH.Clear()
Dim series As ChartDataSeries = unitH.AddNewSeries
series.Label = "Unit Holder"
series.LineStyle.Color = Color.MediumPurple
series.X.CopyDataIn(items)
series.Y.CopyDataIn(unitHData)
'tooltip
C1Chart2.ToolTip.Enabled = True
For Each ds As ChartDataSeries In unitH
ds.TooltipText = "Series = {#TEXT}" + ControlChars.Cr + ControlChars.Lf + "X = {#XVAL}" + ControlChars.Cr + ControlChars.Lf + "Y = {#YVAL:0.00%}"
Next ds
End Try
End Sub
This is what the tooltip currently looks like:
Changing from using tooltiptext to CoordToDataIndex solved my problems.
Here is how I use it:
Private Sub C1Chart2_MouseClick(sender As Object, e As MouseEventArgs) Handles C1Chart2.MouseClick
Dim SeriesOutput As Integer
Dim PointOutput As Integer
Dim DistanceOutput As Integer
Dim items As String() = New String() {InputLabel10.Text, InputLabel9.Text, InputLabel8.Text, InputLabel7.Text}
Dim unitHData As String() = New String() {FormatPercent((CDbl(lblPersen00.Text) / 100)), FormatPercent((CDbl(lblPersen05.Text) / 100)), FormatPercent((CDbl(lblPersen10.Text) / 100)), FormatPercent((CDbl(lblPersen20.Text) / 100))}
C1Chart2.ChartGroups.Group0.CoordToDataIndex(e.X, e.Y, CoordinateFocusEnum.XandYCoord, SeriesOutput, PointOutput, DistanceOutput)
Debug.WriteLine("Series Index : " & SeriesOutput.ToString())
Debug.WriteLine("Point Index : " & PointOutput.ToString())
Debug.WriteLine("Distance from Point: " & DistanceOutput.ToString())
'MessageBox.Show(PointOutput)
If PointOutput.ToString <> "" Then
MessageBox.Show("X : " & items(PointOutput) & ControlChars.Cr & ControlChars.Lf & "Y : " & unitHData(PointOutput))
End If
End Sub
Result(First Data):
Result(Last Data):

vb PrintDocument not printing within specified margins

I am using the following code to print but every time I print to a laser printer, the right and bottom margins get cut off regardless of what I set my margins at. Could anyone shed some light on this situation? Note, I have tried using PrintDoc.OriginAtMargins = True/False but it doesn't appear to be working either.
/code/
Public MarginSize As Integer = 15
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'temp = Nothing
'With dgView
' For Each row In dgView.Rows
' temp += row.Cells(0).Value & " " & row.Cells(1).Value & " " & row.Cells(2).Value & " " & row.Cells(3).Value & vbNewLine
' Next
'End With
PrintDialog.PrinterSettings = PrintDoc.PrinterSettings
If PrintDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDoc.PrinterSettings = PrintDialog.PrinterSettings
Dim PageSetup As New PageSettings
With PageSetup
.Margins.Left = MarginSize
.Margins.Right = MarginSize
.Margins.Top = MarginSize
.Margins.Bottom = MarginSize
.Landscape = False
End With
PrintDoc.DefaultPageSettings = PageSetup
End If
' PrintDoc.OriginAtMargins = False
PrintPreviewDialog.Document = PrintDoc
PrintPreviewDialog.WindowState = FormWindowState.Maximized
PrintPreviewDialog.PrintPreviewControl.Zoom = 1
PrintPreviewDialog.ShowDialog()
End Sub
Private Sub PrintDoc_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
Static intStart As Integer
Dim fntText As Font = txtDrawnBy.Font
Dim txtHeight As Integer
Dim LeftMargin As Integer = PrintDoc.DefaultPageSettings.Margins.Left
Dim RightMargin As Integer = PrintDoc.DefaultPageSettings.PaperSize.Width - MarginSize
Dim TopMargin As Integer = PrintDoc.DefaultPageSettings.Margins.Top
Dim BottomMargin As Integer = PrintDoc.DefaultPageSettings.PaperSize.Height - MarginSize
txtHeight = PrintDoc.DefaultPageSettings.PaperSize.Height - PrintDoc.DefaultPageSettings.Margins.Top - PrintDoc.DefaultPageSettings.Margins.Bottom
Dim LinesPerPage As Integer = CInt(Math.Round(txtHeight / (fntText.Height + 0.025)))
'Draw Rectangle for Margin
e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)
Dim y1 As Integer = e.PageBounds.Height.ToString / 3
Dim y2 As Integer = e.PageBounds.Height.ToString / 3 * 2
'Draw line 1/4 way down
e.Graphics.DrawLine(Pens.Orange, LeftMargin, y1, RightMargin, y1)
'Draw line 3/4 way down
e.Graphics.DrawLine(Pens.Orange, LeftMargin, y2, RightMargin, y2)
Dim intLineNumber As Integer
Dim sf As New StringFormat
Dim LineStep As Integer = 0
For intCounter = intStart To 66
'Print line numbers
e.Graphics.DrawString(intLineNumber.ToString & ": ", fntText, Brushes.Black, LeftMargin, fntText.Height * intLineNumber + TopMargin)
intLineNumber += 1
If intLineNumber > LinesPerPage Then
intStart = intCounter
e.HasMorePages = True
Exit For
End If
Next
End Sub
I have also attached an image of my results.
Image of print results

How to update a Progressbar / Label created per code in a for each loop?

First my code :
Sub festplatte()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
Dim I As Integer
For Each d In allDrives
If d.IsReady = True Then
Try
' Intelize controls
Dim progressbar As ProgressBar = New ProgressBar
Dim pbgroup As GroupBox = New GroupBox
Dim info As Label = New Label
Dim type As Label = New Label
Dim format As Label = New Label
Dim Space As Double = d.TotalFreeSpace / d.TotalSize
' Add to tab
Me.Tab_M.Controls.Add(progressbar)
Me.Tab_M.Controls.Add(pbgroup)
Me.Tab_M.Controls.Add(info)
Me.Tab_M.Controls.Add(type)
Me.Tab_M.Controls.Add(format)
' Add to group
pbgroup.Controls.Add(progressbar)
pbgroup.Controls.Add(info)
pbgroup.Controls.Add(type)
pbgroup.Controls.Add(format)
pbgroup.Text = d.Name & " | Name : " & CheckName(d.VolumeLabel.ToString())
pbgroup.Size = New System.Drawing.Size(600, 65)
pbgroup.Location = New System.Drawing.Point(8, I * 70 + 40)
' format
format.AutoSize = True
format.Name = "format" & I
format.Location = New System.Drawing.Point(435, 36)
format.Text = "Format : " & d.DriveFormat
' Typ
type.AutoSize = True
type.Name = "type" & I
type.Location = New System.Drawing.Point(435, 16)
type.Text = "Art : " & GetDriverFormat(d)
' info
info.Name = "info" & I
info.Location = New System.Drawing.Point(6, 16)
info.AutoSize = True
' Info Text :: 1000 MB Grenze
If d.TotalFreeSpace > Math.Pow(1024, 3) Then
info.Text = "Benutzter Speicher beträgt : " & Math.Round((d.TotalSize - d.TotalFreeSpace) / btogb, 2) & " GB von " & Math.Round((d.TotalSize) / btogb, 2) & " GB (" & Math.Round(100 - Space * 100, 2) & " % )"
Else
info.Text = "Benutzter Speicher beträgt : " & Math.Round((d.TotalSize - d.TotalFreeSpace) / btomb, 2) & " MB von " & Math.Round((d.TotalSize) / btomb, 2) & " MB (" & Math.Round(100 - Space * 100, 2) & " % )"
End If
'Progressbar
progressbar.Size = New System.Drawing.Size(425, 23)
progressbar.Location = New System.Drawing.Point(6, 32)
progressbar.Name = "Memory" & I
progressbar.Value = 100 - Space * 100
Catch ex As Exception
MsgBox(ex.ToString())
End Try
I += 1
End If
Next
End Sub
The code does generate a groupbox with each a progressbar in it and 3 labels, where i would like to edit something.
How can I edit the text of for example a label ? And how could I add / remove generated elements from the form ? I tried with refresh but it doesnt work
The Form
This was something quick I tried:
Dim LabelList As New List(Of Integer)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim LBL As New Label
LBL.Location = New Point(6, 23)
LBL.AutoSize = True
Me.Controls.Add(LBL)
LabelList.Add(Me.Controls.Count - 1)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.Controls(LabelList(0)).Text = "HELLO WORLD!"
End Sub
This will add a label to the form and then add it's control index to LabelList. LabelList(0) will get the index for the first added label.

Help with using an array to insert items into a textbox vb.net

I'm still a student without much experience using vb.net and I am having some trouble splitting a string within an array into 2 values. For example in my textbox I have several lines of measurements that are Length x Width: 20x14, 10x8, 16x13. Each measurement is on its own line. I'm trying to split all Width values that are greater than 12 into 2 separate measurements, so with that last example, I would have 5 measurements (LxW): 20x12, 20x2, 10x8, 16x12, 16x1, then I would like to add these measurements to a new textbox with each measurement on its own line.
Here is the code I have so far. Again, I am very new to programming and this is the first serious project for me since "Hello World", so what I have might be way off. Thanks in advance.
Dim room As String = RoomsTextBox.Text
If room.EndsWith(vbCrLf) Then room = room.Substring(0, room.Length - vbCrLf.Length)
Dim roomarray() As String = room.Split(vbCrLf)
Dim Cuts(roomarray.Length - 1, 0) As String
RoomsTextBox.Select(0, 0)
Dim CutLength As Integer
Dim CutWidth As Integer
Dim i As Integer
Dim j As Integer
CutsTextBox.Select()
Cuts(i, j) = (Val(roomarray(i).Split("x")(0))) & Val(roomarray(j).Split("x")(1))
For i = 0 To Cuts.GetUpperBound(0)
For j = 0 To Cuts.GetUpperBound(1)
Cuts(i, j) = 0
Next
If Val(roomarray(i)) > 12 Then
CutWidth = Val(roomarray(i)) - 12
CutLength = Val(roomarray(j))
Else
CutWidth = Val(roomarray(i))
CutLength = Val(roomarray(j))
End If
Dim inserttext = CutsTextBox.Text
Dim insertposition As Integer = CutsTextBox.SelectionStart
CutsTextBox.Text = CutsTextBox.Text.Insert(0, CutLength.ToString & "x" & _
CutWidth.ToString)
CutsTextBox.SelectionStart = insertposition + inserttext.Length
Next i
I even tried it with inserting the measurements into a ListBox. Here is the code for that:
Dim room As String = RoomsTextBox.Text
Dim roomarray() As String = room.Split(vbCrLf)
Dim Cuts(roomarray.Length - 1, 0) As String
Dim CutLength As Integer
Dim CutWidth As Integer
Dim i As Integer
Dim j As Integer
CutsTextBox.Select()
Cuts(i, j) = (Val(roomarray(i).Split("x")(0))) & Val(roomarray(j).Split("x")(1))
For i = 0 To Cuts.GetUpperBound(0)
For j = 0 To Cuts.GetUpperBound(1)
Cuts(i, j) = 0
Next
If Val(roomarray(i)) > 12 Then
CutWidth = Val(roomarray(i)) - 12
CutLength = Val(roomarray(j))
Else
CutWidth = Val(roomarray(i))
CutLength = Val(roomarray(j))
End If
ListBox1.Items.Add(CutLength.ToString & "x" & CutWidth.ToString)
Next i
Try this out.
Dim dimensions As String() = txtInput.Text.Split(vbCrLf)
Dim final As New List(Of String)
For Each item In dimensions
Dim lw As String() = item.Split("x")
Dim length As String = lw(0)
Dim width As Integer = CInt(lw(1))
If width > 12 Then
Dim new1 As String
Dim new2 As String
new1 = length & "x" & (width - 12).ToString
new2 = length & "x12"
final.Add(new1)
final.Add(new2)
Else
final.Add(item)
End If
Next
For Each item In final
txtOutPut.Text += item & vbCrLf
Next