I am using a DataSource in my form app, and it was working fine for calls made across the board, Fill, Add, Delete, etc... It suddenly stopped working. I get no errors on build, no data is added to any ComboBoxes, and no new Adds work either.
I created a new DataSource from the same database that works fine with the exact same connection. The location of the database never moved, no changes were made to any properties of the DataSource or any of the Adapters assigned to the source, it just stopped working. Here is some screen shots and code of my form.
I tried to do a Code Compare but since there are a bunch of Adapters assigned to the source I can't find any anomalies. What would kill a data connection so that the code still sees the connection, but nothing gets filled?
The following code no longer works, no Fill or Add to DCGDataSet;
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs)
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
'comboClear()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As System.EventArgs)
' Add new Job to the database
Dim newJobRow As New DCGDataSetTableAdapters.MainTableAdapter
Dim intInsert As Integer
Dim jobText = txtBoxAddNewJob.Text
intInsert = newJobRow.InsertJob(jobText)
If intInsert = 1 Then
MessageBox.Show("New Job Added")
' Update the comboBox values
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
txtBoxAddNewJob.Text = ""
clearTabOne()
Else
MessageBox.Show("Job Not Added")
End If
End Sub
The following call for a ComboBox works fine, this is the new DataSource;
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter1.Fill(Me.DCGDataSet1.Main)
End Sub
These pics are of the two DS I am working with, the top one is the non-working DS.
Entire .vb of Form1 Code;
Public Class MainForm
Dim strCurrency As String = ""
Dim acceptableKey As Boolean = False
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
comboClear()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As System.EventArgs)
' Add new Job to the database
Dim newJobRow As New DCGDataSetTableAdapters.MainTableAdapter
Dim intInsert As Integer
Dim jobText = txtBoxAddNewJob.Text
intInsert = newJobRow.InsertJob(jobText)
If intInsert = 1 Then
MessageBox.Show("New Job Added")
' Update the comboBox values
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
txtBoxAddNewJob.Text = ""
clearTabOne()
Else
MessageBox.Show("Job Not Added")
End If
End Sub
Private Sub TabPage2_Enter(sender As Object, e As System.EventArgs)
Me.ActiveControl = txtBoxAddNewJob
clearTabOne()
End Sub
Public Sub comboClear()
ComboBox1.SelectedIndex = -1
ComboBox2.SelectedIndex = -1
End Sub
Private Sub TabPage1_Enter(sender As Object, e As System.EventArgs)
'comboClear()
ComboBox1.SelectedIndex = -1
'ComboBox1.SelectedText = ""
End Sub
Private Sub TabPage3_Enter(sender As Object, e As System.EventArgs)
'comboClear()
ComboBox2.SelectedIndex = -1
clearTabOne()
End Sub
Private Sub btnDeleteJob_Click(sender As Object, e As System.EventArgs)
Dim delJobID = ComboBox2.SelectedValue
Dim delJobRowAdpt As New DCGDataSetTableAdapters.MainTableAdapter
Dim delJobRow As DCGDataSet.MainRow
Dim intDelete As Integer
delJobRow = DCGDataSet.Main.FindByID(delJobID)
delJobRow.Delete()
intDelete = delJobRowAdpt.Update(DCGDataSet.Main)
If intDelete = 1 Then
MessageBox.Show("Job Deleted")
'comboClear()
clearTabOne()
'ComboBox2.SelectedValue = -1
Me.MainTableAdapter.Fill(Me.DCGDataSet.Main)
Else
MessageBox.Show("Job Failed to Delete")
End If
ComboBox2.SelectedValue = -1
End Sub
Private Sub FillSubCombo(ByVal subJob As String)
Dim selSubRow = DCGDataSet.SubBilling
Dim selSubValue As New DCGDataSetTableAdapters.SubBillingTableAdapter
Me.SubBillingTableAdapter.SubName(Me.DCGDataSet.SubBilling, subJob)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs)
Dim subJob As String = ComboBox1.Text
If subJob.Length > 1 Then
Label5.Visible = True
ComboBox3.Visible = True
FillSubCombo(subJob)
End If
End Sub
Public Sub clearTabOne()
Label5.Visible = False
ComboBox3.Visible = False
End Sub
Private Sub TabPage4_Enter(sender As Object, e As System.EventArgs)
clearTabOne()
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) OrElse (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse e.KeyCode = Keys.Back Then
acceptableKey = True
Else
acceptableKey = False
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
' Check for the flag being set in the KeyDown event.
If acceptableKey = False Then
' Stop the character from being entered into the control since it is non-numerical.
e.Handled = True
Return
Else
If e.KeyChar = Convert.ToChar(Keys.Back) Then
If strCurrency.Length > 0 Then
strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
End If
Else
strCurrency = strCurrency & e.KeyChar
End If
If strCurrency.Length = 0 Then
TextBox1.Text = ""
ElseIf strCurrency.Length = 1 Then
TextBox1.Text = "0.0" & strCurrency
ElseIf strCurrency.Length = 2 Then
TextBox1.Text = "0." & strCurrency
ElseIf strCurrency.Length > 2 Then
TextBox1.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
End If
TextBox1.Select(TextBox1.Text.Length, 0)
End If
e.Handled = True
End Sub
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DCGDataSet1.Main' table. You can move, or remove it, as needed.
Me.MainTableAdapter1.Fill(Me.DCGDataSet1.Main)
End Sub
End Class
Your not-working code is missing the Handles clauses on Form1_Load and btnAddNew_Click. If you don't connect the event handler to the event (using either a Handles clause or an AddHandler statement), the event handler won't be run.
Related
I have a code for seat reservation but i don't know how to link it with access database. I am using buttons as seats so when a seat is selected it hides itself so i want help when the seat is selected the seat number shows in access database .Here is my code :
Public Class Form1
Dim seatnumber As char
Private Sub BTNA1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA1.Click
seatnumber = "A1"
Confirmseat()
End Sub
Private Sub BTNA2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA2.Click
seatnumber = "A2"
confirmseat()
End Sub
Private Sub BTNA3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA3.Click
seatnumber = "A3"
Confirmseat()
End Sub
Private Sub BTNA4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA4.Click
seatnumber = "A4"
Confirmseat()
End Sub
Private Sub BTNA5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA5.Click
seatnumber = "A5"
Confirmseat()
End Sub
Private Sub BTNA6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNA6.Click
seatnumber = "A6"
Confirmseat()
End Sub
Private Sub BTNB7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNB7.Click
seatnumber = "B7"
confirmseat()
End Sub
Private Sub BTNB8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNB8.Click
seatnumber = "B8"
confirmseat()
End Sub
Private Sub BTNB9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNB9.Click
seatnumber = "B9"
confirmseat()
End Sub
Private Sub BTNB10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnb10.Click
seatnumber = "B10"
confirmseat()
End Sub
Private Sub BTNB11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnb11.Click
seatnumber = "B11"
confirmseat()
End Sub
Private Sub BTNB12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnb12.Click
seatnumber = "B12"
confirmseat()
End Sub
Public Sub confirmseat()
Dim intresult As Integer
intresult = MessageBox.Show("you selected" & seatnumber, "CONFIRM", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If intresult = Windows.Forms.DialogResult.No Then
seatnumber = "NO"
Else
Select Case (seatnumber)
Case "A1"
BTNA1.Visible = False
Case "A2"
BTNA2.Visible = False
Case "A3"
BTNA3.Visible = False
Case "A4"
BTNA4.Visible = False
Case "A5"
BTNA5.Visible = False
Case "A6"
BTNA6.Visible = False
Case "B7"
BTNB7.Visible = False
Case "B8"
BTNB8.Visible = False
Case "B9"
BTNB9.Visible = False
Case "B10"
btnb10.Visible = False
Case "B11"
btnb11.Visible = False
Case "B12"
btnb12.Visible = False
End Select
MessageBox.Show("Seat" & seatnumber & "is confirmed", "confirmation")
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim seatavailability As Integer
If seatavailability <> "12" Then
BTNA1.Visible = True
BTNA2.Visible = True
BTNA3.Visible = True
BTNA4.Visible = True
BTNA5.Visible = True
BTNA6.Visible = True
BTNB7.Visible = True
BTNB8.Visible = True
BTNB9.Visible = True
btnb10.Visible = True
btnb11.Visible = True
btnb12.Visible = True
Dim i As Integer
Dim reservedseats(1) As Char
For i = 0 To 12
Select Case (reservedseats(1))
Case "BTNA1"
BTNA1.Visible = False
Case "BTNA2"
BTNA2.Visible = False
Case "BTNA3"
BTNA3.Visible = False
Case "BTNA4"
BTNA4.Visible = False
Case "BTNA5"
BTNA5.Visible = False
Case "BTNA6"
BTNA6.Visible = False
Case "BTNB7"
BTNB7.Visible = False
Case "BTNB8"
BTNB8.Visible = False
Case "BTNB9"
BTNB9.Visible = False
Case "BTNB10"
btnb10.Visible = False
Case "BTNB11"
btnb11.Visible = False
Case "BTNB12"
btnb12.Visible = False
End Select
Next
End If
End Sub
Private Sub btncontinue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncontinue.Click
Me.Close()
End Sub
End Class
Note that you have a LOT of duplicate code. You could simplify your code by writing a single button click handler that forwards the button to ConfirmSeat:
Private Sub ClickHandler(sender As Object, e As EventArgs)
Dim seatNumber = Mid(DirectCast(sender, Button).Name, 4)
ConfirmSeat(seatNumber)
End Sub
and write ConfirmSeat as follows:
Private Sub ConfirmSeat(seatNumber As String)
Dim result As Integer
result = MessageBox.Show("you selected" & seatnumber, "CONFIRM", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If intresult = Windows.Forms.DialogResult.No Then
MessageBox.Show("No seat confirmed")
Exit Sub
End If
Dim btn As Button = Controls("btn" & seatNumber)
btn.Visible = False
MessageBox.Show($"Seat {seatNumber} is confirmed", "confirmation")
End Sub
I'm not quite sure what you are trying to do in the Form1_Load, as none of the Select Case will ever be hit. In any event, the handler could be attached to the Click event of the buttons as follows:
For Each ctrl As Control In Controls
If Not TypeOf ctrl Is Button Then Continue
If ctrl Is btnClick Then Continue
AddHandler DirectCast(ctrl, Button).Click, ClickHandler
Next
With that out of the way, we can talk about reading and writing from / to an Access database in your program, using ADO.NET.
Connections and Execute* methods
At the lowest level, you use a connection string:
Dim connectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" &
"Data Source=" & pathToDatabase
to connect to the database with an OleDbConnection:
Using connection AS New OleDbConnection(connectionString)
connection.Open
' do stuff here
End Using
With an open connection, you can execute commands using an OleDbCommand. You can use commands together with the ExecuteNonQuery method to perform update queries:
Dim cmdDeleteAll As New OleDbCommand("DELETE * FROM Persons", connection)
cmdDeleteAll.ExecuteNonQuery
or together with the ExecuteScalar method, to return a single result:
Dim cmdCount As New OleDbCommand("SELECT COUNT(*) FROM Persons", connection)
Dim count As Integer? = cmdCount.ExecuteScalar
or using the ExecuteReader method, return an OleDbDataReader, to iterate once over a set of results -- forward-only.
Using cmd As New OleDbCommand("SELECT * FROM Persons", connection),
rdr = cmd.ExecuteReader
Do While rdr.Read
MessageBox.Show($"{rdr["FirstName"]} {rdr["LastName"]}")
Loop
End Using
Adapters and DataSets
An OleDbDataReader has access to one record at a tine, from a single result set. If you want to load the data from multiple result sets, or you need to keep all the data in memory, you can use the OleDbDataAdapter to load data into a DataSet from an OleDbConnection object:
Dim ds = new DataSet();
Using connection As New OleDbConnection(connectionString)
Dim sql = "SELECT * FROM Persons"
Dim adapter = new OleDbDataAdapter(sql, connection)
adapter.Fill(ds, "Persons")
End Using
Since a DataSet can contain multiple sets of data, there is a hierarchy of objects for working with the DataSets and its DataTables:
(Source: ADO.NET Architecture)
LINQ against a DataReader
You can also use LINQ and LINQ methods on a data reader, to retrieve a set of objects from a single result set:
Dim persons As List(Of Person)
Using reader = cmd.ExecuteReader()
persons = reader.Cast<DbDataRecord>()
.Select(Function(row) New Person With {
.LastName = row["LastName"])
.FirstName = row["FirstName"]
}).ToList
End Using
So I'm very new to coding and especially Visual Basic, and after fidgeting around for a bit with open source code I only seem to have one recurring problem in my current Form1.vb file; Handles clause requires a WithEvents variable defined in the containing type or one of its base type. This is what is looks like:
Public Class Form1
Dim Firstnum As Decimal
Dim Secondnum As Decimal
Dim Operations As Integer
Dim Operator_selector As Boolean = False
Private lblHold As Object
Public Property TextBox1 As Object
WithEvents Btn1_click As Button
Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "1"
Else
TextBox1.Text = "1"
End If
End Sub
Private Sub Btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "2"
Else
TextBox1.Text = "2"
End If
End Sub
Private Sub Btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "3"
Else
TextBox1.Text = "3"
End If
End Sub
Private Sub Btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "4"
Else
TextBox1.Text = "4"
End If
End Sub
Private Sub Btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "5"
Else
TextBox1.Text = "5"
End If
End Sub
Private Sub Btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "6"
Else
TextBox1.Text = "6"
End If
End Sub
Private Sub Btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "7"
Else
TextBox1.Text = "7"
End If
End Sub
Private Sub Btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "8"
Else
TextBox1.Text = "8"
End If
End Sub
Private Sub Btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "9"
Else
TextBox1.Text = "9"
End If
End Sub
Private Sub Btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "0"
End If
End Sub
Private Sub BtnPoint_Click(sender As Object, e As EventArgs) Handles btnPoint.Click
If Not (TextBox1.Text.Contains(".")) Then
TextBox1.Text += "."
End If
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
TextBox1.Text = "0"
End Sub
Private Sub BtnPlus_Click(sender As Object, e As EventArgs) Handles btnPlus.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 1 'For addition
lblHold.Text = Firstnum.ToString + "+"
End Sub
Private Sub BtnMinus_Click(sender As Object, e As EventArgs) Handles btnMinus.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 2 'For subtraction
lblHold.Text = Firstnum.ToString + "-"
End Sub
Private Sub BtnMult_Click(sender As Object, e As EventArgs) Handles btnMult.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 3 'For multiplication
lblHold.Text = Firstnum.ToString + "*"
End Sub
Private Sub BtnDiv_Click(sender As Object, e As EventArgs) Handles btnDiv.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 4 'For division
lblHold.Text = Firstnum.ToString + "+"
End Sub
Private Sub BtnEqual_Click(sender As Object, e As EventArgs) Handles btnEqual.Click
If Operator_selector = True Then
Secondnum = TextBox1.Text
If Operations = 1 Then
TextBox1.Text = Firstnum + Secondnum
ElseIf Operations = 2 Then
TextBox1.Text = Firstnum - Secondnum
ElseIf Operations = 3 Then
TextBox1.Text = Firstnum * Secondnum
Else
If Secondnum = 0 Then
TextBox1.Text = "Error!"
Else
TextBox1.Text = Firstnum / Secondnum
End If
Operator_selector = False
End If
lblHold.Text = ""
End If
End Sub
End Class
Now I'm wondering what I have to do to get it right and make this error disappear. I tried using something around the lines of "WithEvents Btn1_click As Button" but it doesn't do anything. It also says "Btn1_click is already declared as 'Private WithEvents Btn1_click As Button' in this class." I'm truly a noob, so please don't throw hardcore coding terms at me but just the simple stuff :P
Many thanks in advance!
The Handles clause you see on those methods indicates that that method will be executed when the specified event of the object currently assigned to the specified variable is raised. For example:
Private Sub Btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
means that the Btn2_Click method will be executed when the object assigned to the btn2 variable raises its Click event. The error message flags this line:
Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
and it's telling you that you have no btn1 variable declared WithEvents.
When you add a control to your form in the designer, it automatically adds a variable with the specified name and declares it WithEvents. You can see that variable if you open the designer code file, which you can access via the Solution Explorer if you click the Show All Files button first. You can declare your own variables WithEvents but this:
WithEvents Btn1_click As Button
is not using the correct name for a start and it is also useless because it doesn;t have anything assigned to it. You'd need to create a Button object, assign it to the variable and add it to the form, which you haven't done.
The solution would be to simply add a Button to the form with that name.
I'm, trying to navigate through my records but when I press the buttons, nothing happens. I'm using Jet 4.0 and an Access DB.
I'm not sure what I'm doing wrong but if anyone can help me in the right direction, it would be appreciated.
Code:
Private Sub showData(ByVal CurrentRow)
CurrentRow = 0
Dad.Fill(dst, "patrolDB")
TextBox27.Text = dst.Tables("patrolDB").Rows(CurrentRow)("ID").ToString.ToUpper
DateTimePicker1.Text = dst.Tables("patrolDB").Rows(CurrentRow)("patroldate").ToString.ToUpper
TextBox2.Text = dst.Tables("patrolDB").Rows(CurrentRow)("patroltime").ToString.ToUpper
ComboBox1.Text = dst.Tables("patrolDB").Rows(CurrentRow)("patroltype").ToString.ToUpper
ComboBox2.Text = dst.Tables("patrolDB").Rows(CurrentRow)("patrolsite").ToString.ToUpper
ComboBox4.Text = dst.Tables("patrolDB").Rows(CurrentRow)("patrolloc").ToString.ToUpper
ComboBox3.Text = dst.Tables("patrolDB").Rows(CurrentRow)("patrolofficer").ToString.ToUpper
RichTextBox1.Text = dst.Tables("patrolDB").Rows(CurrentRow)("patrolnotes").ToString.ToUpper
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If CurrentRow <> dst.Tables("patrolDB").Rows.Count - 1 Then
CurrentRow += 1
showData(CurrentRow)
End If
MsgBox("You've reached the last record.")
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
index = 0
showData(CurrentRow)
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If CurrentRow <> 0 Then
CurrentRow -= 1
showData(CurrentRow)
End If
MsgBox("You've reached the first record.")
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
CurrentRow = dst.Tables("patrolDB").Rows.Count - 1
showData(CurrentRow)
End Sub
Dim table As DataTable
Private Sub LoadData()
CurrentRow = 0
Dad.Fill(dst, "patrolDB")
table = dst.Tables("patrolDB") 'for the comfort
ShowCurrentRow()
End Sub
Private Sub ShowCurrentRow()
TextBox27.Text = table.Rows(CurrentRow)("ID").ToString.ToUpper
DateTimePicker1.Text = table.Rows(CurrentRow)("patroldate").ToString.ToUpper
TextBox2.Text = table.Rows(CurrentRow)("patroltime").ToString.ToUpper
ComboBox1.Text = table.Rows(CurrentRow)("patroltype").ToString.ToUpper
ComboBox2.Text = table.Rows(CurrentRow)("patrolsite").ToString.ToUpper
ComboBox4.Text = table.Rows(CurrentRow)("patrolloc").ToString.ToUpper
ComboBox3.Text = table.Rows(CurrentRow)("patrolofficer").ToString.ToUpper
RichTextBox1.Text = table.Rows(CurrentRow)("patrolnotes").ToString.ToUpper
End Sub
'go to next
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If CurrentRow <> table.Rows.Count - 1 Then
CurrentRow += 1
ShowCurrentRow()
Else MsgBox("You've reached the last record.")
End If
End Sub
'go to prev
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If CurrentRow <> 0 Then
CurrentRow -= 1
ShowCurrentRow()
Else MsgBox("You've reached the first record.")
End If
End Sub
'go to start
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
index = 0
ShowCurrentRow()
End Sub
'go to last
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
CurrentRow = table.Rows.Count - 1
ShowCurrentRow()
End Sub
I am writing a calculator WinForm in VB. I have a label used to display the output "Result". My problem comes when trying to add a "." to my label string. Example: I will type 355.5 and until the 5 is pressed after it, my string is showing up as .355 After the last 5 is pressed, it jumps into the correct location. I have exhausted my debugging skill and now am just going crazy. Anyone encounter this before?
Here's my entire code so far (ignore unfinished functions)
Public Class MyCalc
Private bDecFlag As Boolean
Private sMathOp As String
Private Sub ModeSel_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ModeSel.SelectedIndexChanged
If ModeSel.SelectedIndex = 3 Then
Me.Width = 360
ElseIf ModeSel.SelectedIndex > 2 Then
Me.Width = 590
Else
Me.Width = 250
End If
If ModeSel.SelectedIndex = 0 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
End If
If ModeSel.SelectedIndex = 1 Then
For iCount As Integer = 2 To 9
Me.Controls("Digit" & iCount).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 2 Then
For iCount As Integer = 0 To 7
Me.Controls("Digit" & iCount).Enabled = True
Next
Digit8.Enabled = False
Digit9.Enabled = False
End If
If ModeSel.SelectedIndex = 3 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("F")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
For iCount As Integer = Asc("G") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 4 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
AlphaA.Enabled = False
AlphaE.Enabled = False
AlphaI.Enabled = False
AlphaO.Enabled = False
AlphaU.Enabled = False
End If
End Sub
Private Sub Digit0_Click(sender As System.Object, e As System.EventArgs) Handles Digit0.Click
UpdateResult(0)
End Sub
Private Sub Digit1_Click(sender As System.Object, e As System.EventArgs) Handles Digit1.Click
UpdateResult(1)
End Sub
Private Sub Digit2_Click(sender As System.Object, e As System.EventArgs) Handles Digit2.Click
UpdateResult(2)
End Sub
Private Sub Digit3_Click(sender As System.Object, e As System.EventArgs) Handles Digit3.Click
UpdateResult(3)
End Sub
Private Sub Digit4_Click(sender As System.Object, e As System.EventArgs) Handles Digit4.Click
UpdateResult(4)
End Sub
Private Sub Digit5_Click(sender As System.Object, e As System.EventArgs) Handles Digit5.Click
UpdateResult(5)
End Sub
Private Sub Digit6_Click(sender As System.Object, e As System.EventArgs) Handles Digit6.Click
UpdateResult(6)
End Sub
Private Sub Digit7_Click(sender As System.Object, e As System.EventArgs) Handles Digit7.Click
UpdateResult(7)
End Sub
Private Sub Digit8_Click(sender As System.Object, e As System.EventArgs) Handles Digit8.Click
UpdateResult(8)
End Sub
Private Sub Digit9_Click(sender As System.Object, e As System.EventArgs) Handles Digit9.Click
UpdateResult(9)
End Sub
Private Sub DecBut_Click(sender As System.Object, e As System.EventArgs) Handles DecBut.Click
If bDecFlag = False Then
If Result.Text = "0" Then
Result.Text = "0."
bDecFlag = True
Else
Result.Text = Result.Text + "."
bDecFlag = True
End If
End If
End Sub
Private Sub ClrBut_Click(sender As System.Object, e As System.EventArgs) Handles ClrBut.Click
Result.Text = 0
bDecFlag = False
End Sub
Private Sub DelBut_Click(sender As System.Object, e As System.EventArgs) Handles DelBut.Click
If Result.Text = "0" Then
ElseIf Result.Text.Substring(Result.Text.Length - 1) = "." Then
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
bDecFlag = False
Else
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
If Result.Text = "" Then
Result.Text = "0"
End If
End If
End Sub
Private Sub MyCalc_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad0
Digit0_Click(Digit0, New EventArgs)
Case Keys.NumPad1
Digit1_Click(Digit1, New EventArgs)
Case Keys.NumPad2
Digit2_Click(Digit2, New EventArgs)
Case Keys.NumPad3
Digit3_Click(Digit3, New EventArgs)
Case Keys.NumPad4
Digit4_Click(Digit4, New EventArgs)
Case Keys.NumPad5
Digit5_Click(Digit5, New EventArgs)
Case Keys.NumPad6
Digit6_Click(Digit6, New EventArgs)
Case Keys.NumPad7
Digit7_Click(Digit7, New EventArgs)
Case Keys.NumPad8
Digit8_Click(Digit8, New EventArgs)
Case Keys.NumPad9
Digit9_Click(Digit9, New EventArgs)
Case Keys.Back
DelBut_Click(DelBut, New EventArgs)
Case Keys.Decimal
DecBut_Click(DecBut, New EventArgs)
End Select
End Sub
Private Sub MyCalc_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bDecFlag = False
End Sub
Public Sub UpdateResult(ByVal sNum As String)
If Result.Text = "0" Then
Result.Text = sNum
Else
Result.Text &= sNum
End If
End Sub
End Class
So after the long debugging, I found out that you are using the Label/Textbox RightToLeft property set to yes. Nothing is wrong with it really, but it can't handle special characters including decimal points, commas, etc. properly. The workaround for this is to reverse what we have expected - to reverse the string concatenation.
Result.Text = "." & Result.Text
Not seeing entire code I'm not exactly sure but I think there might be something wrong with string concatenation.
Try changing into this:
Result.Text = Result.Text & "."
I have a form with over 10 textboxes and 1 button, I would like to disable the button with a realtime validation until all textboxes filled with a 10 or a 13 length numeric value, my code is the following so far:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
For Each userID As Control In Me.Controls.OfType(Of TextBox)()
AddHandler userID.TextChanged, AddressOf ValidateAllFields
Next userID
End Sub
Private Sub userID_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar <> ChrW(Keys.Back) Then
If Char.IsNumber(e.KeyChar) Then
Else
e.Handled = True
End If
End If
End Sub
Private Function ValidateAllFields()
Dim Validation As Boolean = True
For Each userID As Control In Me.Controls.OfType(Of TextBox)()
Dim e As New System.ComponentModel.CancelEventArgs
e.Cancel = False
Call userID_Validating(userID, e)
If e.Cancel = True Then Validation = False
Next userID
buttonSave.Enabled = Not Me.Controls.OfType(Of TextBox).Any(Function(userID) userID.Text.Length <> 10 AndAlso userID.Text.Length <> 13)
Return Validation
End Function
Private Sub userID_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
user00.Validating, _
user01.Validating, _
user02.Validating, _
user03.Validating, _
user04.Validating, _
user05.Validating, _
user06.Validating, _
user07.Validating, _
user07.Validating, _
user08.Validating, _
user09.Validating, _
user10.Validating, _
user11.Validating
If Not IsNumeric(sender.Text) OrElse (sender.Text.Length <> 10) AndAlso (sender.Text.Length <> 13) Then
ErrorProvider1.SetError(sender, "")
ErrorProvider2.SetError(sender, "Please enter a valid User ID.")
e.Cancel = True
Else
ErrorProvider1.SetError(sender, "Valid User ID.")
ErrorProvider2.SetError(sender, "")
End If
End Sub
Thanks to your help it works as I wanted, but can you help me improve/clean it? I'm still studying vb, I'm open to any suggestion. Thanks in advance!
Here you have a code performing the actions you want:
Dim done1, done2, done3 As Boolean
Dim targetLength1 As Integer = 10
Dim targetLength2 As Integer = 13
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Try
If (IsNumeric(TextBox1.Text)) Then
If (TextBox1.Text.Length = targetLength1 Or TextBox1.Text.Length = targetLength2) Then
done1 = True
End If
End If
If (done1 And done2 And done3) Then
Button1.Enabled = True
End If
Catch ex As Exception
End Try
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
Try
If (IsNumeric(TextBox2.Text)) Then
If (TextBox2.Text.Length = targetLength1 Or TextBox2.Text.Length = targetLength2) Then
done2 = True
End If
End If
If (done1 And done2 And done3) Then
Button1.Enabled = True
End If
Catch ex As Exception
End Try
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
Try
If (IsNumeric(TextBox3.Text)) Then
If (TextBox3.Text.Length = targetLength1 Or TextBox3.Text.Length = targetLength2) Then
done3 = True
End If
End If
If (done1 And done2 And done3) Then
Button1.Enabled = True
End If
Catch ex As Exception
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Enabled = False
End Sub
It accounts just for 3 textboxes (TextBox1, TextBox2 and TextBox3) and 1 button (Button1) but you can extend the idea to as many textboxes as you wish. It relies on the TextChanged even of each textbox. When the condition is met (given textbox has a number whose length is 10 or 13), the corresponding flag is set to true (e.g., done1 for TextBox1...). When all the flags are true (all the textboxes contain the expected information), the button is disabled.
I think would better to use TextChanged Event ..
Private Sub TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, ....., TextBox10.TextChanged
Chk4ButtonEnabled()
End Sub
Sub Chk4ButtonEnabled()
Dim s as String
For Each userID As Control In Me.Controls
If userID.GetType Is GetType(TextBox) Then
s = userID.Text
If Not s.Length = 10 OR Not s.Length = 13 Then
ErrorProvider1.SetError(userID, "")
buttonSave.Enabled = False
Else
ErrorProvider1.SetError(userID, "Valid User ID.")
buttonSave.Enabled = True
End If
End If
Next
End Sub