这是我用来动态创建文本框的代码,我希望将它们的值从excel表中获取
Imports excel = Microsoft.Office.Interop.Excel
Imports System.Data.OleDb
Public Class Form4
Public Property workbook As Object
Dim x As Single
Public Sub Form4_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Single
For i = 1 To 100
Dim textbox(i) As TextBox
Next
x = InputBox("Enter No of factors")
For i = 1 To x
Dim Combobox1 As New ComboBox
Combobox1.Name = "Textbox" & i
Combobox1.Size = New Size(150, 20)
Combobox1.Location = New Point(10, i * 25)
Me.Controls.Add(Combobox1)
Combobox1.Items.Add("Length")
Combobox1.Items.Add("Breadth")
Combobox1.Items.Add("Height")
Combobox1.Items.Add("Other")
Next
For i = 1 To x
Dim textbox As New TextBox
textbox.Name = "Textbox" & i
textbox.Size = New Size(150, 20)
textbox.Location = New Point(250, i * 25)
Me.Controls.Add(textbox)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim worksheet As excel.Worksheet
Dim workbook As excel.Workbook
Dim app As New excel.Application
workbook = app.Workbooks.Open("D:\3.xlsx")
worksheet = workbook.Worksheets("Sheet1")
For i = 1 To x
workbook.Worksheets("Sheet1").cells(2, 1).value = textbox(i).Text
Next
Me.Close()
workbook.Save()
app.Quit()
End Sub
结束班
答案 0 :(得分:0)
您可以使用For Each
查找在表单中创建的所有TextBox
,然后您可以根据需要操作TextBox
:
For Each ctl1 In ctl.Controls
If TypeOf ctl1 Is TextBox Then
workbook.Worksheets("Sheet1").cells(i, 1).value = ctl1.Text
End If
Next