Add new data to the text box when selecting the item from the toolbox

advertisements

How can I automatically input a number to a textbox after clicking a button upon selecting an item from a combobox. All I know to do is to get the data from the combobox to textbox. But what I would like to do is to add data to a textbox once the item is selected.

This is My Code:

Imports System.Data.OleDb
Imports System.Data

Public Class voting1

Dim con As New OleDbConnection
Dim Com As New OleDbCommand
Dim ComInsert As New OleDbCommand
Dim ComUpdate As New OleDbCommand
Dim ComDelete As New OleDbCommand
Dim aAdapter As New OleDbDataAdapter
Dim Dset As New DataSet

Private Sub voting1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim ConProvider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\EvS\EVS.accdb"
    Try

        Try
            con.ConnectionString = ConProvider
            If Not con.State = ConnectionState.Open Then
                con.Open()
            End If

        Catch ex As Exception
            MsgBox("No Connection Established")
        End Try

        Me.fill()

        user.Visible = False
        user.Text = Form1.usertxt.Text

    Catch ex As Exception

    End Try

End Sub

Private Sub fill()

    With aAdapter
        .SelectCommand = New OleDb.OleDbCommand()
        .SelectCommand.CommandText = "select * from candidate"
        .SelectCommand.Connection = con
    End With
    Dim dataRead As OleDb.OleDbDataReader
    dataRead = aAdapter.SelectCommand.ExecuteReader()

    While (dataRead.Read())
        presbox.Items.Add(dataRead("President"))
        vpbox.Items.Add(dataRead("VicePresident"))
        secbox.Items.Add(dataRead("Secretary"))
        treasbox.Items.Add(dataRead("Treasurer"))
    End While
    aAdapter.Dispose()
End Sub

Public Sub Initialize()
    Try
        If Not con.State = ConnectionState.Open Then
            con.Open()
        End If
    Catch ex As System.Exception
        MsgBox(ex.Message, MsgBoxStyle.Information, "ERROR CONNECTION")

    End Try
End Sub

Private Sub bo()
        Dim con1 As New OleDbConnection
        Dim cmd As New OleDbCommand
        con1.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\EvS\EVS.accdb"
        cmd.Connection = con1
        con1.Open()
        Dim num As Integer
    cmd.CommandText = "SELECT pres1 FROM vote1"
        If IsDBNull(cmd.ExecuteScalar) Then
            num = 1
            samp.Text = num
        Else
            num = 1
            samp.Text = num
        End If
        cmd.Dispose()
        con1.Close()
    con1.Dispose()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Try
        Call Initialize()

        If presbox.Text = "" Or vpbox.Text = "" Or secbox.Text = "" Or treasbox.Text = "" Then

            MsgBox("All Fields are required, Check the fields", MsgBoxStyle.Information, "Required Fiels")
            Exit Sub
        End If

        With aAdapter
            .SelectCommand = New OleDb.OleDbCommand()
            .SelectCommand.CommandText = "select * from [vote] where username = '" & user.Text & "'"
            .SelectCommand.Connection = con
        End With

        Dim dataRead As OleDb.OleDbDataReader
        dataRead = aAdapter.SelectCommand.ExecuteReader()

        If Not dataRead.HasRows Then

            ComInsert.CommandText = "INSERT INTO vote([username],[president],[vicePresident],[secretary],[treasurer])" & _
                    "VALUES('" & user.Text & "','" & presbox.Text & "','" & vpbox.Text & "','" & secbox.Text & "','" & treasbox.Text & "')"

            ComInsert.Connection = con
            ComInsert.ExecuteNonQuery()

            MsgBox("Voting Successful!", MsgBoxStyle.Information, "NEW RECORD")

            Form1.Show()
            Me.Close()
            aAdapter.Dispose()
            ComInsert.Dispose()
        Else
            MsgBox("WARNING: User Voted Already!!", MsgBoxStyle.Exclamation, "ERROR SAVING DATA")
            Me.Close()
            Form1.Show()
        End If
    Catch ex As Exception

    End Try
End Sub

End Class


There are a couple of ways to do this if I am understanding what you are wanting.

1.) We can just check if there is anything in the combobox.text property.

If String.IsNullOrEmpty(cbobox.text) Then
 Perform the logic of adding data to textbox here.
cbobox.text = null
End If

That is the most simple way, but you will have to set the combobox back to null after the data is added to make it work again.

2.) We can make use of the Combobox.SelectedIndexChanged method.

Here is the documentation website: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx

Here is an example:

    private void ComboBox1_SelectedIndexChanged(object sender,
            System.EventArgs e)
        {
perform logic here

        }

Hope this helps.