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
Related
I'm trying to get 10 different inputs in TextBoxes to be displayed in a randomized order.
I split 2 processes to make it easier to understand:
Button puts numbers in random orders from 1-10(works)
Button should replace the numbers with the contents of the TextBoxes(doesn't work)
Here is the GUI
/ Here is the code:
Public tblRandom As New DataTable
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnGo.Click
Dim r As New Random
Dim tblRandom As New DataTable
tblRandom.Columns.Add("Order")
tblRandom.Constraints.Add("pk", tblRandom.Columns(0), True)
While tblRandom.Rows.Count < 10
Dim newrow As Object = r.Next(10) + 1
Try
tblRandom.Rows.Add(CStr(newrow))
Catch ex As Exception
End Try
End While
dgvRandom.DataSource = tblRandom
End Sub
Private Sub btnReplace_Click(sender As Object, e As EventArgs) Handles btnReplace.Click
For Each row As DataGridViewRow In dgvRandom.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.Value IsNot Nothing Then
Dim i As Integer = 0
While i < 10
i += 1
If cell.Value.ToString = i Then
cell.Value = ActiveControl.Tag(i)
End If
End While
End If
Next
Next
dgvRandom.DataSource = tblRandom
End Sub
End Class
My understanding of your goal here is to take the text in 10 textboxes and shuffled their content. The DataGrid seems to be a temporary location that you're using during the shuffle.
There's a much easier way that avoids all that. Try this code:
Private _random = New Random()
Private Sub BtnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
Dim tbs As TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10}
Dim text = tbs.Select(Function(tb) tb.Text).OrderBy(Function(t) _random.Next()).ToList()
For Each x In tbs.Zip(text, Function(tb, t) New With {.tb = tb, .t = t})
x.tb.Text = x.t
Next
End Sub
That's it. Job done.
If you still need the DataGrid it would be easy to add a further line just after the x.tb.Text = x.t that adds the content of x.t to the grid.
For the first part you can do:
Public tblRandom As DataTable
Private ReadOnly rand As New Random
Private Sub btnRandom_Click(sender As Object, e As EventArgs) Handles btnRandom.Click
tblRandom?.Dispose()
tblRandom = New DataTable
tblRandom.Columns.Add("Order")
tblRandom.Constraints.Add("pk", tblRandom.Columns(0), True)
Dim nums As New List(Of Integer)
While nums.Count < 10
Dim num = rand.Next(1, 11)
If Not nums.Contains(num) Then
nums.Add(num)
End If
End While
nums.ForEach(Sub(n) tblRandom.Rows.Add(n))
dgvRandom.DataSource = tblRandom
End Sub
As for the second part, you just need to do:
Private Sub btnReplace_Click(sender As Object, e As EventArgs) Handles btnReplace.Click
For Each row As DataRow In tblRandom.Rows
'Assuming the text boxes are hosted by the Form.
Dim txt = Me.Controls.OfType(Of TextBox).
Where(Function(x) x.Tag?.ToString = row(0).ToString).
FirstOrDefault
If txt IsNot Nothing Then
row(0) = txt.Text
End If
Next
tblRandom.AcceptChanges()
End Sub
You are getting the tag from the ActiveControl
cell.Value = ActiveControl.Tag(i)
but as you've just clicked the Replace button, isn't that the active control?
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
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
I m new to vb.net & I have a problem that whenever user enters an alphabet he/she will receive message that only numbers are allowed. For this code.... Please help me. I shall be very thankful to you.
Public Class Form1
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 0 Then
Dim combo As ComboBox = CType(e.Control, ComboBox)
If (combo IsNot Nothing) Then
RemoveHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
AddHandler combo.SelectionChangeCommitted, New EventHandler(AddressOf ComboBox_SelectionChangeCommitted)
End If
End If
End Sub
Private Sub ComboBox_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim combo As ComboBox = CType(sender, ComboBox)
If combo.SelectedItem = "Item1" Then
DataGridView1.CurrentRow.Cells(1).Value = "KG"
DataGridView1.CurrentRow.Cells(3).Value = "100"
DataGridView1.CurrentRow.Cells(2).Value = "Raw Material"
ElseIf combo.SelectedItem = "Item2" Then
DataGridView1.CurrentRow.Cells(1).Value = "Liter"
DataGridView1.CurrentRow.Cells(3).Value = "47"
DataGridView1.CurrentRow.Cells(2).Value = "Raw Material"
ElseIf combo.SelectedItem = "Item3" Then
DataGridView1.CurrentRow.Cells(1).Value = "Pound"
DataGridView1.CurrentRow.Cells(3).Value = "54"
DataGridView1.CurrentRow.Cells(2).Value = "Raw Material"
End If
End Sub
Private Sub Mul_Button_Click(sender As Object, e As EventArgs) Handles Mul_Button.Click
Dim s As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(3).Value)
Dim s1 As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(4).Value)
DataGridView1.CurrentRow.Cells(5).Value = s * s1
End Sub
Private Sub DataGridView1_CellValidated(sender As Object, e As EventArgs) Handles DataGridView1.CellValidated
Dim s As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(3).Value)
Dim s1 As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(4).Value)
DataGridView1.CurrentRow.Cells(5).Value = s * s1
If DataGridView1.RowCount > 0 Then
Dim sum As Integer
For index As Integer = 0 To DataGridView1.RowCount - 1
sum += Convert.ToInt32(DataGridView1.Rows(index).Cells(5).Value)
Next
TextBox1.Text = sum
End If
End Sub
Private Sub Add_Button_Click(sender As Object, e As EventArgs) Handles Add_Button.Click
If DataGridView1.RowCount > 0 Then
Dim sum As Integer
For index As Integer = 0 To DataGridView1.RowCount - 1
sum += Convert.ToInt32(DataGridView1.Rows(index).Cells(5).Value)
Next
TextBox1.Text = sum
End If
End Sub
End Class
Either wrap your code that has Convert.ToInt16() calls in them with Try/Catch blocks, or convert them to the Int16.TryParse() approach instead.
For example, this line:
Dim s As Int16 = Convert.ToInt16(DataGridView1.CurrentRow.Cells(3).Value)
Could become:
Dim s As Int16
Dim strValue As String = DataGridView1.CurrentRow.Cells(3).Value
If Int16.TryParse(strValue, s) Then
' ... do something with "s" in here ...
' ... continue with code...
Else
MessageBox.Show(strValue, "Invalid Value")
End If
The TabLoad() function doesn't seem to be working on clicking of this dynamic button. The aim of this button click event is that it deletes text from a text file and loads the form again.
Below is the complete code. The TabLoad() function is in the NextDelbtn_Click sub at the end.
Also any suggestion regarding change of code is appreciated.
Imports System.IO
Public Class Form1
Dim str As String
Dim FILE_NAME As String = "D:\1.txt"
Dim file_exists As Boolean = File.Exists(FILE_NAME)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Please enter text that you want to save", MsgBoxStyle.Information, "TOCC Error")
Else
str = TextBox1.Text
Dim fs As FileStream = Nothing
If (Not File.Exists(FILE_NAME)) Then
fs = File.Create(FILE_NAME)
Using fs
End Using
End If
If File.Exists(FILE_NAME) Then
Dim sw As StreamWriter
sw = File.AppendText(FILE_NAME)
sw.WriteLine(str)
sw.Flush()
sw.Close()
TabLoad()
TextBox1.Text = ""
End If
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TabControl1.SelectedIndex = TabControl1.TabIndex + 1
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TabLoad()
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
Dim s As String = DirectCast(DirectCast(sender, Button).Tag, Label).Text
Clipboard.SetText(s)
End Sub
Private Sub TabLoad()
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
Dim NextDelbtn As New Button
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextDelbtn.Text = "Delete"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextDelbtn.Width = 55
NextDelbtn.Height = 22
Nextbtn.BackColor = Color.WhiteSmoke
NextLabel.Tag = Nextbtn
Nextbtn.Tag = NextLabel
NextDelbtn.Tag = NextLabel
NextDelbtn.BackColor = Color.WhiteSmoke
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
TabPage2.Controls.Add(NextDelbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
NextDelbtn.Location = New Point(180, 10 * i + ((i - 1) * NextDelbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
AddHandler NextDelbtn.Click, AddressOf Me.NextDelbtn_Click
i += 1
Next
File.WriteAllLines(FILE_NAME,
File.ReadAllLines(FILE_NAME).Where(Function(y) y <> String.Empty))
End Sub
Private Sub NextDelbtn_Click(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim s As String = (btn.Tag).Text
Dim lines() As String = IO.File.ReadAllLines(FILE_NAME)
Using sw As New IO.StreamWriter(FILE_NAME)
For Each line As String In lines
If line = s Then
line = ""
End If
sw.WriteLine(line)
Next
sw.Flush()
sw.Close()
TabLoad()
End Using
End Sub
End Class
Your problem appears to be that you're initializing new controls but you're not changing the controls on the form or even creating a new form.