Visual Basic : Is there a WAY to shorten this raw codes - vb.net
I designed some software to help sellers for calculating the products bought by the costumer.
I coded it too raw, I know this code can be shortened but I don't know what to do.
And this is my code:
Public Class Form1
Private Strg1, Strg2, Strg3, Strg4, Strg5, Strg6, Strg7, Strg8, Strg9, Strg10, Strg11, Strg12, Strg13, Strg14, Strg15, Strg16, Strg17, Strg18, Strg19, Strg20, Strg21, Strg22, Strg23, Strg24, Strg25, Strg26, Strg27, Strg28, Strg29, Strg30 As Double
Private p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20, p21, p22, p23, p24, p25, p26, p27, p28, p29, p30, MedPrc1, MedPrc2, TotMedPrc As Double
Private Function QntCalc(ByVal x As Double, ByVal y As Double) As Double
Return x * y
End Function
Private Sub Form1_Load(ByVal sder As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MedPrc1 = 0
MedPrc2 = 0
TotMedPrc = 0
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If TextBox1.Enabled = False Then
TextBox1.Enabled = True
p1 = Val(Label1.Text)
Else
TextBox1.Enabled = False
TextBox1.Text = ""
p1 = 0
End If
End Sub
Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
If TextBox2.Enabled = False Then
TextBox2.Enabled = True
p2 = Val(Label2.Text)
Else
TextBox2.Enabled = False
TextBox2.Text = ""
p2 = 0
End If
End Sub
Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
If TextBox3.Enabled = False Then
TextBox3.Enabled = True
p3 = Val(Label3.Text)
Else
TextBox3.Enabled = False
TextBox3.Text = ""
p3 = 0
End If
End Sub
Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
If TextBox4.Enabled = False Then
TextBox4.Enabled = True
p4 = Val(Label4.Text)
Else
TextBox4.Enabled = False
TextBox4.Text = ""
p4 = 0
End If
End Sub
Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
If TextBox5.Enabled = False Then
TextBox5.Enabled = True
p5 = Val(Label5.Text)
Else
TextBox5.Enabled = False
TextBox5.Text = ""
p5 = 0
End If
End Sub
Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
If TextBox6.Enabled = False Then
TextBox6.Enabled = True
p6 = Val(Label6.Text)
Else
TextBox6.Enabled = False
TextBox6.Text = ""
p6 = 0
End If
End Sub
Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
If TextBox7.Enabled = False Then
TextBox7.Enabled = True
p7 = Val(Label7.Text)
Else
TextBox7.Enabled = False
TextBox7.Text = ""
p7 = 0
End If
End Sub
Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
If TextBox8.Enabled = False Then
TextBox8.Enabled = True
p8 = Val(Label8.Text)
Else
TextBox8.Enabled = False
TextBox8.Text = ""
p8 = 0
End If
End Sub
Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged
If TextBox9.Enabled = False Then
TextBox9.Enabled = True
p9 = Val(Label9.Text)
Else
TextBox9.Enabled = False
TextBox9.Text = ""
p9 = 0
End If
End Sub
Private Sub CheckBox10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox10.CheckedChanged
If TextBox10.Enabled = False Then
TextBox10.Enabled = True
p10 = Val(Label10.Text)
Else
TextBox10.Enabled = False
TextBox10.Text = ""
p10 = 0
End If
End Sub
Private Sub CheckBox11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged
If TextBox11.Enabled = False Then
TextBox11.Enabled = True
Else
TextBox11.Enabled = False
TextBox11.Text = ""
p1 = 0
End If
End Sub
Private Sub CheckBox12_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox12.CheckedChanged
If TextBox12.Enabled = False Then
TextBox12.Enabled = True
Else
TextBox12.Enabled = False
TextBox12.Text = ""
End If
End Sub
Private Sub CheckBox13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox13.CheckedChanged
If TextBox13.Enabled = False Then
TextBox13.Enabled = True
Else
TextBox13.Enabled = False
TextBox13.Text = ""
p1 = 0
End If
End Sub
Private Sub CheckBox14_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox14.CheckedChanged
If TextBox14.Enabled = False Then
TextBox14.Enabled = True
Else
TextBox14.Enabled = False
TextBox14.Text = ""
End If
End Sub
Private Sub CheckBox15_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox15.CheckedChanged
If TextBox15.Enabled = False Then
TextBox15.Enabled = True
Else
TextBox15.Enabled = False
TextBox15.Text = ""
End If
End Sub
Private Sub CheckBox16_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox16.CheckedChanged
If TextBox16.Enabled = False Then
TextBox16.Enabled = True
Else
TextBox16.Enabled = False
TextBox16.Text = ""
End If
End Sub
Private Sub CheckBox17_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox17.CheckedChanged
If TextBox17.Enabled = False Then
TextBox17.Enabled = True
Else
TextBox17.Enabled = False
TextBox17.Text = ""
End If
End Sub
Private Sub CheckBox18_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox18.CheckedChanged
If TextBox18.Enabled = False Then
TextBox18.Enabled = True
Else
TextBox18.Enabled = False
TextBox18.Text = ""
End If
End Sub
Private Sub CheckBox19_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox19.CheckedChanged
If TextBox19.Enabled = False Then
TextBox19.Enabled = True
Else
TextBox19.Enabled = False
TextBox19.Text = ""
End If
End Sub
Private Sub CheckBox20_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox20.CheckedChanged
If TextBox20.Enabled = False Then
TextBox20.Enabled = True
Else
TextBox20.Enabled = False
TextBox20.Text = ""
End If
End Sub
Private Sub CheckBox21_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox21.CheckedChanged
If TextBox21.Enabled = False Then
TextBox21.Enabled = True
Else
TextBox21.Enabled = False
TextBox21.Text = ""
End If
End Sub
Private Sub CheckBox22_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox22.CheckedChanged
If TextBox22.Enabled = False Then
TextBox22.Enabled = True
Else
TextBox22.Enabled = False
TextBox22.Text = ""
End If
End Sub
Private Sub CheckBox23_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox23.CheckedChanged
If TextBox23.Enabled = False Then
TextBox23.Enabled = True
Else
TextBox23.Enabled = False
TextBox23.Text = ""
End If
End Sub
Private Sub CheckBox24_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox24.CheckedChanged
If TextBox24.Enabled = False Then
TextBox24.Enabled = True
Else
TextBox24.Enabled = False
TextBox24.Text = ""
End If
End Sub
Private Sub CheckBox25_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox25.CheckedChanged
If TextBox25.Enabled = False Then
TextBox25.Enabled = True
Else
TextBox25.Enabled = False
TextBox25.Text = ""
End If
End Sub
Private Sub CheckBox26_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox26.CheckedChanged
If TextBox26.Enabled = False Then
TextBox26.Enabled = True
Else
TextBox26.Enabled = False
TextBox26.Text = ""
End If
End Sub
Private Sub CheckBox27_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox27.CheckedChanged
If TextBox27.Enabled = False Then
TextBox27.Enabled = True
Else
TextBox27.Enabled = False
TextBox27.Text = ""
End If
End Sub
Private Sub CheckBox28_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox28.CheckedChanged
If TextBox28.Enabled = False Then
TextBox28.Enabled = True
Else
TextBox28.Enabled = False
TextBox28.Text = ""
End If
End Sub
Private Sub CheckBox29_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox29.CheckedChanged
If TextBox29.Enabled = False Then
TextBox29.Enabled = True
Else
TextBox29.Enabled = False
TextBox29.Text = ""
End If
End Sub
Private Sub CheckBox30_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox30.CheckedChanged
If TextBox30.Enabled = False Then
TextBox30.Enabled = True
Else
TextBox30.Enabled = False
TextBox30.Text = ""
End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Strg1 = Str(QntCalc(p1, Val(TextBox1.Text)))
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
Strg2 = Str(QntCalc(p2, Val(TextBox2.Text)))
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
Strg3 = Str(QntCalc(p3, Val(TextBox3.Text)))
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
Strg4 = Str(QntCalc(p4, Val(TextBox4.Text)))
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
Strg5 = Str(QntCalc(p5, Val(TextBox5.Text)))
End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
Strg6 = Str(QntCalc(p6, Val(TextBox6.Text)))
End Sub
Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
Strg7 = Str(QntCalc(p7, Val(TextBox7.Text)))
End Sub
Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
Strg8 = Str(QntCalc(p8, Val(TextBox8.Text)))
End Sub
Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged
Strg9 = Str(QntCalc(p9, Val(TextBox9.Text)))
End Sub
Private Sub TextBox10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
Strg10 = Str(QntCalc(p10, Val(TextBox10.Text)))
End Sub
Private Sub MedPrc_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MedPrc.TextChanged
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TotMedPrc = Strg1 + Strg2 + Strg3 + Strg4 + Strg5 + Strg6 + Strg7 + Strg8 + Strg9 + Strg10
MedPrc.Text = Val(TotMedPrc)
End Sub
End Class
Is there a way to shorten this?
Your code looks like VB.NET right? In VBA you would write something little bit different. Sample:
Option Explicit
Private Strg1 As String
Private p1 As Double
Private Sub CheckBox1_Change()
OnCheckBoxChange Me.TextBox1, Me.Label1, p1
End Sub
Private Sub TextBox1_Change()
OnTextBoxChanged Me.TextBox1, p1, Strg1
End Sub
Private Sub OnTextBoxChanged(ByRef textBoxControl As Variant, ByRef p As Double, Strg As String)
Strg = Str(QntCalc(p, Val(textBoxControl.Text)))
End Sub
Private Sub OnCheckBoxChange(ByRef textBoxControl As Variant, ByRef labelControl As Variant, ByRef p As Double)
If textBoxControl.Enabled = False Then
textBoxControl.Enabled = True
p = Val(labelControl.Caption)
Else
textBoxControl.Enabled = False
textBoxControl.Text = ""
p = 0
End If
End Sub
Private Function QntCalc(ByVal x As Double, ByVal y As Double) As Double
QntCalc = x * y
End Function
Related
OleDbException was unhandled [Newbie here]
i got error in this code please i need this in school its a group project but im only doing this because they dont know how to code, so i participate to do this program, im a begginer in vb, i hope you understand, Thank you :) Imports System.Data.OleDb Imports System Imports System.Data Imports System.Data.SqlClient Public Class Form9 Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'FoodDataSet.Table1' table. You can move, or remove it, as needed. Table1BindingSource.AddNew() End Sub Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged If RadioButton1.Checked = True Then TextBox1.Text = ("Burger P 12") ElseIf RadioButton1.Checked = False Then TextBox1.Text = ("Choose Your Order!") End If End Sub Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged If RadioButton2.Checked = True Then TextBox1.Text = ("Cheese Burger = P 15") ElseIf RadioButton2.Checked = False Then TextBox1.Text = ("Choose Your Order!") End If End Sub Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged If RadioButton3.Checked = True Then TextBox1.Text = ("Ham Burger = P 15") ElseIf RadioButton3.Checked = False Then TextBox1.Text = ("Choose Your Order!") End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Hide() End Sub Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged If RadioButton4.Checked = True Then TextBox2.Text = ("Potato = P 10") ElseIf RadioButton4.Checked = False Then TextBox2.Text = ("Choose Your Order!") End If End Sub Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged If RadioButton5.Checked = True Then TextBox2.Text = ("Piatos = P 12") ElseIf RadioButton5.Checked = False Then TextBox2.Text = ("Choose Your Order!") End If End Sub Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged If RadioButton6.Checked = True Then TextBox2.Text = ("Potato Fries = P 15") ElseIf RadioButton6.Checked = False Then TextBox2.Text = ("Choose Your Order!") End If End Sub Private Sub RadioButton11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton11.CheckedChanged If RadioButton11.Checked = True Then TextBox3.Text = ("Coke = P 10") ElseIf RadioButton11.Checked = False Then TextBox3.Text = ("Choose Your Order!") End If End Sub Private Sub RadioButton8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton8.CheckedChanged If RadioButton8.Checked = True Then TextBox3.Text = ("Sprite = P 10") ElseIf RadioButton8.Checked = False Then TextBox3.Text = ("Choose Your Order!") End If End Sub Private Sub RadioButton9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton9.CheckedChanged If RadioButton9.Checked = True Then TextBox3.Text = ("Royal = P 10") ElseIf RadioButton9.Checked = False Then TextBox3.Text = ("Choose Your Order!") End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim con As OleDbConnection Table1BindingSource.AddNew() MessageBox.Show("Your Orders in ongoing,Kindly Prepare your money and wait your order, Thank you!") Me.Validate() Me.Table1BindingSource.EndEdit() con = New OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;Data Source=C:\Documents and Settings\WinXP\Desktop\LoadTest HP\WindowsApplication1\Food.mdb") Me.Table1TableAdapter.Update(Me.FoodDataSet.Table1) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Table1BindingSource.RemoveCurrent() End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Close() End Sub Private Sub RadioButton7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton7.CheckedChanged End Sub Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick End Sub Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Form10.Show() Me.Close() End Sub End Class
It means you have a possible exception that wasn't handled you should handle the exception which will even show you a error which can further help you figure out what's wrong with your code :) How do you handle exceptions? wrap code which uses OleDB with try/catch anywhere you use OleDb you should try/catch it.. remove the try/catch later when you know your code works 100%. like so.. Try con = New OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;Data Source=C:\Documents and Settings\WinXP\Desktop\LoadTest HP\WindowsApplication1\Food.mdb") Catch ex as OleDbException Msgbox ex.ToString() End Try I would wrap where you declare the OleDbConnection as well which is not necessary but it gets all the OleDb stuff in a try/catch Try Dim con As OleDbConnection Table1BindingSource.AddNew() MessageBox.Show("Your Orders in ongoing,Kindly Prepare your money and wait your order, Thank you!") Me.Validate() Me.Table1BindingSource.EndEdit() con = New OleDbConnection("Provider=Microsoft.Ace.Oledb.12.0;Data Source=C:\Documents and Settings\WinXP\Desktop\LoadTest HP\WindowsApplication1\Food.mdb") Catch ex as OleDbException Msgbox ex.ToString() End Try
VB.Net Move an object[Error]
i just start create a project with this(To The Topic) an error is start in here(maybe) First Button Code Private Sub DeimosButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeimosButton2.Click State(i) DeimosButton3.Visible = False DeimosButton4.Visible = False End Sub Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick DeimosButton2.Top = DeimosButton2.Top - 1 If DeimosButton2.Location = New Point(22, 54) Then Timer4.Stop() DeimosButton2.Enabled = True End If End Sub Private Sub State(ByVal Ref As Integer) If Ref = 0 Then Timer4.Start() DeimosButton2.Enabled = False i = 1 Else Timer5.Start() DeimosButton2.Enabled = False i = 0 End If End Sub Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick DeimosButton2.Top = DeimosButton2.Top + 1 If DeimosButton2.Location = New Point(22, 85) Then Timer5.Stop() DeimosButton2.Enabled = True DeimosButton3.Visible = True DeimosButton4.Visible = True End If End Sub This Second Button Code Private Sub DeimosButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeimosButton3.Click states(o) DeimosButton2.Visible = False DeimosButton4.Visible = False End Sub Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick DeimosButton3.Top = DeimosButton3.Top - 1 If DeimosButton3.Location = New Point(22, 54) Then Timer6.Stop() DeimosButton3.Enabled = True End If End Sub Private Sub states(ByVal Def As Integer) If Def = 0 Then Timer6.Start() DeimosButton3.Enabled = False o = 1 Else Timer7.Start() DeimosButton3.Enabled = False o = 0 End If End Sub Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick DeimosButton3.Top = DeimosButton3.Top + 1 If DeimosButton3.Location = New Point(22, 116) Then Timer7.Stop() DeimosButton3.Enabled = True DeimosButton2.Visible = True DeimosButton4.Visible = True End If End Sub The problem is when i click the second button but when i click the first button, is no matter. Problem : The button is still moving to top, although it is has reach the position. there is a solution for this ?
You must change DeimosButton3.Location = New Point(22, 116) to DeimosButton3.Location.y = 116 This is valid to all for all your code
Get highest value from scales and remove unnecessary words
I created a little program to get weight from scales via COM port. But then I turn on program. It's start to loop (creating long list) SV 0 KG in Label. And if weight increase it's also loop, for e.g. SV 0 KG, SV 50 KG, SV 48 KG, SV 0 KG and etc.. So my question is how to stop loop and always show one value? And second question can I use Replace("SV", " ") or there is other better method? Dim myPort As Array Delegate Sub SetTextCallback(ByVal [text] As String) Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load myPort = IO.Ports.SerialPort.GetPortNames() cmbBaud.Items.Add(9600) cmbBaud.Items.Add(19200) cmbBaud.Items.Add(38400) cmbBaud.Items.Add(57600) cmbBaud.Items.Add(115200) For i = 0 To UBound(myPort) cmbPort.Items.Add(myPort(i)) Next cmbPort.Text = cmbPort.Items.Item(0) cmbBaud.Text = cmbBaud.Items.Item(0) btnDisconnect.Enabled = False End Sub Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click SerialPort1.PortName = cmbPort.Text SerialPort1.BaudRate = cmbBaud.Text SerialPort1.Parity = IO.Ports.Parity.None SerialPort1.StopBits = IO.Ports.StopBits.One SerialPort1.DataBits = 8 SerialPort1.Open() btnConnect.Enabled = False btnDisconnect.Enabled = True End Sub Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click SerialPort1.Close() btnConnect.Enabled = True btnDisconnect.Enabled = False End Sub Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click SerialPort1.Write(txtTransmit.Text & vbCr) End Sub Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived ReceivedText(SerialPort1.ReadExisting()) End Sub Private Sub ReceivedText(ByVal [text] As String) If Me.rtbReceived.InvokeRequired Then Dim x As New SetTextCallback(AddressOf ReceivedText) Me.Invoke(x, New Object() {(text)}) Else Me.rtbReceived.Text &= [text] End If End Sub Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.PortName = cmbPort.Text Else MsgBox("Valid only if port is Closed", vbCritical) End If End Sub Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged If SerialPort1.IsOpen = False Then SerialPort1.BaudRate = cmbBaud.Text Else MsgBox("Valid only if port is Closed", vbCritical) End If End Sub Private Sub rtbReceived_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbReceived.TextChanged End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click rtbReceived.Text = " " End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick SerialPort1.Write(txtTransmit.Text & vbCr) End Sub
String cannot be converted to System.Uri
Codeļ¼ Imports System Imports System.IO Imports System.Text Public Class Browser Dim Tab1Url As String = "" Dim Tab2Url As String = "" Dim TabNumber As Integer = 1 Dim DefaultHomePageSavedLocation As String = "C:\ToolBoxData\TinyBrowser\Home.txt" Private Sub WebBrowser1_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating Label2.Text = "[BROWSER] Loading in Progress......" ProgressBar1.Value = 50 End Sub Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click WebBrowser1.GoBack() Label2.Text = "[BROWSER] Going to Last History......" ProgressBar1.Value = 0 End Sub Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click 'Next WebBrowser1.GoForward() Label2.Text = "[BROWSER] Going to Next History......" ProgressBar1.Value = 0 End Sub Private Sub HomeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HomeToolStripMenuItem.Click 'Home Dim HomeLink As String = My.Computer.FileSystem.ReadAllText(DefaultHomePageSavedLocation) WebBrowser1.Navigate(HomeLink) Label2.Text = "[BROWSER] Going Home......" ProgressBar1.Value = 0 End Sub Private Sub ToolStripTextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripTextBox1.Click Label2.Text = "[USER] Inserting / Copying URL......" End Sub Private Sub GoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoToolStripMenuItem.Click If TabNumber = 1 Then Tab1Url = ToolStripTextBox1.Text WebBrowser1.Navigate(Tab1Url) Button1.Text = ToolStripTextBox1.Text ElseIf TabNumber = 2 Then Tab2Url = ToolStripTextBox1.Text WebBrowser1.Navigate(Tab2Url) Button2.Text = ToolStripTextBox1.Text End If ProgressBar1.Value = 0 End Sub Private Sub ReloadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReloadToolStripMenuItem.Click WebBrowser1.Refresh() Label2.Text = "[BROWSER] Refreshing......" ProgressBar1.Value = 0 End Sub Private Sub TinyBrowserV10ByToolBoxWeeblyComToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TinyBrowserV10ByToolBoxWeeblyComToolStripMenuItem.Click WebBrowser1.Navigate("http://tool-box.weebly.com/") Label2.Text = "[BROWSER] Redirecting to Our Website......" ProgressBar1.Value = 0 End Sub Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted Label2.Text = "[BROWSER] Loading Completed." ToolStripTextBox1.Text = WebBrowser1.Url.ToString ProgressBar1.Value = 100 End Sub Private Sub ToolStripTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ToolStripTextBox1.KeyPress If e.KeyChar = Chr(13) Then If TabNumber = 1 Then Tab1Url = ToolStripTextBox1.Text WebBrowser1.Navigate(Tab1Url) Button1.Text = ToolStripTextBox1.Text ElseIf TabNumber = 2 Then Tab2Url = ToolStripTextBox1.Text WebBrowser1.Navigate(Tab2Url) Button2.Text = ToolStripTextBox1.Text End If ProgressBar1.Value = 0 End If End Sub Private Sub Browser_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress If e.KeyChar = Convert.ToChar(Keys.Back) Then WebBrowser1.GoBack() ProgressBar1.Value = 0 End If End Sub Private Sub OptionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionToolStripMenuItem.Click Setting.Show() End Sub Private Sub Browser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If My.Computer.FileSystem.FileExists(DefaultHomePageSavedLocation) = False Then My.Computer.FileSystem.CreateDirectory("C:\ToolBoxData\TinyBrowser\") My.Computer.FileSystem.WriteAllText(DefaultHomePageSavedLocation, "http://hk.yahoo.com", True) End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Not TabNumber = 1 Then TabNumber = 1 ChangeTab() End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If Not TabNumber = 2 Then TabNumber = 2 ChangeTab() End If End Sub Private Sub ChangeTab() If TabNumber = 1 Then WebBrowser1.Url = Tab1Url ElseIf TabNumber = 2 Then WebBrowser1.Url = Tab2Url End If End Sub End Class And the error is: Value of type "String" cannot be converted to "System.Uri" (Line 121, 123) What's the problem?
I've found the solution at another website. Solution: Line 121: WebBrowser1.Url = New Uri(Tab1Url) Line 123: WebBrowser1.Url = New Uri(Tab2Url) Thanks for all your help!
Multiple camera shots in vb
See I have this simple vb.net codes that counts from 5 to 1 then says capture! I need to do this 4 consecutive times after the start button is clicked .. I tried a do until loop but it didn't work, I'm a newbie here so please help.. Public Class Form_welcome Dim Count As Integer Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Count -= 1 Label2.Text = Count If (Count = 0) Then Timer1.Enabled = False Label2.Hide() Label3.Show() End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Count = 5 Timer1.Enabled = True Label2.Text = Count Timer1.Interval = 1000 End Sub End Class
something like this? Public Class Form_welcome Dim Count As Integer Dim pictureCount as integer = 4 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Count -= 1 Label2.Text = Count If (Count = 0) Then pictureCount -=1 If pictureCount = 0 then Timer1.Enabled = False End If 'take a picture Label2.Hide() Label3.Show() Count = 5 End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Count = 5 Timer1.Enabled = True Label2.Text = Count Timer1.Interval = 1000 End Sub End Class
Public Class Form_welcome Dim Count As Integer Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Admin_Login.Show() Me.Hide() End Sub Private Sub Form_welcome_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing Application.Exit() End Sub Private Sub Form_welcome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Button4.Hide() Button5.Hide() Button6.Hide() 'Timer1.Enabled = True 'Label2.Text = Count 'Timer1.Interval = 1000 End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Count -= 1 'Label2.Text = Count If (Count = -1) Then Timer1.Enabled = False Label3.Show() Label3.Hide() Label2.Hide() Button4.Show() Button5.Show() Button6.Show() PictureBox4.Hide() PictureBox3.Show() pict1.Show() pict2.Show() pict3.Show() pict4.Show() Button2.Hide() End If If (Count Mod 3) = 0 Then Label2.Text = "Captured!" Else Label2.Text = Count End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Count = 12 Timer1.Enabled = True Label2.Text = Count Label2.Show() Timer1.Interval = 1000 End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Application.Exit() End Sub Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click Button2.Show() Button4.Hide() Button5.Hide() Button6.Hide() PictureBox3.Hide() pict1.Hide() pict2.Hide() pict3.Hide() pict4.Hide() PictureBox4.Show() End Sub End Class