Loop through Dates vb.net - vb.net

I want to create a datagridview every day. Here is my code so far. It's not displaying any errors but when I run the code it just load the form but the dgv does not appear. What can I do?
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As Date = Date.Now
Dim apocap As Date = Date.Parse(#8/22/2050#)
Dim loopdate As Date = start
While start < apocap
Dim dgv As New DataGridView
With dgv
.Size = New Size(250, 250)
.ColumnCount = 2
.RowCount = 12
.Location = New Point(12, 9)
.Visible = True
End With
start = start.Date.AddDays(1)
End While
End Sub
End Class

You have to add them to your form. Additionally, you'll want to change the .Left and/or .Top (.Location) properties so they don't all stack on top of each other:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As DateTime = DateTime.Today
Dim apocap As New DateTime(2050, 8, 22)
Dim i As Integer = 0
While start < apocap
Dim dgv As New DataGridView
With dgv
.Size = New Size(250, 250)
.ColumnCount = 2
.RowCount = 12
.Location = New Point(12, (9 + (250 * i)))
.Visible = True
End With
Me.Controls.Add(dgv)
i += 1
start = start.AddDays(1)
End While
End Sub
For fun, I like to write it like this:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As DateTime = DateTime.Today
Dim apocap As New DateTime(2050, 8, 22)
Dim count As Integer = CInt((apocap - start).TotalDays)
Me.Controls.AddRange(Enumerable.Range(0, count).Select(
Function(i)
Return New DataGridView With {
.Size = New Size(250, 250),
.ColumnCount = 21,
.RowCount = 12,
.Location = New Point(12, (9 + (250 * i))),
.Visible = True
}
'start.AddDays(i) is there if you need it
End Function
).ToArray())
End Sub

Related

How can I make this label to just show different string values at each case

Take a look at the simple code below. It is suppose to show string values at different case but the output is only the first one: "what is your email". Please, I need explanation.
The objective is to change the labll text upon click event on next button on the form.
Public Class Form4PassworRecovery
Dim counter As Integer = 0
Private Sub Button1Next_Click(sender As Object, e As EventArgs) Handles Button1Next.Click
Label1Intro.Hide()
Select Case counter
Case 0
Question("What is your Email?")
Case 1
Question("What is your favorite Hobby")
Case 2
Question("What is your minor")
End Select
counter += 1
Answer()
End Sub
Sub Answer()
Dim A As New TextBox
A.Location = New Point(66.5, 120)
A.ForeColor = Color.White
A.BackColor = Color.FromArgb(153, 217, 255)
A.Size = New Point(400, 29)
GroupBox1.Controls.Add(A)
A.Show()
End Sub
Sub Question(ByVal Question As String)
Dim Q As New Label
Q.Text = Question
Q.Location = New Point(66.5, 90)
Q.Size = New Point(400, 29)
Q.ForeColor = Color.White
Q.BackColor = Color.FromArgb(153, 217, 255)
GroupBox1.Controls.Add(Q)
Q.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Cancel" Then
Me.Hide()
Me.Dispose()
End If
End Sub
End Class
Am expecting the output of the form to be different string value at each case but I kept receiving only the first string value.
You can store the locations on your form and then increment them as you place each Label and TextBox pairing:
Dim counter As Integer = 0
Dim labelLocation As Point = New Point(5, 5)
Dim textBoxLocation As Point = New Point(5, 30)
Private Sub Button1Next_Click(sender As Object, e As EventArgs) Handles Button1Next.Click
Label1Intro.Hide()
Select Case counter
Case 0
Question("What is your Email?")
Case 1
Question("What is your favorite Hobby")
Case 2
Question("What is your minor")
End Select
counter += 1
Answer()
End Sub
Sub Question(ByVal Question As String)
Dim Q As New Label
Q.Text = Question
Q.Location = labelLocation
Q.Size = New Size(400, 29)
Q.ForeColor = Color.White
Q.BackColor = Color.FromArgb(153, 217, 255)
GroupBox1.Controls.Add(Q)
Q.Show()
textBoxLocation = New Point(labelLocation.X, labelLocation.Y + Q.Height + 7)
labelLocation = New Point(labelLocation.X, textBoxLocation.Y + 7 + Q.Height)
End Sub
Sub Answer()
Dim A As New TextBox
A.Location = textBoxLocation
A.ForeColor = Color.White
A.BackColor = Color.FromArgb(153, 217, 255)
A.Size = New Size(400, 29)
GroupBox1.Controls.Add(A)
A.Show()
End Sub

Unweighted GPA Calculator Code code problem

I want the user to input credit hours in the TextBox, select grade from the ComboBox for the number of rows generated based on the semester course number entered.
And the code for generating the rows is in the Form1_Load event and the code for calculating the GPA is to be done in the CHECK GPA button. The problem am having is how to save all the credit hours and grades and calculate it.
Here is the code:
Imports System.Windows.Forms
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Show()
Dim Answer As String '= InputBox("Enter number of rows:", "Input Row ", "0")
Dim Row As Integer '= CInt(Answer)
Dim Height As Integer
Dim Valid As Boolean = False
Do While Valid = False
Try
Dim Repeat = True
Do While Repeat = True
Answer = InputBox("Please Enter Number Of Semester Courses:", "GPA CHECKER", "0")
Row = CInt(Answer)
If Row > 0 And Row <= 13 Then
Repeat = False
Else
Repeat = True
End If
Loop
Valid = True
Catch ex As InvalidCastException
MsgBox("You Can Only Input Numbers!", MsgBoxStyle.Information)
Valid = False
End Try
Loop
For index As Integer = 1 To Row
Dim myPanel As New Panel
Dim myLabel As New Label
Dim myTextBox As New TextBox
Dim myComboBox As New ComboBox
myPanel.Name = "myPanel" + index.ToString
myPanel.Location = New Point(12, 70 + Height)
myPanel.Size = New Size(260, 34)
myLabel.Text = "Course" + index.ToString
myLabel.Size = New Size(80, 22)
myLabel.TextAlign = ContentAlignment.MiddleLeft
myLabel.Location = New Point(45, 12)
myTextBox.Name = "CreditHours"
myTextBox.Size = New Size(50, 22)
myTextBox.Location = New Size(125, 12)
myComboBox.Name = "Grade"
myComboBox.Size = New Size(50, 22)
myComboBox.Location = New Size(210, 12)
myComboBox.Items.Add("A+")
myComboBox.Items.Add("A")
myComboBox.Items.Add("B+")
myComboBox.Items.Add("B")
myComboBox.Items.Add("C+")
myComboBox.Items.Add("C")
myComboBox.Items.Add("D+")
myComboBox.Items.Add("D")
myComboBox.Items.Add("F")
myPanel.Controls.Add(myLabel)
myPanel.Controls.Add(myTextBox)
myPanel.Controls.Add(myComboBox)
Me.Controls.Add(myPanel)
Height += 34
Me.Show()
Next
End Sub
Private Sub btnCheckGpa_Click(sender As Object, e As EventArgs) Handles btnCheckGpa.Click
Form2.Show()
Me.Hide()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class

How to add picture boxes in rows in vb 2010

I am not sure how to create picture boxes on multiple rows in vb 2010. At the moment, I am only able to create them on
one row and one picture box on the second row. (Eventually I would like to add in 5 rows with 10 pictureboxes on each row) I have used the follow code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = 20
Dim yPosition As Integer = 40
For i As Integer = 1 To 20
Dim pb As New PictureBox
With pb
If i < 20 Then
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(xPosition, yPosition)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
xPosition += 70
ElseIf i > 10 Then
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(20, 120)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
xPosition += 70
End If
Dim thisSeating As New Seating
With thisSeating
.SeatNumber = i
.PB = pb
.Occupied = False
End With
seatingList.Add(thisSeating)
End With
Next
End Sub
If anyone would be willing to help me or direct me to the correct path, I would be very grateful :)
Thanks in advance
Is this what you're looking for?
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = 20
Dim yPosition As Integer = 40
For i As Integer = 1 To 5
For j As Integer = 1 To 10
Dim pb As New PictureBox
With pb
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(xPosition + (j * 70), yPosition + (i * 100))
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
Dim thisSeating As New Seating
With thisSeating
.SeatNumber = i
.PB = pb
.Occupied = False
End With
seatingList.Add(thisSeating)
End With
Next
Next
End Sub

How to change image in picture box when clicked

I have used the following code in a program that I am designing to book seats. Each picturebox is a seat, and when each picturebox is clicked, the image should change from Seating_No_Person to Seating_With_Person to show that the seat has been selected. I am currently getting a problem with the changing image, as when clicked, none of the pictureboxes swap images. Anyone got any suggestions?
Thanks
Public Class Form1
Public Class Seating
Public SeatRow As Integer = 0
Public SeatColumn As Integer = 0
Public PB As PictureBox = Nothing
Public Occupied As Boolean = False
End Class
Private seatingList As New List(Of Seating)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = -50
Dim yPosition As Integer = -25
For i As Integer = 1 To 5
'Number of rows
For j As Integer = 1 To 10
Dim pb As New PictureBox
With pb
.Name = "PictureBox" & i.ToString & j.ToString
'Name of Picture box i.e. if i = 1 (row 1), j = 3 (column 3), name is PictureBox13
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
'Size of seat is 60 by 60
.Location = New Point(xPosition + (j * 70), yPosition + (i * 70))
'Location of picture box is: -50 + (columnnumber * 70), -25 + (rownumber * 70)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
Dim thisSeating As New Seating
With thisSeating
.SeatRow = i
.SeatColumn = j
.PB = pb
.Occupied = True
End With
seatingList.Add(thisSeating)
End With
Next
Next
End Sub
Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
Dim seatRowNum As Integer = CInt(pb.Name.Replace("PictureBox", ""))
Dim seatColumnNum As Integer = CInt(pb.Name.Replace("PictureBox", ""))
Dim qry = From seat As Seating In seatingList Where seat.SeatRow = seatRowNum And seat.SeatColumn = SeatColumnNum
If qry.Count = 1 Then
If qry.First.Occupied = True Then
pb.Image = My.Resources.Seating_No_Person
qry.First.Occupied = False
Else
pb.Image = My.Resources.Seating_With_Person
qry.First.Occupied = True
End If
End If
End Sub
End Class
I would suggest setting a breakpoint and debugging to see where you're going wrong. If you just call DirectCast(sender, PictureBox).Image = My.Resources.Seating_With_Person inside Private Sub PictureBox_Click it works, which suggests that there is problem with the logic inside your If block.

How can i make a point in a chart equal the value of a textbox?

In my project I have created a chart and I want there to be the same number of points as in my textboxes, how can i do that? this is my code
Public Class Form1
Dim a As Object
Dim b As Object
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
Chart1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top
Chart1.ChartAreas(0).AxisX.Maximum = 20
Chart1.ChartAreas(0).AxisY.Maximum = 30
Chart1.ChartAreas(0).AxisX.Minimum = -20
Chart1.ChartAreas(0).AxisY.Minimum = -30
Chart1.ChartAreas(0).AxisY.Interval = 5
Chart1.ChartAreas(0).AxisX.Interval = 5
Chart1.ChartAreas(0).AxisX.Crossing = 0
Chart1.ChartAreas(0).AxisY.Crossing = 0
Chart1.ChartAreas(0).AxisX.LineWidth = 2
Chart1.ChartAreas(0).AxisY.LineWidth = 2
Chart1.ChartAreas(0).AxisY.MajorGrid.LineColor = Color.Black
a = New DataPoint(0, 0)
a.Label = "#VALX : #VALY"
a.MarkerStyle = MarkerStyle.Circle
a.MarkerSize = 5
Chart1.Series(0).Points.Add(a)
End Sub
And I want the datapoints to be like
a =(val(textbox1.text),val(textbox2.text))
In the below example I just created a collection of textboxes and looped through them adding the x and y coordinates from the textboxes
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim Xtbs() As TextBox = {TextBox1, TextBox3, TextBox5}
Dim Ytbs() As TextBox = {TextBox2, TextBox4, TextBox6}
For i As Integer = 0 To Xtbs.Length - 1
Dim x As Double = Xtbs(i).Text
Dim y As Double = Ytbs(i).Text
Dim pt = New DataPoint(x, y)
Chart1.Series(0).Points.Add(pt)
Next
End Sub