从组合框中选择项目时,将新数据添加到文本框

时间:2014-03-25 14:40:27

标签: asp.net database vb.net combobox

textbox选择项目后,如何在点击button后自动为combobox输入数字。我所知道的就是从comboboxtextbox获取数据。但我想要做的是在选择项目后将数据添加到textbox

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

如果我理解你想要的东西,有几种方法可以做到这一点。

1。)我们可以检查combobox.text属性中是否有任何内容。

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

这是最简单的方法,但是在添加数据后你必须将组合框设置回null,以使其再次工作。

2.。)我们可以使用Combobox.SelectedIndexChanged方法。

以下是文档网站:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx

以下是一个例子:

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

        }

希望这有帮助。

答案 1 :(得分:0)

Private Sub p1()
    If Not con.State = ConnectionState.Open Then
        con.Open()
    End If

    Com = con.CreateCommand
    Com.CommandText = "SELECT COUNT(vote.presnum) as countofpres FROM vote WHERE presnum ='1';"
    Dim dt As OleDb.OleDbDataReader = Com.ExecuteReader

    dt.Read()

    Dim countpres As Integer = dt("countofpres")

    Me.pres1.Text = countpres

    dt.Close()

End Sub

这里是Jlott