I have an assignment to create a program that basically grades a multiple choice test. I have a form with 20 textboxes to accept input (only the letters "a", "b", "c", or "d"), an array with the correct answers, and I created an array to hold all the text boxes:
dim txtboxes() as TextBox = {txtInput1, txtInput2...txtInput20}
I know there's got to be a way to use For Each where the type of control = textbox to iterate over all the textboxes, but I am kind of stuck on actually extracting the values from the textboxes and adding them to an array of their own. I've gotten as far as:
dim txtbox as TextBox
for each txtbox in Controls.OfType(Of TextBox)
which isn't very far at all...
The following code should return the list of textbox values.
Public Sub Convert()
Dim lstTxtBoxValues As List(Of String) = New List(Of String)
For Each txtBox As TextBox In txtboxes
lstTxtBoxValues.Add(txtBox.Text)
Next
End Sub
Related
I am kind of new to programming in general, especially in Visual Basic. As part of a random project I am doing, I have declared a 2D array of type button and have then populated the array with buttons. I am trying to assign a new button to one of the buttons in the array and then access its .text property, but I get a null reference error. Instead of assigning one of the buttons in the array to a new button, I have also just tried to access the .text property of the button in the array, but I get the same error. Sorry if this is worded badly. I am still learning all the proper language.
Public Class Form1
Dim buttonarray(,) As Button = {{btn11, btn12}, {btn21, btn22}}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim newbutton As Button
newbutton = buttonarray(1, 1)
lblOut.Text = newbutton.Text
lblOut.Text = buttonarray(1, 1).Text
End Sub
End Class
This is an example of what I'm trying to do. The buttons in the array are on form1 and are declared in the designer file.
Hey all I have the following VBA code that allows me to have a click event on a dynamically created comboboxes and textboxes onto the userform.
userform:
Option Explicit
Dim comboboxBoxColct As New Collection
Dim textboxBoxColct As New Collection
Private Sub UserForm_Activate()
Dim comboboxEvent As Class1
Dim textboxEvent As Class1
'..lots more code within here
If LCase(TypeName(controller(i))) = "combobox" Then
Set comboboxEvent = New Class1
Set comboboxEvent.comboboxBox = controller(i)
comboboxBoxColct.Add comboboxEvent
ElseIf LCase(TypeName(controller(i))) = "textbox" Then
Set textboxEvent = New Class1
Set textboxEvent.textboxBox = controller(i)
textboxBoxColct.Add textboxEvent
End If
End Sub
Class1:
Option Explicit
Public WithEvents comboboxBox As MSForms.ComboBox
Public WithEvents textboxBox As MSForms.TextBox
Private Sub comboboxBox_Click()
MsgBox "worked"
End Sub
Private Sub textboxBox_Click()
MsgBox "worked"
End Sub
The above code works just fine when using it for the comboboxes. However, once I get to a textbox within that array it never does the msgbox popup.
I am guessing it may have to do with the array number for that box as it may be looking for a 0 instead of whatever the number is in the array 6 so its starting at 6 instead of 0 since its the first textbox in the array it sees.
What can I do in order to get them both to work within the same array?
Hum... seems that a textbox in VBA does not have a Click event, so when I changed it to handle the Change (no pun intended) event instead, it shows the popup if I type a letter into the textbox.
Private Sub textboxBox_Change() 'was textboxBox_Click()
MsgBox "worked"
End Sub
I have created a datagrid, in which there are two columns. One is datagridCombobox and another datagridtextbox column. Datagridcombobox consists of list of items. When I select an item, I want it to be displayed in the next cell ie., datagridtextbox column.
Create a cell value changed event for the grid, in that you can get the value of combobox column value and then assign to 2nd column. hope it helps.
Private Sub DGV_CellValueChanged(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellValueChanged
Dim ComboText As String = DGV.CurrentRow.Cells(1).Value
DGV.CurrentRow.Cells(2).Value = ComboText
End Sub
I've got a listbox issue. I'm attempting to copy the contents of a multi-column listbox to a collection as arrays of each row of the listbox. I realize I could loop through each row and column of the listbox, but I was trying to find a way to return each row of the listbox and assign it directly to the array. Something like this:
For each item in ListBox
tempArrayA = item
tempCollection.add item
Next item
However, when I do that it gives me each individual field of each row rather than the entier row all at once as I'd like it to do. I've been googling, and I'll keep googling, but I just felt it would be nice to do this by iterating through each item in the listbox rather than selecting each row and then itterating through each column of that row since I have other code that executes upon selection of a row of the listbox.
Any ideas are, as always, greatly appreciated. Thanks!
You can take advantage of the worksheet Index function, the fact that it works on arrays, and the fact that a 0 entered as its Column argument grabs the whole row. It's really cool!:
Private Sub UserForm_Activate()
Dim varList As Variant
Dim varRow As Variant
Dim i As Long
Dim tempCollection As Collection
Me.ListBox1.List = ActiveSheet.Range("A2:B20").Value
varList = Me.ListBox1.List
Set tempCollection = New Collection
For i = 1 To Me.ListBox1.ListCount
varRow = Application.WorksheetFunction.Index(varList, i, 0)
tempCollection.Add varRow, CStr(i)
Debug.Print tempCollection(i)(1); " "; tempCollection(i)(2)
Next i
End Sub
am working on a project where some forms will have duplicate methods of populating comboboxes. In the code snippet below, the fonts installed on a given pc are added as items to a combobox. How can I pass a parameter that is the actual combobox to fill? eg, AddFonts(combobox)
Private Sub AddFonts()
'add the font names installed on this pc to the font name combo box
' Get the installed fonts collection.
Dim allFonts As New InstalledFontCollection
' Get an array of the system's font familiies.
Dim fontFamilies() As FontFamily = allFonts.Families
' Display the font families.
For i As Integer = 0 To fontFamilies.Length - 1
'figure our how to make the textbox passable as a paramter
cbxTitleFonts.Items.Add(fontFamilies(i).Name)
Next
End Sub
Pass the control as Control Datatype and cast it in the actual control in the function.
Public Sub mycallingfunc()
myfunc(textbox1)
End Sub
Public Shared Sub myfunc(ctrl As Control)
Dim txt As TextBox = DirectCast(ctrl, TextBox)
End Sub