I have programmed a calculator with Visual Basic, but I would like to add a few more functions. But I can't figure out how to solve the following problems in my code:
if more than one zero is entered in the input field, it should simply delete the leading zeros.(example 0000111 -----> 111 or 0002345 ----> 2345)
likewise, a decimal number should not have several zeros before the decimal point (example 000.176 ----> 0.176)
furthermore, it should not be possible to enter two commas (Example 0.187.56 -----> should not be Possible -------------> 0.18756)
Could somebody help me ?
Public Class Form1
Public LCD As Single
Public OPZ As String
Private Sub LCDfuellen(sender As Object, e As EventArgs) Handles btn3.Click, btn2.Click, btn1.Click, btn9.Click, btn8.Click, btn7.Click, btn6.Click, btn5.Click, btn4.Click
txtLCD.Text &= ActiveControl.Tag
Replace(LTrim(Replace(txtLCD.Text, "0", " ")), " ", "0")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtLCD.Clear()
End Sub
Private Sub btnCE_Click(sender As Object, e As EventArgs) Handles btnCE.Click
'txtLCD.Text = txtLCD.Text.Remove(txtLCD.TextLength - 1, 1)
txtLCD.Text = LSet(txtLCD.Text, txtLCD.TextLength - 1)
End Sub
Private Sub OPZlesen(sender As Object, e As EventArgs) Handles btnPlus.Click, btnMinus.Click, btnMal.Click, btnDurch.Click
LCD = CSng(txtLCD.Text)
txtLCD.Clear()
OPZ = ActiveControl.Tag
End Sub
Private Sub btnGleich_Click(sender As Object, e As EventArgs) Handles btnGleich.Click
Dim activeLCD As Single
activeLCD = CSng(txtLCD.Text)
Select Case OPZ
Case "+" : txtLCD.Text = CStr(LCD + activeLCD)
Case "-" : txtLCD.Text = CStr(LCD - activeLCD)
Case "*" : txtLCD.Text = CStr(LCD * activeLCD)
Case "/" : txtLCD.Text = CStr(LCD / activeLCD)
End Select
End Sub
Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
txtLCD.Text &= ActiveControl.Tag
End Sub
Private Sub btnPunkt_Click(sender As Object, e As EventArgs) Handles btnPunkt.Click
txtLCD.Text &= ActiveControl.Tag
End Sub
End Class
Related
This is my code but it prints all records. I want to search a record and print crystalreport.
Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
Me.Patient_tblTableAdapter.Fill(Me.DoctorDataSet.patient_tbl)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PatienttblBindingSource.Filter = "(Convert(ID,'System.String') LIKE '" & TextBox1.Text & "%')"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rpt As New CrystalReport1
rpt.SetDataSource(DoctorDataSet)
Form2.CrystalReportViewer1.ReportSource = rpt
Form2.ShowDialog()
End Sub
End Class
try to use '*' instead '%' in like statement :
PatienttblBindingSource.Filter = "(Convert(ID,'System.String') LIKE '" & TextBox1.Text & "*')"
So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub
Im trying to create a web browser is Visual Studio 2013 but i keep getting the error:
An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code
Additional information: Conversion from string "0-1" to type 'Double' is not valid.
when I run the program.
The error occurred after i added a progress bar.
My Code:
Public Class Form1
Dim MyTemp As String = My.Settings.homepage
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub AboutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem.Click
MsgBox("Created by Lachlan Johnson" & vbCrLf & " (2016)", 0, "About")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
WebBrowser1.Navigate(TextBox1.Text)
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
WebBrowser1.Navigate(TextBox1.Text)
e.Handled = True
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate("https://www.google.com.au/webhp#q=" + TextBox2.Text)
End Sub
Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
WebBrowser1.Navigate("https://www.google.com.au/webhp#q=" + TextBox2.Text)
e.Handled = True
End If
End Sub
Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
ToolStripStatusLabel1.Text = "Loading..."
End Sub
Private Sub WebBrowser1_Navigated(sender As Object, e As WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated
ToolStripStatusLabel1.Text = "Complete"
End Sub
Private Sub SetAsHomepageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SetAsHomepageToolStripMenuItem.Click
My.Settings.homepage = WebBrowser1.Url.ToString
My.Settings.Save()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(MyTemp)
End Sub
Private Sub WebBrowser1_ProgressChanged(sender As Object, e As WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
If Int(e.MaximumProgress > 0 & e.CurrentProgress > 0) Then
ToolStripProgressBar1.ProgressBar.Value = e.CurrentProgress * 100 / e.MaximumProgress
End If
End Sub
End Class
The error occured after i added this:
Private Sub WebBrowser1_ProgressChanged(sender As Object, e As WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
If Int(e.MaximumProgress > 0 & e.CurrentProgress > 0) Then
ToolStripProgressBar1.ProgressBar.Value = e.CurrentProgress * 100 / e.MaximumProgress
End If
End Sub
I cant seem to find the issue,
Any help is greatly appreciated.
Lachlan
The issue is that you're using a string concatenation operator here:
If Int(e.MaximumProgress > 0 & e.CurrentProgress > 0) Then
That should be:
If e.MaximumProgress > 0.0 AndAlso e.CurrentProgress > 0.0 Then
This
Int(e.MaximumProgress > 0 & e.CurrentProgress > 0)
Does a string concatenation of MaximumProgress and CurrentProgress. If CurrentProgress is -1 (indicating completion) and MaximumProgress is 0 (indicating the total number of bytes to be transferred is 0) then e.MaximumProgress > 0 & e.CurrentProgress > 0 will result in the string "0-1". Then the Int() function converts it's parameter, "0-1" into a double so it can truncate the decimal part and return just the integral part. The problem is, "0-1" isn't a valid double.
Public Class Form1
Public digits As String = "0123456789"
Public userInput As String
Public digitCount As Integer = 0
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
userInput = Console.ReadLine()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each i As Char In userInput
If digits.Contains(i) Then digitCount += 1
Next
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub MaskedTextBox1_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
End Sub
End Class
What should my loop be since this one isn't working and what is the syntax for entering in my digitCount in my maskedtextbox1
Replace your loop with the following simple statement:
digitCount = userInput.Count(Function(c) Char.IsDigit(c))
N.B. Requires reference to System.Linq, which is there by default.
So I'm doing this calculator program and need the numbers, the operator used and the "=" sign to show up in the listbox so "1 + 1 = 2" should show up. I have the calculator working and can move items to the listbox, but can't figure out how to remember the button that was clicked to get the outcome and move it over also.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & NumTwo.Text & Result.Text)
End Sub
End Class
As far as you just need to store one character, you can rely on the Tag property of ListBox1 (basically, a black box where you can store anything you want). Sample code:
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
ListBox1.Tag = "+"
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
ListBox1.Tag = "-"
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
ListBox1.Tag = "x"
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
ListBox1.Tag = "/"
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & "=" & Result.Text)
End Sub