I have two datetime pickers in vb.net that calculate the number of days between two dates and display in a message box the number of days. I want the number to display in a text box using vbnewline or any method but NOT to display in a messageBox
Private Sub btnCalculate_Click(sender As System.Object, e As System.EventArgs)Handles btnCalculate.Click
If True Then
Dim dt1 As DateTime = Convert.ToDateTime(DateTimePicker1.Text)
Dim dt2 As DateTime = Convert.ToDateTime(DateTimePicker2.Text)
Dim ts As TimeSpan = dt2.Subtract(dt1)
If Convert.ToInt32(ts.Days) >= 0 Then
MessageBox.Show("Total Days are " & Convert.ToInt32(ts.Days))
Else
MessageBox.Show("Invalid Input")
End If
End If
End Sub
Any input appreciated
Why not simply use Environment.NewLine
TextBox1.Text = "Total Days are"
TextBox1.AppendText(Environment.NewLine & Convert.ToInt32(ts.Days))
I think you are a bit confused. You can show the info in a TextBox, a MessageBox or a Label, but vbNewLine is just a constant that represents a line break, so you can use it with any of the previous controls.
Just put a TextBox usign the designer and then change the line
MessageBox.Show("Total Days are " & Convert.ToInt32(ts.Days))
with this:
YourTextBox.Text = "Total ......"
Good luck but do a little research the next time.
Related
I populate DataGridView with data and depending on the data I put an image in the 12th column either succes or fail. My goal is to be able to double-click on the 12th cell and show MsgBox with the status inside. I can pretty much do all whats was asked in the homework. However I can't tell in IF CONDITION; if the image in the 12th row = to succes;
here's the error;System.InvalidCastException : 'L'oerator '=' not defined for type 'Bitmap' and type 'Bitmap'.'
thank you for all the help
Private Sub DgvTest_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvTest.CellMouseDoubleClick
If e.ColumnIndex = 12 And dgvTest.Rows(e.RowIndex).Cells("status").Value = imgList.Images.Item("fail") Then
Dim numTest As String = dgvTest.Rows(e.RowIndex).Cells.Item(0).Value
For Each i As Test_Auto In lst_Test_Auto
If i.No_Test = numTest Then
If i.status = False Then
MsgBox("status of test #" & numTest & " ; " & "a generated and error: " & i.message)
End If
End If
Next
End If
End Sub
I have two TextBox elements: textbox1 for hour and textbox2 for minutes. I've tried a lot of codes but they didn't work for me. I always ended up getting:
Conversion from string "1" to type 'Date' is not valid.
I want to display time (in format hh:mm) on a label from two textboxes (textbox1 and textbox2).
I ~think~ you're trying to parse the values from the TextBoxes into a DateTime, and then display that in a Label? If so, try something like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt As DateTime
If DateTime.TryParse(TextBox1.Text & ":" & TextBox2.Text, dt) Then
Label1.Text = dt.ToString
End If
End Sub
If you want only the Time portion displayed, then:
Label1.Text = dt.ToString("h:mm tt")
First of,u didn't mention any code u tried so far.However the solution would be :
textbox1.text=DateTime.Now.ToString("HH")
textbox2.text=DateTime.Now.ToString("mm")
The exception u r getting Cannot convert string to date,this can be fixed by using ToString.In the code, HH stands for Hours and mm stands for minutes
For better understanding , read the documentation of DateTime.Now
I have to prepare a school project of a Currency Converter in Visual Studio using Basic language.
I get the value of extange from XE website and it returns a string like "1.23454 GBP' So I need to remove most characters and numbers from it to have it as a Decimal, not String anymore so I can times it later in calculation.
I've tried to use .Remove anywhere and place it in textBox or Label but it didn't work.
Public Class receipt
Private Sub receipt_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim testString As String = "sadasdasd"
customername.Text = "Name: " + My.Settings.Username
Label6.Text = "Entered Money: " + My.Settings.inputamount + " " + My.Settings.currency
Label7.Text = "Converted To: " + My.Settings.outputamount
Label8.Text = "Charge: 0 (0%)"
TextBox1.Text = testString.Remove(4, 9)
End Sub
End Class
Screenshot of program running
Can someone help me to get this work?
Given your current input of "sadasdasd" and your expected return value then the call to string.Remove should be
TextBox1.Text = testString.Remove(3)
If you look at the MSDN documentation page of string.Remove you could see that the index is zero based so you want to remove anything starting from the fourth character that is at index 3 (keeps 0,1,2, removes from 3)
Also It seems that you think that the Remove second parameter is the end index where the Remove action should stop. But this is wrong, the second parameter is the number of characters to remove so 9 is not correct and probably gives you an ArgumentOutofRange Exception
I am using MS SQL Server and Visual Basic.
I want to combine a calculated column and another column to add units to the calculation. (for example "200 m2")
DgwNewLayout.Columns(5).DefaultCellStyle.Format = "0.00 " & "'" & DgwNewLayout.CurrentRow.Cells(9).Value & "'"
This code does that but only for first entered unit? How can I do this for each column?
You could use CellFormatting event.
Private WithEvents DgwNewLayout As DataGridView
Private Sub DgwNewLayout_CellFormatting(sender As Object,
e As DataGridViewCellFormattingEventArgs
) Handles DgwNewLayout.CellFormatting
If e.ColumnIndex = 5 Then
e.Value = CDbl(e.Value).ToString("0.00") &
" " & CStr(DgwNewLayout.Rows(e.RowIndex).Cells(9).Value)
e.FormattingApplied = True
End If
End Sub
As Dom Sinclair says, this could have poor performance.
I am trying to write a For... Next loop that will allow for 12 entries, or the cancel button.
Somehow intEntries is only using 1, 3, 5, 7, 9, and 11. After that is completed the calculation is being divided by 13, and not 12. I'm not sure what it is I've got wrong, but it's apparently something. Any Assistance you can give me is greatly appreciated!
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'initialize accumulator
Dim decEntries As Decimal
' For loop to ask for input.
For decEntries = 1 To 12 Or strMonthlyAvg = " "
strMonthlyAvg = InputBox("Please Enter the Average for Month # " & decEntries & ":")
decEntries += 1
lstTemps.Items.Add(strMonthlyAvg)
decMontlyAvg = Convert.ToDecimal(strMonthlyAvg)
' This will add the montly average to the total Average Temperature for
' calculations
decTotalTemp += decMontlyAvg
Next
' Calculation to provide the average temp for all entered values
decAnnualAvg = decTotalTemp / decEntries
' convert annual average to string
strAnnualAvg = Convert.ToString(decAnnualAvg)
' Display the results for the user
lblResults.Text = "The average annual temperature " & vbCrLf &
"based on your entries is: " & strAnnualAvg & "."
End Sub
You have decEntries as the counter in the For-Next loop, which increments each time the Next is reached.
However, you are also incrementing it 'manually' in the middle of the loop:
decEntries += 1
remove the line decEntries += 1 the for .. loop increase by one automatically!