2014-03-25 70 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

相關問題