How to overwrite suggest text in textbox during typing? - excel

I have Userform using Textbox to input date.
I'd like to show suggestion text before input like __ /__/____ (same format dd/mm/yyyy)
When enter this Textbox, cursor always in beginning. When I typing, each _ symbol will be replaced by number, and skip / symbol.
For example: I just type 05041991, in Textbox will show 05/04/1991.
Please help me about this code.

You could do something like shown below. This code is just an example (probably not perfect).
Image 1: Note that only number keys and backspace were pressed.
Put the following code into a class module and name it MaskedTextBox
Option Explicit
Public WithEvents mTextBox As MSForms.TextBox
Private mMask As String
Private mMaskPlaceholder As String
Private mMaskSeparator As String
Public Enum AllowedKeysEnum
NumberKeys = 1 '2^0
CharacterKeys = 2 '2^1
'for more options next values need to be 2^2, 2^3, 2^4, …
End Enum
Private mAllowedKeys As AllowedKeysEnum
Public Sub SetMask(ByVal Mask As String, ByVal MaskPlaceholder As String, ByVal MaskSeparator As String, Optional ByVal AllowedKeys As AllowedKeysEnum = NumberKeys)
mMask = Mask
mMaskPlaceholder = MaskPlaceholder
mMaskSeparator = MaskSeparator
mAllowedKeys = AllowedKeys
mTextBox.Text = mMask
FixSelection
End Sub
' move selection so separators get not replaced
Private Sub FixSelection()
With mTextBox
Dim Sel As Long
Sel = InStr(1, .Text, mMaskPlaceholder) - 1
If Sel >= 0 Then
.SelStart = Sel
.SelLength = 1
End If
End With
End Sub
Private Sub mTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim tb As MSForms.TextBox
Set tb = Me.mTextBox
'allow paste
If Shift = 2 And KeyCode = vbKeyV Then
On Error Resume Next
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
Dim PasteData As String
PasteData = DataObj.GetText(1)
On Error GoTo 0
If PasteData <> vbNullString Then
Dim LikeMask As String
LikeMask = Replace$(mMask, mMaskPlaceholder, "?")
If PasteData Like LikeMask Then
mTextBox = PasteData
End If
End If
End If
Select Case KeyCode
Case vbKey0 To vbKey9, vbKeyNumpad0 To vbKeyNumpad9
'allow number keys
If Not (mAllowedKeys And NumberKeys) = NumberKeys Then
KeyCode = 0
ElseIf Len(tb.Text) >= Len(mMask) And InStr(1, tb.Text, mMaskPlaceholder) = 0 Then
KeyCode = 0
End If
Case vbKeyA To vbKeyZ
'allow character keys
If Not (mAllowedKeys And CharacterKeys) = CharacterKeys Then
KeyCode = 0
ElseIf Len(tb.Text) >= Len(mMask) And InStr(1, tb.Text, mMaskPlaceholder) = 0 Then
KeyCode = 0
End If
Case vbKeyBack
'allow backspace key
KeyCode = 0
If tb.SelStart > 0 Then 'only if not first character
If Mid$(tb.Text, tb.SelStart, 1) = mMaskSeparator Then
'jump over separators
tb.SelStart = tb.SelStart - 1
End If
'remove character left of selection and fill in mask
If tb.SelLength <= 1 Then
tb.Text = Left$(tb.Text, tb.SelStart - 1) & Mid$(mMask, tb.SelStart, 1) & Right$(tb.Text, Len(tb.Text) - tb.SelStart)
End If
End If
'if whole value is selected replace with mask
If tb.SelLength = Len(mMask) Then tb.Text = mMask
Case vbKeyReturn, vbKeyTab, vbKeyEscape
'allow these keys
Case Else
'disallow any other key
KeyCode = 0
End Select
FixSelection
End Sub
Private Sub mTextBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
FixSelection
End Sub
Put the following code into your userform
Option Explicit
Private MaskedTextBoxes As Collection
Private Sub UserForm_Initialize()
Set MaskedTextBoxes = New Collection
Dim MaskedTextBox As MaskedTextBox
'init TextBox1 as date textbox
Set MaskedTextBox = New MaskedTextBox
Set MaskedTextBox.mTextBox = Me.TextBox1
MaskedTextBox.SetMask Mask:="__/__/____", MaskPlaceholder:="_", MaskSeparator:="/"
MaskedTextBoxes.Add MaskedTextBox
'init TextBox2 as barcode textbox
Set MaskedTextBox = New MaskedTextBox
Set MaskedTextBox.mTextBox = Me.TextBox2
MaskedTextBox.SetMask Mask:="____-____-____", MaskPlaceholder:="_", MaskSeparator:="-", AllowedKeys:=CharacterKeys + NumberKeys
MaskedTextBoxes.Add MaskedTextBox
End Sub

Related

Excel VBA UserForm Tag Property

I have a UserForm with Text and Combo Boxes, some of which represent "REQUIRED FIELDS" and cannot be left blank. I have entered the value RQD for the tag property of the target controls. My objective is to loop through the controls and use their Tag property to identify controls that cannot be empty (where Tag property value = RQD) and change their BackColor property if they are. However, I cannot get this work. Below is some of the code:-
With frm_RecCapture
'
.lbl01_RecDate = Format(Date, "Long Date", vbSunday)
.txt01_RecNum = Format(RecNum, "000000")
.txt01_RecNum.Enabled = False
.txt01_AccNum.SetFocus
'
frmComplete = False
.Show
'
Do While frmComplete = False
.Show
'
For Each frmCtrl In .Controls
If TypeName(frmCtrl) = "Textbox" Or TypeName(frmCtrl) = "Combobox" Then
If frmCtrl.Tag = "RQD" And frmCtrl.Text = "" Then
frmCtrl.BackColor = &HFFFF&
n = n + 1
End If
End If
Next frmCtrl
'
If n = 0 Then
frmComplete = True
Else
frmComplete = False
MsgBox "ERROR! Fields highlighted in yellow cannot be left blank. Please "
complete these fields before continuing.", vbInformation + vbOKOnly, SysTitle
End If
Loop
'
End With
Any suggestions? Thanks...
I prefer writing a class that wraps and validates a control. You will need to store a reference to the wrapper class in a collection of some type of class level variable keep it from falling out of scope.
Class: RequiredFieldControl
Option Explicit
Private WithEvents ComboBox As MSForms.ComboBox
Private WithEvents TextBox As MSForms.TextBox
Private mControl As MSForms.Control
Private Const DefaultBackColor As Long = -2147483643
Private Const InvalidBackColor As Long = vbYellow ' &HFFFF&
Public Property Set Control(ByVal Value As MSForms.Control)
Set mControl = Value
Select Case TypeName(Control)
Case "ComboBox"
Set ComboBox = Value
Case "TextBox"
Set TextBox = Value
End Select
FormatControl
End Property
Public Property Get Control() As MSForms.Control
Set Control = mControl
End Property
Sub FormatControl()
Control.BackColor = IIf(isValid, DefaultBackColor, InvalidBackColor)
End Sub
Public Function isValid() As Boolean
Select Case TypeName(Control)
Case "ComboBox"
isValid = ComboBox.ListIndex > -1
Case "TextBox"
isValid = Len(TextBox.Value) > 0
End Select
End Function
Private Sub ComboBox_Change()
FormatControl
End Sub
Private Sub TextBox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
FormatControl
End Sub
Form Code
Private RequiredFields As Collection
Private Sub UserForm_Initialize()
Set RequiredFields = New Collection
AddRequiredFields Me.Controls
ComboBox1.List = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Debug.Print RequiredFields.Count
End Sub
Sub AddRequiredFields(pControls As MSForms.Controls)
Dim RequiredField As RequiredFieldControl
Dim Control As MSForms.Control
For Each Control In pControls
If Control.Tag = "RQD" Then
Select Case TypeName(Control)
Case "ComboBox", "TextBox"
Set RequiredField = New RequiredFieldControl
Set RequiredField.Control = Control
RequiredFields.Add RequiredField
End Select
Else
On Error Resume Next
AddRequiredFields Control.Controls
On Error GoTo 0
End If
Next
End Sub
Function AreAllRequiredFieldsFilled() As Boolean
Dim RequiredField As RequiredFieldControl
For Each RequiredField In RequiredFields
If Not RequiredField.isValid Then Exit Function
Next
AreAllRequiredFieldsFilled = True
End Function

Click and recognize part of text in textbox vb.net

I am trying to make a program that utilises a textbox that has text options listed in it that can be clicked on.
As a textbox example:
[Selection:<1><2><3>]
so the user could then as example click on (over the text) <2> to select the 2nd option or <3> so select the 3rd option. The idea comes from the AutoCAD commands prompt which uses a similar system.
How would I achieve something like this in vb.net code (if its even possible)?
Try this:
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles TextBox1.Click
Dim SplitText As String() = TextBox1.Text.Split(CChar("<"), ">")
Dim SelectedText As String = GetSelectedText()
Dim Options As New List(Of String)
If Not SelectedText = "" Then
For i = 0 To SplitText.Length - 1
If IsNumeric(SplitText(i)) Then
Options.Add("<" & SplitText(i) & ">")
End If
Next
For i = 0 To Options.Count - 1
If SelectedText = Options(i) Then
'Put your code here if it is the current option in the loop equals the selected option.
'I added a messagebox just so you can see the current option.
MessageBox.Show("You selected option: " & Options(i))
End If
Next
End If
End Sub
Private Function GetSelectedText()
Dim CursorPosition As Integer = TextBox1.SelectionStart
Dim SelectedNumber As String = ""
Dim NumberLength As Integer = 0
If CursorPosition = 0 Or CursorPosition = TextBox1.Text.Length Then
Return ""
End If
Do Until Mid(TextBox1.Text, CursorPosition - NumberLength, 1) = "<"
NumberLength += 1
Loop
SelectedNumber = Mid(TextBox1.Text, CursorPosition - NumberLength, NumberLength + 1)
NumberLength = 0
CursorPosition += 1
Do Until Mid(TextBox1.Text, CursorPosition + NumberLength, 1) = ">"
NumberLength += 1
Loop
SelectedNumber &= Mid(TextBox1.Text, CursorPosition, NumberLength + 1)
If IsNumeric(SelectedNumber.Remove(0, 1).Remove(SelectedNumber.Length - 2, 1)) Then
Return SelectedNumber
Else
Return ""
End If
End Function
I put this inside of the textbox click event, and it works. I did not try putting the code in any other events. I assume that the textbox is named: TextBox1.
Here's a quick example showing how to build a "menu" with a LinkLabel:
Public Class Form1
Private menuItems() As String = {"cat", "dog", "fish"}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim menu As String = "Selection: " & String.Join(", ", menuItems)
LinkLabel1.Text = menu
LinkLabel1.Links.Clear()
For Each item In menuItems
LinkLabel1.Links.Add(menu.IndexOf(item), item.Length)
Next
End Sub
Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Dim i As Integer = LinkLabel1.Links.IndexOf(e.Link)
Label1.Text = menuItems(i)
End Sub
End Class
Output:

VBA Excel ListView Checkboxes do not show in Userform

I have a UserForm with a MultipageControl (name Controller_MultiPage).
At runtime my code adds pages to the Multipage and creates a newListView on each page.
Every ListView has:
With newListView
.MultiSelect = False
.Width = Controller_MultiPage.Width - 10
.Height = Controller_MultiPage.Height - 20
.View = lvwReport
.HideColumnHeaders = False
.ColumnHeaders.Add Text:="Signal Name", Width:=.Width / 10 * 4
.ColumnHeaders.Add Text:="Type", Width:=.Width / 10
.ColumnHeaders.Add Text:="I/O", Width:=.Width / 10
.ColumnHeaders.Add Text:="Description", Width:=.Width / 10 * 4
.CheckBoxes = True
.FullRowSelect = True
End With
then I populate the newListView with data from an XML file:
For Each node In list
With node.Attributes
Set listItem = newListView.ListItems.Add(Text:=.getNamedItem("Name").Text)
listItem.ListSubItems.Add = .getNamedItem("Type").Text
listItem.ListSubItems.Add = IIf(.getNamedItem("Input").Text = "1", "IN", "OUT")
listItem.ListSubItems.Add = .getNamedItem("Description").Text
listItem.Checked = False
End With
Next
but the checkboxes do not show. I can see the space for them in front of the first column and by clicking that space the checkbox of that particular row then appears. What I also noticed is that if I change the property
listItem.Checked = True
the behavior described above does not change, and when I click the free space in front of the first column (checkboxes space) the chsckbox that then shows up is still unchecked.
Any idea?
The problem seems to be in the behavior of the MultiPage control.
What I noticed was that if I forced the checkboxes' status (checked or unchecked) from the code, using the MultiPage_Change event, then the checkboxes show up.
So what I did was to create a class that holds the status of all checkboxes of all listviews on a single page, instantiate the Class for each ListView and store everything into a Dictionary, using the newListView.Name as Key
Then when the user changes page, the MultiPage_Change event that fires resets all the values of the checkboxes according to the Dictionary stored values.
In the Listview_N_ItemChecked event some other code updates the status of the item stored in the Dictionary.
Kind of cumbersome but it works.
the class (updated):
' Class Name = ComponentsSignalsRecord
Option Explicit
Dim Name As String
' NOTE: Signals(0) will always be empty and status(0) will always be False
Dim Signals() As String
Dim Status() As Boolean
Dim Component As String
Property Let SetComponentName(argName As String)
Component = argName
End Property
Property Get GetComponentName() As String
GetComponentName = Component
End Property
Property Get getSignalName(argIndex) As String
If argIndex >= LBound(Signals) And argIndex <= UBound(Signals) Then
getSignalName = Signals(argIndex)
Else
getSignalName = vbNullString
End If
End Property
Property Get dumpAll() As String()
dumpAll = Signals
End Property
Property Get Count() As Long
Count = UBound(Signals)
End Property
Property Get getStatus(argName As String) As Integer
' returns: -1 = Not Found; 1 = True; 0 = False
getStatus = -1
Dim i As Integer
For i = 0 To UBound(Signals)
If argName = Signals(i) Then getStatus = IIf(Status(i) = True, 1, 0): Exit For
Next
End Property
Property Let setName(argName As String)
Name = argName
End Property
Property Get getName() As String
getName = Name
End Property
Public Sub UncheckAll()
Dim i As Integer
For i = 0 To UBound(Status)
Status(i) = False
Next
End Sub
Public Sub CheckAll()
Dim i As Integer
For i = 0 To UBound(Status)
Status(i) = True
Next
End Sub
Public Sub deleteSignal(argName As String)
Dim spoolSignals() As String
Dim spoolStatus() As Boolean
Dim i As Integer
spoolSignals = Signals
spoolStatus = Status
ReDim Signals(0)
ReDim Status(0)
For i = 1 To UBound(spoolSignals)
If argName <> spoolSignals(i) Then
ReDim Preserve Signals(UBound(Signals) + 1): Signals(UBound(Signals)) = spoolSignals(i)
ReDim Preserve Status(UBound(Status) + 1): Status(UBound(Status)) = spoolStatus(i)
End If
Next
End Sub
Public Sub addSignal(argName As String, argValue As Boolean)
Dim i As Integer
For i = 0 To UBound(Signals)
If argName = Signals(i) Then GoTo bye
Next
ReDim Preserve Signals(UBound(Signals) + 1)
ReDim Preserve Status(UBound(Status) + 1)
Signals(UBound(Signals)) = argName
Status(UBound(Status)) = argValue
bye:
End Sub
Public Sub setStatus(argName As String, argValue As Boolean)
Dim i As Integer
For i = 0 To UBound(Signals)
If argName = Signals(i) Then Status(i) = argValue: Exit For
Next
End Sub
Private Sub Class_Initialize()
ReDim Signals(0)
ReDim Status(0)
End Sub
The Form relevant code. Module level:
Dim myDict As New Dictionary ' the Dictionary
Dim ComponentsSignalsList As ComponentsSignalsRecord ' the Class
for each ListView created, may be one or more for every single MultiPage page :
Set ComponentsSignalsList = New ComponentsSignalsRecord
ComponentsSignalsList.setName = newListView.name
while populating the listview(s) in a loop for each single item added:
ComponentsSignalsList.addSignal List_Item.Text, List_Item.Checked
end of each loop, add the Class instance to the Dictionary:
myDict.Add ComponentsSignalsList.getName, ComponentsSignalsList
Now when changing Page in the MultiPage widget:
Private Sub Controller_MultiPage_Change()
If isLoading Then Exit Sub 'avoid errors and undue behavior while initializing the MultiPage widget
Dim locControl As Control
Dim controlType As String: controlType = "ListView"
With Controller_MultiPage
For Each locControl In .Pages(.value).Controls
If InStr(1, TypeName(locControl), controlType) > 0 Then
Call Check_CheckBoxes(locControl)
End If
Next
End With
End Sub
Private Sub Check_CheckBoxes(argListView As listView)
If argListView.CheckBoxes = False Then Exit Sub 'some ListViews don't have checkboxes
Dim myItem As ListItem
For Each myItem In argListView.ListItems
With myItem
.Checked = myDict.Item(argListView.name).getStatus(.Text)
End With
Next
End Sub
when ticking/unticking a checkbox (note the the ItemChecked event handler is defined in another Class Public WithEvents, where the handler calls this method passing both the ListView ID and the Item object) :
Public Sub ListViewsEvents_ItemCheck(argListView As listView, argItem As MSComctlLib.ListItem)
With argItem
myDict.Item((argListView .name).setStatus argName:=.Text, argValue:=.Checked
End With
End Sub
I just found the answer to the same problem that I also had and I feel so stupid. I had the first column of the Listview set to Width = 0... and thus the checkboxes would no longer show.
I gave it a width and everithing is back to normal...

Excel VBA Userform Dynamic Runtime Controls - Trigger Same Class Event Across Multiple Controls

I am in the process of building an Excel based Application that builds itself dynamically at run-time based on external data.
Here is the empty userform:
Code within UserForm_Activate()
Private Sub UserForm_Activate()
Dim f As Control, i As Integer
mdMenuItems.BuildMenuItems
mdTheme.GetTheme
For Each f In Me.Controls
If TypeName(f) = "Frame" Then
i = i + 1
ReDim Preserve fra(1 To i)
Set fra(i).fraEvent1 = f
End If
Next f
End Sub
mdMenuItems.BuildMenuItems dynamically builds a series of menu items based on external data...
Code within mdMenuItems module
Option Explicit
Dim lbl() As New cMenuItem
Public myFileData As String
Public myFileValue As String
Public frmTheme As String
Sub BuildMenuItems()
Dim FileNum As Integer, i As Integer
Dim WrdArray() As String
Dim lblMenuIcon As MSForms.Label, lblMenuText As MSForms.Label, lblMenuBackground As MSForms.Label
FileNum = FreeFile()
Open Application.ThisWorkbook.Path & "\Data\MenuItems.csv" For Input As #FileNum
Do While Not EOF(FileNum)
i = i + 1
Line Input #FileNum, myFileData ' read in data 1 line at a time
WrdArray() = Split(myFileData, ",")
Set lblMenuBackground = frmTest.frmMenuBackground.Controls.Add("Forms.Label.1", "lblMenuBackground_" & i)
Set lblMenuIcon = frmTest.frmMenuBackground.Controls.Add("Forms.Label.1", "lblMenuIcon_" & i)
Set lblMenuText = frmTest.frmMenuBackground.Controls.Add("Forms.Label.1", "lblMenuText_" & i)
With lblMenuBackground
.top = 30 * i
.left = 0
.Width = 170
.Height = 30
.BackColor = RGB(255, 255, 255)
.BackStyle = fmBackStyleOpaque
.MousePointer = fmMousePointerCustom
.MouseIcon = LoadPicture(Application.ThisWorkbook.Path & "\Creative\Other\Hand.cur")
.Tag = "_006"
End With
ReDim Preserve lbl(1 To i)
Set lbl(i).lblEvent1 = lblMenuBackground
With lblMenuIcon
.Caption = Sheets("FontAwesome").Cells(WrdArray(0), 1)
.top = (30 * i) + 9
.left = 0
.Width = 30
.Height = 20
.ForeColor = RGB(0, 0, 0)
.BackStyle = fmBackStyleTransparent
.Font.Name = "FontAwesome"
.Font.Size = 14
.TextAlign = fmTextAlignCenter
.MousePointer = fmMousePointerCustom
.MouseIcon = LoadPicture(Application.ThisWorkbook.Path & "\Creative\Other\Hand.cur")
.Tag = "-021"
End With
With lblMenuText
.Caption = WrdArray(1)
.top = (30 * i) + 8
.left = 30
.Width = 90
.Height = 20
.ForeColor = RGB(0, 0, 0)
.BackStyle = fmBackStyleTransparent
.Font.Size = 12
.MousePointer = fmMousePointerCustom
.MouseIcon = LoadPicture(Application.ThisWorkbook.Path & "\Creative\Other\Hand.cur")
.Tag = "-021"
End With
Loop
Close #FileNum
End Sub
Ok, so a brief overview of whats happeing here...
I open a data file MenuItems.csv for input. I assign each line within this file to i. I then Set three individual MSForms.Label(s):
lblMenuBackground
lblMenuIcon
lblMenuText
...and build them asynchronously.
You will notice that after building the first label (lblMenuBackground), I assign a custom class event lbl(i).lblEvent1 = lblMenuBackground.
(It is important that I use ReDim Preserve correctly here so that each sequential menu item gains this custom class, and not just the last one.)
Code within cMenuItem class module
Public WithEvents lblEvent1 As MSForms.Label
Private Sub lblEvent1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim ctl As Control
For Each ctl In frmTest.frmMenuBackground.Controls
If TypeName(ctl) = "Label" Then
If Not ctl.BackColor = RGB(GetB(mdTheme.frmThemeID6), GetG(mdTheme.frmThemeID6), GetR(mdTheme.frmThemeID6)) Then ctl.BackColor = RGB(GetB(mdTheme.frmThemeID6), GetG(mdTheme.frmThemeID6), GetR(mdTheme.frmThemeID6))
End If
Next ctl
Me.lblEvent1.BackColor = RGB(GetB(mdTheme.frmThemeID2), GetG(mdTheme.frmThemeID2), GetR(mdTheme.frmThemeID2))
End Sub
(Please ignore the .BackColor property complexity here as it could get even more confusing, and is un-related to this question.)
After UserForm_Activate, here is the updated form:
(You may notice the use of FontAwesome icons here.)
Because I have added a custom MouseOver class event to each lblMenuBackground label, mousing over causes the .BackColor to change:
Here is my issue...
This mouse over effect is only triggered when the cursor passes over one of the three labels that make up each menu item.
lblMenuBackground
Why?
I only know how to affect the called control's properties.
Or rather...
I don't know how to affect un-called control properties from within the called control's event.
Here is the structure of each menu item:
Here is my question...
How can I affect the .BackColor of the same control from the MouseOver events of all three individual controls which make up each menu item?
Moves cursor over icon = Background colour changes
Moves cursor over text = Background colour changes
Moves cursor over background = Background colour changes
The class event needs to be assigned at build time...
ReDim Preserve lbl(1 To i)
Set lbl(i).lblEvent1 = lblMenuBackground
...for each menu item.
EndSubQuestion
__________
This logic will fundamentally lay the foundations for my interface.
For those of you who made it this far - thank you for reading!
Any help is appreciated.
Thanks,
Mr. J
You are on hooking into the events for lblMenuBackground
lbl(i).lblEvent1 = lblMenuBackground
Modify BuildMenuItems
Change
Set lbl(i).lblEvent1 = lblMenuBackground
to
Set lbl(i) = New cMenuItem
lbl(i).setControls lblMenuBackground, lblMenuIcon, lblMenuText
Modify CMenuItem Class
Public WithEvents m_lblMenuBackground As MSForms.Label
Public WithEvents m_lblMenuIcon As MSForms.Label
Public WithEvents m_lblMenuText As MSForms.Label
Public Sub setControls(lblMenuBackground As MSForms.Label, lblMenuIcon As MSForms.Label, lblMenuText As MSForms.Label)
Set m_lblMenuBackground = lblMenuBackground
Set m_lblMenuIcon = lblMenuIcon
Set m_lblMenuText = lblMenuText
End Sub
Private Sub m_lblMenuBackground_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Update
End Sub
Private Sub m_lblMenuIcon_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Update
End Sub
Private Sub m_lblMenuText_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Update
End Sub
Private Sub Update()
Dim ctl As Control
For Each ctl In frmTest.frmMenuBackground.Controls
If TypeName(ctl) = "Label" Then
If Not ctl.BackColor = RGB(GetB(mdTheme.frmThemeID6), GetG(mdTheme.frmThemeID6), GetR(mdTheme.frmThemeID6)) Then ctl.BackColor = RGB(GetB(mdTheme.frmThemeID6), GetG(mdTheme.frmThemeID6), GetR(mdTheme.frmThemeID6))
End If
Next ctl
Me.m_lblMenuBackground.BackColor = RGB(GetB(mdTheme.frmThemeID2), GetG(mdTheme.frmThemeID2), GetR(mdTheme.frmThemeID2))
End Sub

Working with dynamic event handler in vba

I have created a form where I am dynamically creating Textboxes and corresponding Comboboxes along with Combobox change event. Here is the class creating combobox event handler
Option Explicit
Public WithEvents cbx As MSforms.Combobox
Private avarSplit As Variant
Sub SetCombobox(ctl As MSforms.Combobox)
Set cbx = ctl
End Sub
Private Sub cbx_Change()
Dim i As Integer
If cbx.ListIndex > -1 Then
'MsgBox "You clicked on " & cbx.Name & vbLf & "The value is " & cbx.Value
avarSplit = Split(cbx.Name, "_")
'DecessionOnValue
End If
End Sub
And here is the code on the form which is dynamically creating textboxes and Comboboxes
Function AddTextBox(Frame1 As frame, numberOfColumns As Integer)
Dim counter As Integer
Dim i As Integer
Dim TxtBox As MSforms.TextBox
For counter = 1 To numberOfColumns
'Forms.CommandButton.1
Set TxtBox = Frame1.Controls.Add("Forms.TextBox.1")
TxtBox.Name = "tb_" + CStr(counter)
'Bouton.Caption = "Test"
TxtBox.Visible = True
i = Property.TextBoxDisable(TxtBox)
' Defining coordinates TextBox height is 18
If counter = 1 Then
TxtBox.Top = 23
Else
TxtBox.Top = (18 * counter) + 5 * counter
End If
TxtBox.Left = 50
Next counter
End Function
Function Combobox(Frame1 As frame, numberOfColumns As Integer)
Dim counter As Integer
Dim i As Integer
Dim CbBox As MSforms.Combobox
Dim cbx As ComboWithEvent
If pComboboxes Is Nothing Then Set pComboboxes = New Collection
For counter = 1 To numberOfColumns
'Forms.CommandButton.1
Set CbBox = Frame1.Controls.Add("Forms.ComboBox.1")
CbBox.Name = "cb_" + CStr(counter)
i = AddComboboxValues(CbBox)
' Defining coordinates TextBox height is 18
If counter = 1 Then
CbBox.Top = 23
Else
CbBox.Top = (18 * counter) + 5 * counter
End If
CbBox.Left = 150
Set cbx = New ComboWithEvent
cbx.SetCombobox CbBox
pComboboxes.Add cbx
Next counter
i = AddScrollBar(Frame1, counter)
End Function
Combobox event handler is working fine but my problem is that I dont know that how can I copy the text of textbox or enable disable the textbox according to the value selected in the dynamic combobox.
Thanks,
Jatin
you'll use this for example :
Me.Controls("ControlName").Visible = True or instead of Visible you can use enable, disable etc..

Resources