VB.net/Excel-“Backwards”选项卡索引For Each iteration with textboxes

时间:2010-06-17 18:50:52

标签: vb.net excel

我有一个包含3个文本框和1个按钮的表单。

textbox1的标签索引为0,而text = 1

textbox2的标签索引为1,文字= 2

textbox3的标签索引为2,文字= 3

我想通过文本框进行迭代,并将它们的值放入单元格中,以便......

范围(“A1”)。value = txtbox1.text(即:A1 =“1”) 范围(“A2”)。value = txtbox2.text(即:A2 =“2”) 范围(“A3”)。value = txtbox3.text(即:A3 =“3”)

但我得到的是......

范围(“A1”)。value = txtbox1.text(即:A1 =“3”) 范围(“A2”)。value = txtbox2.text(即:A2 =“2”) 范围(“A3”)。value = txtbox3.text(即:A3 =“1”)

我试过反转文本框的标签索引,但它不会改变“向后迭代”。

我可以做些什么来改变这个,以便循环从最低标签索引运行到最高?

谢谢!

Public Class Form1

Private Sub Button1_Click_1(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click

  Dim objExcel As New Microsoft.Office.Interop.Excel.Application 'Declaring the object.
  objExcel.Visible = True 'Setting Excel to visible.

  Dim cntrl As Control

  With objExcel
     .Workbooks.Add() 'Adding a workbook.
     .Range("A1").Select() 'Selecting cell A1.
  End With


  'Form contains 3 text boxes, with one number in each (1,2,3), and one button to fire the code in this sub.

  For Each cntrl In Me.Controls 'For every control on the form...
     If TypeOf (cntrl) Is TextBox Then 'If the control is a textbox, then...
        With objExcel
           .ActiveCell.Value = cntrl.Text 'place the control's text in the active cell and...
           .ActiveCell.Offset(1, 0).Activate() 'offset down one row.
        End With
     End If 'If the control is not a textbox (if it's the button), do nothing.
  Next 'Go to the next control.

  objExcel = Nothing 'Release the object.
  GC.Collect() 'Clean up.

End Sub 结束班

2 个答案:

答案 0 :(得分:0)

听起来这可能是Excel迭代控件的方式。你试过这个看看输出是什么吗?

  Dim objExcel As New Microsoft.Office.Interop.Excel.Application 'Declaring the object.
  objExcel.Visible = True 'Setting Excel to visible.

  Dim cntrl As Control

  With objExcel
     .Workbooks.Add() 'Adding a workbook.
     .Range("A3").Select() 'Selecting cell A3.
  End With


  'Form contains 3 text boxes, with one number in each (1,2,3), and one button to fire the code in this sub.

  For Each cntrl In Me.Controls 'For every control on the form...
     If TypeOf (cntrl) Is TextBox Then 'If the control is a textbox, then...
        With objExcel
           .ActiveCell.Value = cntrl.Text 'place the control's text in the active cell and...
           .ActiveCell.Offset(-1, 0).Activate() 'offset up one row.
        End With
     End If 'If the control is not a textbox (if it's the button), do nothing.
  Next 'Go to the next control.

  objExcel = Nothing 'Release the object.
  GC.Collect() 'Clean up.

答案 1 :(得分:0)

使用For Each循环意味着你不关心你在做什么顺序。

我要做的是使用控件的“tag”属性来保存您想要答案的行号:

For Each cntrl In Me.Controls 
  If TypeOf (cntrl) Is TextBox Then 
    objExcel.ActiveSheet.Cells(cntrl.Tag, 1).Value = cntrl.Text 
  End If 
Next 

您还可以在表单上添加一个仅包含关键文本框的隐藏面板,然后使用传统的FOR循环而不是“For Each”:

Dim i as integer
Dim myBox as textBox
For i = 1 to 3
    set myBox = Me.Panel1.Controls(i)
    objExcel.ActiveSheet.Cells(myBox.Tag, 1).Value = myBox.Text 
Next i

它应该以Tab顺序进行。你甚至可以跳过类型检查,因为你已经知道它是什么。