vb.Net Checking to see what are the two Largest Numbers - vb.net

I have 4 text box that accepts numbers from user and i want to check for what are the two largest numbers entered, can this be done using if then else statements
Dim Big_num_1 As Integer
Dim Big_num_2 As Integer
'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
'if then else statements
End Sub
i am a beginer in Vb.net and i would really appreciate any help that i can get
If possible i would like a If then else statement solution for this

This can be done with an if-statement like this..
Dim Big_num_1 As Integer = 0
Dim Big_num_2 As Integer = 0
'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
Dim numList(4) as Integer
numList(0) = txtbox_1
numList(1) = txtbox_2
numList(2) = txtbox_3
numList(3) = txtbox_4
For x as Integer = 0 to numList.count - 1
If numList(x) > Big_num_1 Then
Big_num_2 = Big_num_1
Big_num_1 = numList(x)
Else If numList(x) > Big_num_2 Then
Big_num_2 = numList(x)
End If
Next
End Sub

Private Sub Docheck()
Dim Numbers As String() = {TextBox1.Text, TextBox2.Text, TextBox3.Text}
Dim Ordered As String() = Numbers.OrderByDescending(Function(x) CInt(x)).ToArray
Dim Highest As Integer = CInt(Ordered(0))
Dim SecondHighest As Integer = CInt(Ordered(1))
MessageBox.Show(String.Concat(Highest , " " , SecondHighest))
End Sub
And in your button click event you call
Docheck()

You could use If/Else statements, but that would be the hard way to do this:
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
Dim Numbers = { TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text }
Dim results = Numbers.Select(Function(s) Integer.Parse(s)).OrderByDescending().Take(2).ToList()
Big_num_1 = results(0)
Big_num_2 = results(1)
End Sub

Related

How to use SQL IN statement with list of Items selected from treeviewlist?

How can I join the accid string values from the For Next loop (below) into one single string?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim accid As String
Dim iLast As Integer
iLast = trv.Nodes.Count
Dim p As Integer = 0
For p = 0 To iLast - 1
If TrV.Nodes(p).Checked = True Then
accid = Strings.Left(TrV.Nodes(p).Text, 9)
MsgBox(accid)
End If
Next
End Sub
This gives me a separate output of string "accid"
I want this output: "accid1,accid2,accid3"
Thanks for supporting!
You need to build your string inside the loop, and then do the MsgBox outside the loop. Something like this should work:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim accid As String
Dim iLast As Integer
iLast = trv.Nodes.Count
Dim p As Integer = 0
For p = 0 To iLast - 1
If TrV.Nodes(p).Checked = True Then
accid = accid & Strings.Left(TrV.Nodes(p).Text, 9) & "," 'notice the change here
End If
Next
accid = accid.Remove(accid.Length - 1, 1)
MsgBox(accid)
End Sub
This could be done with very few lines of code using Linq and String.Join.
Dim texts = TrV.Nodes.Cast(Of TreeNode)().
Where(Function(n) n.Checked).
Select(Function(c) Strings.Left(c.Text,9))
Dim result = String.Join(",", texts)

Get Character Of IndexOf

For an assigment my teacher is asking that we read from a file to find the characters of our name and place them at a label at the top of the form.
here is my code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
searchFile = File.OpenText("AcademicEthicsandIntegrityStatement.txt")
Dim s As String = searchFile.ReadToEnd
Dim b As String = s.IndexOf("b"c)
Dim r As Integer = s.IndexOf("r"c)
Dim i As Integer = s.IndexOf("i"c)
Dim a As Integer = s.IndexOf("a"c)
Dim n As Integer = s.IndexOf("n"c)
Dim ec As Integer = s.IndexOf("e"c)
Dim bRead = GetChar(s, b)
Dim rRead = GetChar(s, r)
Dim iRead = GetChar(s, i)
Dim aRead = GetChar(s, a)
Dim nRead = GetChar(s, n)
Dim ecRead = GetChar(s, ec)
lblName.Text = bRead + rRead + iRead + aRead + nRead + nRead + ecRead
End Sub
The text that is reading into my lbl is "gmcaad" instead of "brianne"
Im sure that I am missing something here or there is a much easier way to do this. Any help is appreciated.
IndexOf returns a zero-based index.
GetChar accepts a one-based index.
For consistency,
if you want to use IndexOf, then use direct indexing instead of GetChar:
Dim bRead = s(b)
Dim rRead = s(r)
if you want to use GetChar, then use InStr instead of IndexOf that also returns one-based values.
Short Answer...case sensitive:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With File.ReadAllText("AcademicEthicsandIntegrityStatement.txt")
For Each C As Char In "Brianne".ToCharArray
' Note this is case-sensitive because it uses a binary comparison
Dim Index As Integer = .IndexOf(C)
If Index >= 0 Then lblName.Text &= .Substring(Index, 1)
Next
End With
End Sub
... and non-case sensitive:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With File.ReadAllText("AcademicEthicsandIntegrityStatement.txt")
For Each C As Char In "Brianne".ToCharArray
' Note this is not case-sensitive
Dim Index As Integer = .IndexOf(C.ToString, StringComparison.InvariantCultureIgnoreCase)
If Index >= 0 Then lblName.Text &= .Substring(Index, 1)
Next
End With
End Sub

Visual basic 2010, database – how to fill specific field

I'm creating database connection in Visual Basic. When user click on "borrow book" it transfer chosen data into table used for lent books. It all works alright, but I need last thing. In my database in lent table I have attribute "End date of lent" and I want fill it everytime when user borrow a book with date today + 1 month (for instance, if now is 19/04/2016, end date will be 19/05/2016). I created method "BorrowTime" for this, but I don't know what I should type into and how can I get value of inserted row into "row". And sorry for horrible look of code and form...
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class
I need to put code here:
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
My form
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class

Error message whenever alphabet is entered rather than number

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

Visual basic less than 0

Im trying to make a program that when i put a value less than 0, in a label something like "
negative numbers cant be used i have this
Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim intSide1 As Integer = txtSide1.Text
Dim intSide2 As Integer = txtSide2.Text
Dim intSide3 As Integer = txtSide3.Text
Dim intSide4 As Integer = txtSide4.Text
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
Catch ex As Exception
MessageBox.Show("Incorrect Input. Enter a numeric value.")
End Try
End Sub
End Class
Yes i have to use the try catch method and i need help for this
This sounds an awful lot like homework, but you really need to convert your integers properly and then perform actual comparisons.
Public Class Form1 'Sebastian roman. Perimeter, 10/1/2014
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
' Relies on the GetIntegerInput method to throw exceptions
' for invalid entries
Dim intSide1 As Integer = GetIntegerInput(txtSide1.Text)
Dim intSide2 As Integer = GetIntegerInput(txtSide2.Text)
Dim intSide3 As Integer = GetIntegerInput(txtSide3.Text)
Dim intSide4 As Integer = GetIntegerInput(txtSide4.Text)
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Function GetIntegerInput(ByVal input as String) As Integer)
Dim returnValue as Integer
' Will attempt a proper try parse. AndAlso will short circuit
' the comparison so a failure in TryParse will not perform the
' the second evaluation. In either case, an actual exception is
' thrown with your invalid numeric message
If (Not Int32.TryParse(input, returnValue) AndAlso returnValue < 0) Then
Throw New ArgumentException("Incorrect input. Enter a proper numeric value.")
End If
Return returnValue
End Function
End Class
Well a integer can be negative...
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Try
Dim intSide1 As Integer = txtSide1.Text
Dim intSide2 As Integer = txtSide2.Text
Dim intSide3 As Integer = txtSide3.Text
Dim intSide4 As Integer = txtSide4.Text
Dim intTotal As Integer = intSide1 + intSide2 + intSide3 + intSide4
lblMessage.Text = intTotal.ToString("#,###.##")
//New code
if intSide1 < 0 or intSide2 < 0 or intSide3 < 0 intSide4 < 0 Then
MessageBox.Show("Incorrect Input. Negative number not valid")
end if
Catch ex As Exception
MessageBox.Show("Incorrect Input. Enter a numeric value.")
End Try
End Sub