VBA dynamic array of comboboxes and textboxes click event - arrays

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

Related

What is the best way to update the source of a XamDataGrid from a different form?

I have a XamDataGrid in my MainWindow which has a Public Shared List(Of Artikelstammdaten) as DataSource. After opening a few other forms I want to add more data to the XamDataGrid with a button click. I thought the easiest way would be just to update the DataSource, but I get an Error:
The reference to an unreleased member requires an object reference.
This is what I have tried:
Private Sub Add_Click(sender As Object, e As RoutedEventArgs)
Dim update = MainWindow.listArtikelstammdaten.Concat(CType(Import.ComparedAccessData, IEnumerable(Of Artikelstammdaten)))
dgArticleMasterData.DataSource = update
Me.Close()
End Sub
If dgArticleMasterData is defined in the MainWindow class, you need to get a reference to the MainWindow instance to be able to access it.
You should be able to find it in the Application.Current.Windows collection:
Private Sub Add_Click(sender As Object, e As RoutedEventArgs)
Dim update = MainWindow.listArtikelstammdaten.Concat(CType(Import.ComparedAccessData, IEnumerable(Of Artikelstammdaten)))
Dim window = Application.Current.Windows.OfType(Of MainWindow).FirstOrDefault()
If window IsNot Nothing Then
window.dgArticleMasterData.DataSource = update
End If
Me.Close()
End Sub

ShowDialog Form property is empty

I have two of the simplest forms, with a couple of Buttons and a TextBox. I click to open the second form (frmModal) using ShowDialog(), type some text into txtGreeting and press the Yes button. What should happen is that a MessageBox appears confirming the text that was entered into txtGreeting, but it is empty.
I understand that the Form's properties should be accessible until the form goes out of scope, but they disappear straight-away. I can't even read dialog.txtGreeting.Text.
Am I missing anything obvious please?
Public Class frmMain
Private Sub btnModal_Click(sender As Object, e As EventArgs) Handles btnModal.Click
Dim dialog As frmModal
dialog = New frmModal()
Dim result As DialogResult = frmModal.ShowDialog(Me)
If result = Windows.Forms.DialogResult.Yes Then
MessageBox.Show(dialog.Greeting)
End If
'dialog.Dispose()
End Sub
End Class
Public Class frmModal
Public Property Greeting As String
Get
Return txtGreeting.Text
End Get
Set(value As String)
End Set
End Property
Private Sub btnYes_Click(sender As Object, e As EventArgs) Handles btnYes.Click
MessageBox.Show(Greeting)
End Sub
End Class
The Yes button has it's DialogResult property set to Yes.
I've tried moving the dialog-declaration out of the click-event, using an (unnecessary) Dispose(), deliberately assigning the Greeting property in the Yes-click event..
You're effectively instantiating the form twice. By showing frmModal with ShowDialog and then asking for the Greeting value on the instance you created, named 'dialog'.
This should fix it.
Private Sub ModalTestButton_Click(sender As System.Object, e As System.EventArgs) Handles ModalTestButton.Click
Dim dialog As frmModal
dialog = New frmModal()
Dim result As DialogResult = dialog.ShowDialog(Me)
If result = Windows.Forms.DialogResult.Yes Then
MessageBox.Show(dialog.Greeting)
End If
End Sub

WPF TreeView Selecteditem. Issue with adding child Items (VB,net)

This may have a very simple solution but I am pretty new to this. I am trying add child items into a selected treeView item with the click of a button. Code works fine with nothing selected but seems Treeview1.selectedItem doesn't have an .Add method.
Imports System.Windows.Controls.TreeView
Class MainWindow
Private Sub addNodeButton_Click(sender As Object, e As RoutedEventArgs) Handles addNodeButton.Click
Dim n As String = "Model"
If TreeView1.SelectedItem Is Nothing Then
TreeView1.Items.Add(n)
Else
TreeView1.SelectedItem.add("test")
End If
End Sub
End Class
Any help appreciated.
Ok. I knew it would turn out to be simple. I was initially adding items as Strings insead of TreeViewItems and thus couldn't add children. Fixed code below:
Private Sub addNodeButton_Click(sender As Object, e As RoutedEventArgs) Handles addNodeButton.Click
Dim n As New TreeViewItem
n.Header = "Model"
If TreeView1.SelectedItem Is Nothing Then
TreeView1.Items.Add(n)
Else
Dim tempitem As New TreeViewItem
tempitem = TreeView1.SelectedItem
Dim newitem As New TreeViewItem
newitem.Header = "test"
tempitem.Items.Add(newitem)
End If
End Sub

SelectedIndexChanged event of dropdownlist in Listview not happening. in a usercontrol

I have a ListView and DropDownList(primary) and tow more and a text box, inside every Row of that ListView. All on a user control ,and using the this user control on the other usercontol(here the operations to be performed.)(we are using DNN) .
Now I would like to add event handler for dropdownlist which would react on SelectedIndexChanged. I wanted to show and hide the other dropdowns and textbox depending on a specific value of the DropDrownList(primary) selected.
I am nowhere to do the same..despite the several attempts.
Screening Outcome:&nbsp
Comments:
Subset Used:&nbsp
TAR Status:&nbsp
code behind like this
Public Sub ddlFindingsScreeningOutcome_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
Dim item As ListViewItem = CType(ddl.NamingContainer, ListViewItem)
Dim txtFindingsComments As TextBox = CType(item.FindControl("txtFindingsComments"), TextBox)
Dim ddlFindingsTarStatus As DropDownList = CType(item.FindControl("ddlFindingsTarStatus"), DropDownList)
If Not ddl Is Nothing Then
If Not ddl.SelectedItem.Text = "Not Screened" Then
txtFindingsComments.Visible = True
ddlFindingsTarStatus.Visible = True
End If
Else
txtFindingsComments.Visible = False
ddlFindingsTarStatus.Visible = False
End If
End Sub

passing a control as a parameter

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

Resources