2014-03-25 67 views
2

combobox中选择项目后,如何在自动输入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 
+0

你想要做什么?你想知道是否选择了组合框中的项目吗? –

+0

JLott selectedIndexChanged是一个很好的答案,但是我更愿意使用JavaScript来提高页面响应能力,并且如果您已经在HTML中具有想要放入文本框的值,那么我将使用HTML选择元素OnChange事件,请在此处查看:http ://www.w3schools.com/jsref/event_onchange.asp – Jportelas

+0

的确,我从VB的角度来看它,而不是网络的一面。 – JLott

回答

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 

     } 

希望这有助于。

+0

嗯。对我好。但是继承人我想做什么。我点击按钮后,所有事情都会发生。我有1个问题。是否有可能在vb.net中执行此语句:“select count(rooms.rmid)from rmid ='bla'” – EverythingRichardify

+0

所以你不希望它在用户选择某些东西时发生?或者你想要按钮被点击...然后等待用户选择一些东西?无论哪种方式,SelectedIndexChanged都可以工作,只需要设置一个布尔值,当点击按钮或禁用组合框时,直到点击按钮。然后你的问题,我认为你的意思是一个查询,当然是的。 Dim Query As String =“SELECT COUNT(rooms.rmid)FROM rmid ='bla';” – JLott

+0

是的。所以你的意思是说,在按钮点击子里面布置了一个布尔值?它会是什么?嗯。非常感谢。 – EverythingRichardify

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

相关问题