【问题标题】:Add new data to textbox when selecting item from combobox从组合框中选择项目时向文本框添加新数据
【发布时间】:2014-03-25 14:40:26
【问题描述】:

在从combobox 中选择项目时单击button 后,如何自动将数字输入textbox。我只知道从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

【问题讨论】:

  • 你到底想做什么?您想知道组合框中的某个项目是否被选中?
  • JLott selectedIndexChanged 是一个很好的答案,但是我更喜欢使用 javascript 来提高页面响应能力,如果您在 HTML 中已经有了要放入文本框中的值,我将使用 HTML 选择元素 OnChange 事件,在这里查看:w3schools.com/jsref/event_onchange.asp
  • 没错,我是从 VB 的角度来看的,而不是 Web 方面的。

标签: asp.net database vb.net combobox


【解决方案1】:

如果我了解您想要什么,有几种方法可以做到这一点。

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 个问题。是否可以在 vb.net 中执行此语句“select count(rooms.rmid) from rooms where rmid='bla'”
  • 所以你不希望它在用户选择某些东西时发生?或者您希望单击按钮...然后等待用户选择某些内容?无论哪种方式 SelectedIndexChanged 都可以工作,只需为单击按钮时设置一个布尔值,或者在单击按钮之前禁用组合框。然后你的问题,我假设你的意思是查询,当然是的。 Dim Query As String = "SELECT COUNT(rooms.rmid) FROM rooms where rmid='bla';"
  • 是的。所以你的意思是说在按钮点击子里面放一个布尔值?那会是什么?嗯。非常感谢。
  • 您可以创建一个布尔值并将其设置为 false。单击按钮后,将其设置为 true。然后在您的 SelectedIndexChanged 方法中,有一个 if 语句来检查该布尔值是否为真。如果是,那么您将数据放入文本框中。简单的逻辑:) 只需要考虑一下。
  • 哦。我现在明白了。现在最后一件事我试过了。但它现在显示任何数据。 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
【解决方案2】:
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

这里是杰洛特

【讨论】:

    猜你喜欢
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多