How to draw a vertical line on the chart in Visual Basic - vb.net

Im a beginer with Visual Basic. Im trying to draw a vertical line on X asix of the chart, but could not find a way to do so. Please advise.
Private Sub Chart()
'Display Chart
Chart1.Series("ScoreChart").Points.Clear()
For xx As Integer = 0 To DataGridView4.RowCount - 1
Me.Chart1.Series("ScoreChart").Points.AddXY(DataGridView4.Rows(xx).Cells(0).Value, DataGridView4.Rows(xx).Cells(DataGridView4.ColumnCount - 1).Value)
Next
End Sub
Like this in the picture

You can refer to the following code to add a vertical line.
Dim verticalLine As VerticalLineAnnotation = New VerticalLineAnnotation()
verticalLine.AxisX = Chart1.ChartAreas(0).AxisX
verticalLine.IsInfinitive = True
verticalLine.ClipToChartArea = Chart1.ChartAreas(0).Name
verticalLine.LineColor = Color.Red
verticalLine.LineWidth = 2
verticalLine.X = 2
Chart1.Annotations.Add(verticalLine)
Result looks like:

Related

Scroll bar for line graph VB.NET

I created a line graph in Visual Basic to show how many calories the user eats per day. However, my user requires me to include a scroll bar to scroll back and forward along the x-axis to view more days.
Unfortunately, I have never done anything like this before, and after looking through Stack Overflow and Googling, I cannot see any examples of anyone doing so.
Here is a screenshot of my graph so far:
And here is the code:
Cursor.Current = Cursors.WaitCursor
CalorieChartView = True
BurntChartView = False
NetChartView = False
Dim Series As Series = CalorieChart.Series(0)
'keeps track of if the chart is empty, starting as true
Dim empty As Boolean = True
'Clears the chart
Series.Points.Clear()
'Draws the chart in dark red
Series.Color = Color.DarkRed
'The legend text is changed
Series.LegendText = "Calories Consumed"
'For each of the past 8 days, a point is plotted with how many calories were eaten in that day
For i = -7 To 0
Series.Points.Add(User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)))
Series.Points(7 + i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
'If any of the points are not 0
If User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)) <> 0 Then
'the chart is not empty
empty = False
End If
Next
HandleEmpty(empty)
Cursor.Current = Cursors.Default
I would appreciate any help.
If I understand your question you want to add a horizontal scroll bar to your graph. I have made some modification and new code to your code as for mock data purpose. Please refer the below code. You can get the idea by running this code separately.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim blockSize As Integer = 10
Cursor.Current = Cursors.WaitCursor
CalorieChartView = True
BurntChartView = False
NetChartView = False
CalorieChart.Series.Clear()
Dim series = CalorieChart.Series.Add("My Series")
series.ChartType = SeriesChartType.Line
series.XValueType = ChartValueType.Int32
'keeps track of if the chart is empty, starting as true
Dim empty As Boolean = True
'Clears the chart
series.Points.Clear()
'Draws the chart in dark red
series.Color = Color.DarkRed
'The legend text is changed
series.LegendText = "Calories Consumed"
'For each of the past 8 days, a point is plotted with how many calories were eaten in that day
Dim sizeOfDayToDisplay As Int16 = 0
For i = 0 To 100
'Series.Points.Add(User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)))
'Series.Points(7 + i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
''If any of the points are not 0
'If User.GetCaloriesEaten(User.Username, Date.Now.AddDays(i)) <> 0 Then
' 'the chart is not empty
' empty = False
'End If
' just for testing purpose.
series.Points.Add(getRandumNumber())
series.Points(i).AxisLabel = Date.Now.AddDays(i).ToString("dd/MM/yyyy")
' series.Points.AddXY(i, Date.Now.AddDays(i).ToString("dd/MM/yyyy"))
sizeOfDayToDisplay += 1
Next
'most new code added is below here
Dim chartArea = CalorieChart.ChartAreas(Series.ChartArea)
chartArea.AxisX.Minimum = 0
chartArea.AxisX.Maximum = sizeOfDayToDisplay
chartArea.CursorX.AutoScroll = True
chartArea.AxisX.ScaleView.Zoomable = True
chartArea.AxisX.ScaleView.SizeType = DateTimeIntervalType.Number
Dim position As Integer = 0
Dim size As Integer = blockSize
chartArea.AxisX.ScaleView.Zoom(position, size)
chartArea.AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll
chartArea.AxisX.ScaleView.SmallScrollSize = blockSize
'HandleEmpty(empty)
'Cursor.Current = Cursors.Default
End Sub
Public Function getRandumNumber() As Int16
Return CInt(Math.Floor((3500 - 1000 + 1) * Rnd())) + 1000
End Function
Based on this: How to scroll MS Chart along x-axis in vb.net, you can use:
Chart1.Series("LoadCell").Points.AddY(receivedData)
Chart1.ResetAutoValues()
If Chart1.Series("LoadCell").Points.Count >= 100 Then
Chart1.Series("LoadCell").Points.RemoveAt(0)
End If
It Auto scales the y axis as well as limiting the x axis to 100 by
removing the first entry when the entries exeed 100.

Dynamic UserControl Not Showing - flowlayoutpanel

I've array of items and for each item, creating a new instance of custom user control. The problem is that when the control is created and added to Flowlayoutpanel, it's not showing up.
Code
Sub createList()
Dim len_ids As Integer = account_ids.Length - 1
Dim x As Integer = 1
While x <= len_ids
Dim listItem As New wrapperItems()
listItem.txtName.Text = account_names(x)
listItem.picIcon.BackgroundImage = Image.FromFile(account_icons(x))
listItem.lblPath.Text = account_paths(x)
listItem.Tag = "listItem" & x
listItem.Top = 10 * x
listItem.Left = 10
Me.flowWrapper1.Controls.Add(listItem)
listItem.Show() ' tried this
listItem.Visible = True 'tried this
listItem.BringToFront() ' tried this
x += 1
End While
Me.Refresh()
End Sub
Anyone has idea of why it's not working?
It seems that when the background worker is started on Form.Show instead of Form.Load, the code doesn't work.
For the fix, I added background worker to Form.Load().
The better way was finally to add a refresh list button and then load the background worker.

Sending text to projector in VB. NET

I have a need to send text inside richtextbox to projector screen. I have tried to create a new form and make it fullscreen but I cannot yet figure out how I can communicate the text projector as in EasyWorship or PowerPoint. I will appreciate any suggestion. Thanks
I think you don't have to communicate with projector itself.
You can list the available screens, in order to send that form to the correct one (secondary screen). I mean, something like this:
Dim oYourScreen As Screen = nothing
If Screen.AllScreens.Length > 1 Then
For iAux As Integer = 0 To Screen.AllScreens.Length - 1
If Not Screen.AllScreens(iAux).Primary Then
oYourScreen = Screen.AllScreens(iAux)
Exit For
End If
Next
End If
If oYourScreen Is Nothing Then
oYourScreen = Screen.AllScreens(0)
End If
Me.StartPosition = FormStartPosition.Manual
Dim x, y As Integer
x = oYourScreen.Bounds.Location.X + 100 ' adjust as you want
y = oYourScreen.Bounds.Location.X + 30
Me.Location = oYourScreen.Bounds.Location ' or New Point(x, y)

The number of points doesn't work for more than 166..

I am trying to programatically get my Chart object to appear. I have been able to get the Chart to appear using the Design file, but for what I am doing, I need to understand how the Chart Control works programatically. This is my code:
Dim wavesGraph As New Chart()
Dim chartarea As New ChartArea
Dim vn1Series As New Series("VN1", SeriesChartType.Line)
wavesGraph.ChartAreas.Add(chartarea)
wavesGraph.Series.Add(vn1Series)
Console.WriteLine("Here1")
wavesGraph.ChartAreas(0).Visible = True
wavesGraph.ChartAreas(0).AxisY2.Enabled = AxisEnabled.True
wavesGraph.ChartAreas(0).AxisY.Title = "Network Voltage"
If (WavesIsI) Then
wavesGraph.ChartAreas(0).AxisY2.Title = "Network Current"
Else
wavesGraph.ChartAreas(0).AxisY2.Title = "Differential Volts"
End If
Dim rand As New Random
Console.WriteLine("Here2")
For i As Integer = 0 To 500
wavesGraph.Series(0).Points.AddXY(i, rand.Next(-255, 255))
Next
wavesGraph.Enabled = True
wavesGraph.Visible = True
Console.WriteLine("Here3")
Me.Controls.Add(wavesGraph)
The above program doesn't work. If I change the range to be from 0 to 166, then it works fine.
Please anyone help me solve this issue

VB manipulate lots of labels inside panel

So i have like 100 labels inside a panel and i need to to change their texts.I Tried an for and i tried Tab Index like an array without success, any ideas how I can select and change properties of these labels?
Sub setCartela(ByVal numeros As Integer)
For cont As Integer = 0 To numeros Step 1
//change labels text inside panel
Next
End Sub
Try like this ...
Dim i as Integer = 1
For Each ctrl As Control In Panel1.Controls
If ctrl.GetType.ToString = "System.Windows.Forms.Label" Then
ctrl.Text = "Text" & format(i)
End If
i += 1
Next
try this code :
dim _countLbl as integer = 1
For each Lbl as Label in Panel1.Controls.Oftype(Of Label)()
Lbl.text="Label" & _countLbl
_countLbl += 1
Next