如何使用对象的值(例如字符串),然后引用Visual Basic .net中表单上的文本框的名称?我希望能够从SQL数据库中获取信息,并根据它们在表单上的名称将它们放在相应的文本框中(例如,将字段fSynopsis的信息放在文本框txtSynopsis.Text中)。 / p>
以下代码是我目前正在使用的代码。
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvResults.CellMouseClick
' Set values in the text boxes to both corresponding to the film.
Dim strFields() As String = {"fSynopsis", "fGenre", "fSynopsis", "fAgeRating", "fActorsActresses", "fWebRating", "fNoReservations", "fWeeksRunning"}
'Create an array containing the names of the fields in the database.
Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=ApplicationData.accdb;Persist Security Info=False;")
Con.Open() 'Open the connection
Dim Cmd As New OleDbCommand(StringBuilderCommand("fAgeRating", "Films", dgvResults.CurrentCell.Value, "fName"), Con) 'Create a string by calling the StringBuilderCommand to combine the parameters together with quotes.
Cmd.CommandType = CommandType.Text
Dim Rdr As OleDbDataReader = Cmd.ExecuteReader()
Dim intCount As Integer = 0 ' Create a loop variable.
Do While Rdr.Read() ' While this statement is 'TRUE', e.g. there is a valid record.
strResult = "txt" & strFields(intCount).Replace("f", "") 'Remove any instances of 'f', e.g. the prefix of the string.
strResult.Text = Rdr.Item(strFields(intCount).ToString)
'Set the text-box to the correct value from the database.
' This will allow me to go through several text boxes, and grab their corresponding values from the database.
intCount += 1
Loop
我非常感谢任何可用的帮助,谢谢!
答案 0 :(得分:2)
试用此代码。
CType( Me.Controls("strResult"),TextBox).Text = Rdr.Item(strFields(intCount).ToString)