VB.net/Excel- "Backwards" tab index For Each iteration with textboxes.
Posted
by MSD
on Stack Overflow
See other posts from Stack Overflow
or by MSD
Published on 2010-06-17T18:50:52Z
Indexed on
2010/06/17
18:53 UTC
Read the original article
Hit count: 194
Hi there,
I have a form with 3 textboxes and 1 button.
textbox1 has tab index 0, and it's text = 1
textbox2 has tab index 1, and it's text = 2
textbox3 has tab index 2, and it's text = 3
I want to iterate thru the textboxes and place their values into cells so that...
range("A1").value = txtbox1.text (ie: A1 = "1") range("A2").value = txtbox2.text (ie: A2 = "2") range("A3").value = txtbox3.text (ie: A3 = "3")
but what I am getting is...
range("A1").value = txtbox1.text (ie: A1 = "3") range("A2").value = txtbox2.text (ie: A2 = "2") range("A3").value = txtbox3.text (ie: A3 = "1")
I have tried reversing the tab index for the text boxes, but it doesn't change the "backwards iteration".
Is there something I can do to change this so that the loop runs from lowest tab index to highest?
Thanks!
Public Class Form1
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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 End Class
© Stack Overflow or respective owner