Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




Events For A Dynamic Array Of Controls On A Userform


I'm trying to create an array of controls on a userform which are created at runtime, which I can then reference using an index number to define events.

My problem is that I'm declaring a dynamic array of controls using:

Dim textbox() As Object

and then in the Initialization event for the userform, I have the line:

Set textbox(c) = userform.Controls.Add("Forms.Textbox.1")

to add new textbox controls.


But now, once I have the array of textbox controls, I can't define an event for each of the controls in the array. I.e. the syntax:

Private Sub textbox(c)_Change()gettext = textbox(c).TextEnd Sub

doesn't work because VBA doesn't recognize textbox(c)_Change() syntax.

It has been suggested that I use:
Private Sub textbox_Change(Index as Integer)gettext = textbox(c).TextEnd Sub

but this doesn't work either because VBA doesn't recognize textbox_Change(Index as Integer) as referring to the entire array of textbox controls and the event never fires, at least not the way I've declared the array.

Lastly, using the OnChange property of a control has also been suggested, but it seems like VBA in Excel doesn't recognize it (although it's a legitimate property in VB), e.g. the syntax textbox(c).OnChange = "changesub") isn't recognized.

I have to add controls to the array at runtime and not at designtime because the number of textbox controls in the array depends on an integer variable.

Any advice would be much appreciated.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Events On Dynamic Controls
Check the following code which creates controls at the runtime

Option Explicit

Dim WithEvents myCtl as TextBox

Public Sub Form_Load()
Dim I as Integer

For I = 1 to 2
set myCtl = Controls.Add("TextBox","txt"+CStr(I))
myCtl.Move 120, 360*I, 2295, 285
myCtl.Text = "txt"+CStr(I)
myCtl.Visible = True
Next I
End Sub

Private Sub myCtl_Click()
MsgBox myCtl
End Sub

I'm facing the problem in writing event for each contols that is created dynamically. If you run the above code in Visual basic, you'll find two textboxes "txt1" and "txt2" created on the form. As you can see from the code I have written a code on the click event of my runtime control, but this code gets associated only with the last control that was created on the form.
How can I write event code for all dynamic controls without using Control Array? ( I want my form to be empty during design )

Any ideas???

Dynamic Controls & Events
hi

i hv a another question.

can create 10 textbox in runtime? ans: yes

now Can I access the Events,Properties & Methods in runtime after created those controls.? ans: yes but how?

anand

Events Of Dynamic Controls
Hi All,
I was wondering if someone could help me with this problem -
I wish to dynamically create Controls on a form. The number of Controls I need varies at each run. So i have created a control array With the Code

For X = 1 To N
Load ChannelSwitch1(X)
With ChannelSwitch1(X)
.Top = 3765
.Left = X * 950
.Height = 870
.Width = 855
End With
Next

My question is how do I specify events like 'on click' for each control created in the loop, given that the number of controls will vary? I am happy with having the same event for every control created within the loop.

Any help is appreciated,
Novice

Click Events And Dynamic Controls
I have a project where I need command buttons to be created dynamically at runtime. According to the Microsoft website I need to declare the variable for my dynamically-added control a VBControlExtender, and Use the extender to access the control late-bound.

This will work for a single button but not for an array or multiple buttons. Is There is another way to make this work using maybe other objects or Gif's or anything I would like to know?

Here is the test code I have used up to now:

Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton
Dim i As Integer
Dim n As Integer

Private Sub Command1_Click()

Licenses.Add "MSComctlLib.TreeCtrl"

' Dynamically add a TreeView control to the form.
Set ctlDynamic = Controls.Add("MSComctlLib.TreeCtrl", _
"myctl", Form1)
' set the location and size of the control.
ctlDynamic.Move 1, 1, 2500, 3500

' Add some nodes to the control.
For i = 1 To 10
ctlDynamic.object.nodes.Add Key:="Test" & Str(i), Text:="Test" _
& Str(i)
ctlDynamic.object.nodes.Add Relative:="Test" & Str(i), _
Relationship:=4, Text:="TestChild" & Str(i)
Next i

' Make the control visible.
ctlDynamic.Visible = True

' add a textbox
Set ctlText = Controls.Add("VB.TextBox", "ctlText1", Form1)
' Set the location and size of the textbox
ctlText.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
1, 2500, 100

' Change the backcolor.
ctlText.BackColor = vbYellow

' Make it visible
ctlText.Visible = True

' ReDim to match Record Count
'' ReDim ctlCommand(adoBeer.Recordset.RecordCount)

' Add a CommandButton.
Set ctlCommand = Controls.Add("VB.CommandButton", _
"ctlCommand1", Form1)

' Set the location and size of the CommandButton.
ctlCommand.Move (ctlDynamic.Left + ctlDynamic.Width + 50), _
ctlText.Height + 50, 1500, 500

' Set the caption
ctlCommand.Caption = "Click Me"

' Make it visible
ctlCommand.Visible = True

---------------------------------------------------------------------------------

This is the code that does not work using an array using the same code from above but adding the array to the ctlCommand


' test
For n = 0 To UBound(ctlCommand) - 1
With ctlCommand(n)
.Visible = True
.Caption = adoProducts.Recordset.Fields(3).Value
.Top = 720 * n
.Left = 420
.Width = 1275
End With
adoProducts.Recordset.MoveNext

Next n
End Sub


Thanks!

Dynamic Controls - How Do I Access Their Events?
I would like to know how to access the events of a control that I have created dynamically. What I am trying to do is create a winsock control in a program that doesnt contain a form, just a module. Anyone know how to do this?

Handling Events For Dynamic Controls
I'm adding lot of control(sometimes thousand) to form at runtime.

Technique I adopted is created one control with Index=0 at Design Time. Then on runtime I kept Loading more control. But this is hogging the CPU.

If I do everything at runtime then its very fast. But I want to handle events for those controls. How to do that??

Hope my question is clear. At design time I don't know how many controls I'm gone create on runtime. So I can't write individual handlers for each control.

Is there some way to group all those control? Such that a click on one of them gives me the index of the member of the group??

regards
Mandeep

Adding Events To Dynamic Controls
Dear all,
I have added 2 text boxes on click event of a command button dynamicallyby the following piece of code. Now How do I add events to the Textboxes created dynamically,

Dim ctlText(2) As TextBox
Private Sub Command1_Click()
Set ctlText(0) = Controls.Add("VB.TextBox", "c1", frmMain)
ctlText(0).Visible = True
Call ctlText(0).Move(100, 100, 2700, 10)
ctlText(0).Appearance = 0
ctlText(0).MaxLength = 10



Set ctlText(1) = Controls.Add("VB.TextBox", "c2", frmMain)
ctlText(1).Visible = True
Call ctlText(1).Move(100, 600, 2700, 10)
ctlText(1).Appearance = 0
ctlText(1).MaxLength = 10
End Sub

Thanks in Advance,
Vinod.

Adding Dynamic Controls (Capturing Events)
I have the following code that adds a check box control to a form:


VB Code:
Dim WithEvents chkPercentNew as CheckBox Private Sub AddControl() Set chkPercentNew = Controls.Add("VB.CheckBox", "chkPercent")        With chkPercentNew            .Left = 175            .Top = 600            .Width = 255            .Caption = ""            .Height = 255            .Value = vbChecked            .Visible = True        End With End Sub


How do I capture the click event of this newly created control?

Thanks.

An Array Of Dynamic Controls
Hello all,

I've been reading tutorials on creating dynamic controls. However, I can't seem to find how to create a certain number of them based on a user's input. I think I was able to do this before using Load(), but now, I'm using a different method that uses Set and adding it to the controls collection.

I'm just a beginner, so I really don't know VB well. What I'm trying to do is to generate a number of text boxes based on how many of them a user specifies. Then, I'm going to take each value from the text box and manipulate the values.

Any simple examples? Tutorials? Tips?

Thanks!

CD

Events In Array Of Controls Added
Using this code in VBA (or VB with MsForms 2.0) (1 Form with 1 Multipage):

Code:
Dim ArrTextb(1 To 5) As MSForms.TextBox
Dim WithEvents mytb As MSForms.TextBox

Private Sub UserForm_Initialize()
MultiPage1.Pages.Clear
MultiPage1.Pages.Add
MultiPage1.Pages(0).Caption = "tab1"

For i = 1 To 5
Set ArrTextb(i) = MultiPage1.Pages(0).Controls.Add("Forms.textbox.1", _
"tbox" & i)
ArrTextb(i).Top = i * 20
ArrTextb(i).Left = 10
ArrTextb(i).Value = ArrTextb(i).Name
Next

Set mytb = MultiPage1.Pages(0).Controls.Add("Forms.textbox.1", _
"mytb")
mytb.Left = 100
mytb.Top = 20
mytb.Value = mytb.Name
End Sub

Private Sub mytb_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MsgBox "mytb_MouseUp"
End Sub
which is the way for managing the events of array of textboxes(ArrTextb)
(like mytb) because 'WithEvents' do not support array.
PS. I need Multipage control.
Actually i need an event that tell me when focus is on or out the textboxes!

Thanks.

Dynamic Creation Of Controls Array
Hello,
i dynamically create textbox controls array from the class.
part of code here:

public sub CreateControls()
Dim y() As CheckBox
Dim z() As Label

For i = 0 To 10
ReDim Preserve y(i)
ReDim Preserve z(i)
Set y(i) = .Controls.Add("VB.CheckBox", "y" & i, frmMain)
Set z(i) = .Controls.Add("VB.Label", "z" & i, frmMain)

next

frmMain.Show
end sub

Wen i click on one of the controls i want to enter to his event (onClick)
How can i do that?
Thanks

Handle Events In A Class Of A Controls Array
Hi

I think I have been reading what I am going to ask but i don't remember where.

Look, i have a custom class to handle the events of all controls I have on a form for example, so for each control I crate a new instance of the class and assign that object.

The problem I have is when the object I am passing it part of a control array. The error I am getting that my object doesn't admit those set of events. So anybody could explain me how to make this, or giving the place where are this explained?

thanks very much

Add Dynamic Controls To Existing Control Array?
Is it possible to add dynamic controls to an existing control array?

I have 5 hard coded textboxes, and the ability to add dynamic ones
My control is is txtPath (0-4) and I really hate the way vb6 does the object name. It doesn't allow me to do:

Code:
Set ctlText = Controls.Add("VB.TextBox", "txtPaths(" & txtPaths.Count -1 & ")", SSTab1)


says that txtPaths(5) is not a legal name.

is there any way to do this as later in my code I do a loop based on the count of this control array and if there were any added I'd like to include them.

Dynamic Creation Of Controls And Events</title> <script Type="text/javascript"> <!-- Function PrintTags() { Var Curr=document.getElementById('vB_Editor_QR_textarea').value; Newval=curr.replace(/[highlight]/gi,&#
Hi All,

I was wondering if someone could help me with this problem -
I wish to dynamically create Combo boxes on a form. The form is called with the code:
Dim DlgPrj As DlgImportSettings
Set DlgPrj = New DlgImportSettings
DlgPrj.setControls ElementCollection
DlgPrj.Show vbModal

where DlgImportSettings is the form, and ElementCollection is a collection of strings which each correspond to a combo box. So, if there are 50 strings, there will be 50 combo boxes dynamically created.

The code below is the DlgImportSettings form:

Option Explicit
Private WithEvents btnObj As CommandButton
Private ElementCollection As Collection
Private WithEvents comboBx As comboBox

Public Sub setControls(ElCol As Collection)
Dim I As Integer
Dim length As Long
Dim combo As Collection
Set ElementCollection = ElCol

Debug.Print ElementCollection.Count
length = ElementCollection.Count

Set combo = New Collection
I = 1
While I <= ElementCollection.Count
combo.Add Controls.Add("VB.ComboBox", "comboBx" & I)
Debug.Print I
combo.Item(I).Visible = True
combo.Item(I).Left = 3000
combo.Item(I).Top = I * 800
I = I + 1
Wend
End Sub

At the moment, setControls(Collection) sets up the ComboBoxes dynamically, and they display correctly. My question is how do I specify events like 'on click' for each control created in the loop, given that the number of controls will vary? I am happy with having the same event for every control created within the loop.

Any help is appreciated,
Tristan

Dimensionalized Dynamic Array Copied To A New Array Is No Longer Dynamic
I have a user-defined object DS, with a child used-defined object l() which is a dynamic array.

At some stage, I ReDim DS.l(x) where x is any positive integer and use the array.

I later do Let tempDS = DS and try to use the tempDS object and its property .l() in the same way. I'm trying to subsequently ReDim tempDS.l(y) ... but it doesn't work because the tempDS.l object was created as tempDS.l(x), not tempDS.l()

How can I make tempDS.l be created as l() and not l(x) when created by copying DS.l(x) ?

Create Dynamic UserForm
Hi,

I'm looking for some help in creating a UserForm and the associated code in PowerPoint using VBA 6.3.

I want to make a macro that will count the number of slides in a slide show, then prompt the user with a UserForm to enter some text that will go on each slide. My reason for doing this is I need to number each slide as an appendix to a document, but some pages get skipped, ie slide four could be Appendix 7 and slide five could be appendix 9 etc. Please don't ask me to explain why this is the case.

Part 1
Counting the slides and taking text from the userform are no problem, however I would like to know if there is a way I can create a certain number of text boxes depending on the number of pages in the slide show. i.e. if the macro is run and there are 6 slides I want to create a form with six text boxes, if the macro is run and there are 9 slides I want to create a form with 9 text boxes.

Part 2
Whether I'm successful with Part 1 or not, at present when working with code like this I write something to the effect of

Something.Somethingelse1 = TextBox1
Something.Somethingelse2 = TextBox2

There must be a way to use a for loop to say

for i=1 to 10
Something.Somethingelse"i" = TextBox"i"
next i

But I don't know the proper way to do this either.


Any help is appreciated

Matt

Handling Parent Userform Events From Within VB 6.0 ActiveX Control
Hi there I have a slightly unusual requirement.

I have an ActiveX control coded in VB 6.0. The control needs to be capable of being sited within VB 6.0 Forms and Microsoft Office Userforms as well. I have disabled the close button when the control is running a method within a VB Form, but have not been able to do the same when the control is sited on a Userform.

The problem is that using my approach I need to declare a variable of the type of the parent object (which is a Userform in the case of Excel) using a Dim WithEvents statement so that I can handle the close button press event via a
Private Sub VARIABLE-NAME_QueryUnload(Cancel As Integer, UnloadMode As Integer) event handler. As stated in Excel the parent is of type Userform and I cannot declare a variable of this type within VB 6.0.

Is this impossible and can I do this a much simpler way (my humble apologies if this is as I expect the case). My existing code which works fine for Parent VB Forms is shown below:


Code:
Dim cancelObj As Object
Dim WithEvents cancelForm As Form

Private Sub UserControl_Show()
    Set cancelObj = UserControl.Extender.Parent
    ' MsgBox " Parent Type is " & TypeName(cancelObj)
             If TypeOf cancelObj Is Form Then
                If UserControl.Ambient.UserMode = True Then
                        Set cancelObj = Nothing
                        Set cancelForm = Parent
                End If
                MsgBox ("Interface Control sited on a form! " & Parent.Name)
        Else 'parent not of type Form
                If UserControl.Ambient.UserMode = True Then
                        Set cancelObj = Nothing
                        'Set cancelForm = Parent 'type mismatch
                End If
                MsgBox ("Warning Interface Control not sited on a form!")
       End If
End Sub

Private Sub cancelform_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        ' Parent Form window close event captured!
        Dim Msg As String
        If UnloadMode = 0 Then
                If m_MethodRunning = 1 Then
                        Msg = "You can't close this form at this time"
                        If MsgBox(Msg, vbOKOnly, cancelForm.Caption) = vbOK Then
                            Cancel = 1
                        End If
               Else
            Set cancelObj = Nothing
                        Set cancelForm = Nothing
            ' safe shutdown code here
                End If
        End If
End Sub


Thankyou for any time that you can spare.

Handling Parent Userform Events Within VB 6.0 ActiveX Control
Hi there I have a slightly unusual requirement.

I have an ActiveX control coded in VB 6.0. The control needs to be capable of being sited within VB 6.0 Forms and Microsoft Office Userforms as well. I have disabled the close button when the control is running a method within a VB Form, but have not been able to do the same when the control is sited on a Userform.

The problem is that using my approach I need to declare a variable of the type of the parent object (which is a Userform in the case of Excel) using a Dim WithEvents statement so that I can handle the close button press event via a
Private Sub VARIABLE-NAME_QueryUnload(Cancel As Integer, UnloadMode As Integer) event handler. As stated in Excel the parent is of type Userform and I cannot declare a variable of this type within VB 6.0.

Is this impossible and can I do this a much simpler way (my humble apologies if this is as I expect the case). My existing code which works fine for Parent VB Forms is shown below:



Code:

Dim cancelObj As Object
Dim WithEvents cancelForm As Form

Private Sub UserControl_Show()
Set cancelObj = UserControl.Extender.Parent
' MsgBox " Parent Type is " & TypeName(cancelObj)
If TypeOf cancelObj Is Form Then
If UserControl.Ambient.UserMode = True Then
Set cancelObj = Nothing
Set cancelForm = Parent
End If
MsgBox ("Interface Control sited on a form! " & Parent.Name)
Else 'parent not of type Form
If UserControl.Ambient.UserMode = True Then
Set cancelObj = Nothing
'Set cancelForm = Parent 'type mismatch
End If
MsgBox ("Warning Interface Control not sited on a form!")
End If
End Sub

Private Sub cancelform_QueryUnload(Cancel As Integer, UnloadMode As Integer)
' Parent Form window close event captured!
Dim Msg As String
If UnloadMode = 0 Then
If m_MethodRunning = 1 Then
Msg = "You can't close this form at this time"
If MsgBox(Msg, vbOKOnly, cancelForm.Caption) = vbOK Then
Cancel = 1
End If
Else
Set cancelObj = Nothing
Set cancelForm = Nothing
' safe shutdown code here
End If
End If
End Sub


Thankyou for any time that you can spare.

Embedded Controls Vs UserForm Controls
Hello All...

I am (almost done) building a database/spreadsheet program that I have been using a UserForm with controls to manipulate. I would now like to switch to using embedded controls on a sheet within the workbook. The first thing I have noticed in trying to do this is that the embedded controls...(so it seems) can't do anything with a cell range unless it is part of a "Sheets" reference???... even if I select the sheet itself I still can't refrence a cell within the active sheet unless I call the sheet name first. e.g:


Code:
Sheets("Sheet2").Select
[A1].Select

This code gets an object error if I use an embedded control
but works fine with a userform control.

However this works fine for both:


Code:
Sheets("Sheet2").Select
Sheets("Sheet2').[A1].Select

Any Ideas???

Dynamic Forms With Dynamic Controls
I have an mdi form with a couple of child forms on it. these children forms are pre loaded with controls with an index of 0. i create an instance of these children forms and load some controls at run time. in some cases (like a label control) the controls are not visible. and in another it gives run time error 340 (control array element 1 does not exist).

Hope someone can tell me what gives this kind of error?

TIA

ferdz

Word 97 - Events Of Controls Made With Controls.add
I'm looking for a way to trap the events of controls made with the controls.add-methode.

f.e. a textboxes made with controls.add ....

    Set Mycmd = Controls.Add("Forms.textbox.1")
    
    Mycmd.Left = 10: Mycmd.Top = 10
    Mycmd.Width = 90: Mycmd.Height = 15.75
    Mycmd.Text = "This is the content of the TextBox1"

    Set Mycmd = Controls.Add("Forms.textbox.1")
    
    Mycmd.Left = 10: Mycmd.Top = 20
    Mycmd.Width = 90: Mycmd.Height = 15.75
    Mycmd.Text = "This is the content of the TextBox2"


... creates two nice textboxes.
But I cannot trap the events of the textboxes ...


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Stop
End Sub


... because it does not recognise the object textbox1, textbox2 etc..

Anyone a idee how to solve this?

Please notice that I'm using VBA in Word 97
With greetings and thanks in advance for your reply,

Adrey



Edited by - Adrey on 4/22/2003 1:25:45 PM

Dynamic Form Events
I am able to generate a form dynamically inside of a class module. My problem is how do I actually get my object to respond to the dynamic forms OBJECT events. Here is my form code.

(form code)
----------------------------------------
Dim x As TipOfDay

Set x = New TipOfDay
x.GenForm = Me
x.GenerateForm
----------------------------------------
(end form code)

Now inside the class module I have declared all objects and they actually show up when I run the program. All of the dynamic form objects have been delared Public and withevents in the general section of the class module. My objects show up in the objects list, but when I click my command button "cmdTipOK" (during run time) it does not respond to its events.

During runtime when I click on "cmdTipOK" the cmdTipOK_Click event does not fire. I am tring to implement cmdTipOK_Click inside of my class module where the command button has been declared "withevents"

My quesion is, how can I get form objects which are dynamically created, on a dynamic form, inside of a class module, to respond to thier respective events. Below is the class module code.

(class module code)
----------------------------------------
Public WithEvents TipForm As Form
Public WithEvents cmdTipOk As VB.CommandButton
Private WithEvents dummy As Form

Public Sub GenerateForm()
Set cmdTipOk = TipForm.Controls.Add("VB.CommandButton", "cmdTipOk", TipForm)

With TipForm
.Caption = "Tip Of The Day"
.Left = 2310
.Width = 5535
.Height = 3690
.Visible = True
End With

With cmdTipOk
.Caption = "OK"
.Default = True
.Height = 375
.Left = 4080
.Top = 120
.Width = 1215
.Visible = True
End With

TipForm.Refresh
End Sub


' ****** THIS EVENT NEVER FIRES ************
Private Sub TipOk_Click()
MsgBox "Im Here"
End Sub


Public Property Let GenForm(ByRef F As Form)
Set dummy = F
End Property
----------------------------------------
(end class module code)

Any help is greatly appreciated. Thanks!

Dynamic Form Events
after adding the controls (a.e. textboxes, comboboxes, etc.) like this :

Code:
for each r in selection.rows
Set Mycmd = Frame1.Controls.Add("Forms.TextBox.1", "TextBox" & i, Visible)
Mycmd.Left = 50
Mycmd.top = 0 + (30 * i)
Mycmd.Width = 175
Mycmd.Height = 20
Next
how can i respond to events like a.e. KeyPress or Change ?

thx

Creating Dynamic Events
It is easy to dynamically add controls at runtime, and if know that this is going to occur, I can precreate events for them using the 'WithEvents' keyword. I have an application with permits the user to dynamically create up to five command buttons. The form on which these may be created contains the following in the declarations section:
Code:
Option Explicit

Private WithEvents btnButtonOne As CommandButton
Private WithEvents btnButtonTwo As CommandButton
Private WithEvents btnButtonThree As CommandButton
Private WithEvents btnButtonFour As CommandButton
Private WithEvents btnButtonFive As CommandButton
'code for actual creating the buttons not included
For each of these buttons, I have precoded a click event. All of this words splendidly.

My question is: If, rather than specifing a limit on the number of controls that can be dynamically created and thus permitting you the freedom of precreating their events, you allow the user to elect the number of controls that they wish to dynamically create, how can a like number of events be created dynamically as well?

If, as I suspect, you can't do that, then without knowing how many controls will be created, how do you write code that allows the controls to actually do something?

Events From Dynamic Object
Can anyone tell me how can I trap events from dynamic activex control object(object created using createobject() method) .

Thanks in advance

Nilanjan

Redim A Dynamic Array As A Dynamic Array
How can I redim a dynamic array as a dynamic array??



dim h()

some loop function

h() = split(string, chr(0))

redim h()

next

No Events For Dynamic Created Components
Hi there!

I hope you guys can help me out. I'm fetching database records with information about components to build a form on runtime. I build a function where you can trough a formname to, fieldname (I'm selecting this one with a case) a fieldtype (textbox/commandbutton/etc) and the position.

Oke, this works pretty fine exept, I can't handle the EVENTS of these components.

Here's the function:


Code:
Public Function CreateField(MyForm As Object, MyFieldname As String, MyFieldType As String, MyFieldTop As Long, MyFieldLeft As Long)
Dim obj As Object


'Set Field Type
Select Case MyFieldType

'Case textfield
Case "text":

'Create field object
Set obj = MyForm.Controls.Add("VB.textbox", MyFieldname)
obj.Visible = True
obj.Top = PixelsToTwipsY(MyFieldTop)
obj.Left = PixelsToTwipsX(MyFieldLeft)
obj.Height = PixelsToTwipsY(22)


'Case textarea
Case "textarea":

Dim bState As Boolean
bState = True
'Create field objec
Set obj = MyForm.Controls.Add("Project1.UserControl1", MyFieldname)

obj.Visible = True
'SendMessage obj.hwnd, EM_SETTARGETDEVICE, 0, CLng(IIf(bState, 0, 0))

'obj.MultiLine = True
'obj.ScrollBars = 3
obj.Top = PixelsToTwipsY(MyFieldTop)
obj.Left = PixelsToTwipsX(MyFieldLeft)

End Select

Please advice. Cheers.

Best regards,

Jeroen.

Resetting Userform Controls
Hello,

I am trying to figure out if there is a more efficient method of resetting the controls on a userform. My current method is to simply list in a sub each control on the userform and set their values to Empty or False. When needed, I'll call the sub. For example:


Code:
Sub ResetUserform()With Userform1
.Textbox1=Empty
.Listbox1=Empty
.OptionButton1=False
End WithEnd Sub
This works, but it is kind of cumbersome when you have a multipage userform with many controls.

Thanks!

Dynamic Creation Of Activex Object With Events...
Can i load dynamic ActiveX libraries, and create some component´s withevents?

TIA

Dynamic Picture Boxes And Click Events For Each
Hello everyone, I am fairly new to VB.Net and I've run into a problem in my application. I have code that dynamically loops through some image data and creates Picture Boxes of images that are placed onto my form. My problem is that I need each Picture Box to be "clickable" and the way I am currently doing this dynamically only the last picture box placed on the form listens to the Click event.

I appear to need something like ComboBox that can determine which item is clicked on by using that .SelectedIndex property. I've kind of tried using something like a Collection or an Array to place my Picture Box objects, but no luck yet.

This is a simplified code snippet for what I currently have...


Code:
For Each strFilename In dirPhotos.GetFiles(strDir)
pboxPhoto.Image = New Bitmap(strFilename) 'Dim-ed pboxPhoto prior
Controls.Add(pboxPhoto)
Next
I need each of these Picture Boxes to be clickable, please let me know what I need to do, thanks...

-Aaron

Variable Number Of Controls On A Userform
I'm trying to construct a userform that has fields which, when filled, form an equation.  Each field is a separate part of the equation and the number of parts is variable, thus I've had to write code creating the necessary number of controls for any given equation.  This I've written at the Userform_Initialize() event.

What I'd like to do is have the equation show up as each part is entered.  My problems is that I need to modify the equation each time one of the fields is changed.  I know how to use the TextBox_change() event for a control that is already on the Userform, but my problem is that at the time of initialization, I've created an array of controls (e.g. Equationparts(1 To 10)) and now I can't reference them in any way that's recognizable to form an event Sub (i.e. Sub Equationsparts(1)_Change() doesn't work).

I hope I'm making sense. . . .

Alternatively, I could create controls that aren't in an array, avoiding the problem of the parenthetical array-referencing which apparently doesn't work in defining events, but I don't know how to create a variable number of controls that are uniquely named.  Is there a way of creating objects that have a name that includes a variable (e.g. Set object"c" = Userform.Controls.Add("Forms.textbox.1") where "c" is an integer variable? In other words, can I write a For loop with a variable number of iterations, say i, and then have "i" somewhere in the name of a new object created within the loop?

Any help much appreciated!

Search String Among Userform's Controls
Hello everybody!
I have a userform which has 600 checkboxes on it (added at runtime) and I need to implement a search button to help the user reach the desired checkbox quicker then scrooling to it. I don't know how to search the input word through all my checkbox's caption. Anybody can help?
Thanks.
Alex.

How Can I Handle Events For Dynamic Components Added To A Vb Form?
How can I handle events for dynamic components added to a Vb form?

Script Control, Dynamic Control Creation, And Dynamic Control Events
Hey everyone.

I have been messing around with the Scripting Control and Dynamic Control Creation.. I think it's amazingly powerful to have a program that can be extended by an end user by using VB Scripting commands.  I'm actually developing a system that will allow scripts to be written, saved, and run on start-up of my program to create any additional forms or controls that end users have created so it is as if they have been there the whole time.

My problem is this:

I am creating controls as follows:

I Created a class ScriptingFunctions and added some functions to it, instantiate the class, and add the object to the ScriptControl so that i can have full control of the functions.

I run a function called AddObjectToForm(ByVal sFormName as string, byVal sObjectType as string, ByVal sObjectName as String).

It loops through vb.forms, finds the form with the name you sent, selects it, and does the following:


   frmForm.Controls.Add sObjectType, sObjectName


I then call, from script, a function already in the form called "GiveAccessTo" which adds the object with the form name as a prefix to the ScriptControl.. so if I just created a "Vb.CommandButton" with name "cmdNew", I can access it through the scripting control now using "frmFormcmdNew.Caption = "NewCaption""


My problem is, however....

How can I add code .. through the scripting control, for the events on the new control I just created?

I could see creating a new windowproc in the scripting control and doing a SetWindowLong w/ GWL_WNDPROC to subclass the window procedure, but I'm not sure what I would have to do with the function?  Should I have a MsScriptControlCTL.Module and add the function to that, and reference that function via AddressOf to get the pointer to that function?

Don't forget, I know when the program ends, all of this will go away, but Like I said, I plan to have the script saved so it will execute on startup.

I'd really appreciate any help anyone could give me on this.

Thanks a lot in advance! :)
--NipsMG

Populating UserForm With An Array
Hi All,

I'm populating Combobox1 on UserForm1 with an array: aMasterList. aMasterList contains names of other arrays. When I select an item from ComboBox1 (which is a name of an array) I want to populate ListBox1 with the items contained with the passed array name.


Here's what I thought should work.

This part works fine:

Private Sub UserForm_Initialize()

For i = 1 To UBound(aMasterList)
Me.ComboBox1.AddItem aMasterList(i)
Next i

End sub


This part don't work:

Private Sub ComboBox1_Click()

‘get array name from ComboBox and feed it to ListBox
‘.Value contains array name
For i = 1 To UBound(.Value)

‘add each item from array into listbox
Me.ListBox1.AddItem .Value(i)
Next i

The problem seems to be passing the array name as a variable. For instance, if I populate the array with range names instead of array names the following works

Msgbox range(.value).address

However, this does not work

Msgbox ubound(.value)


So I’m thinking that an array name cannot be passed as a variable. So, if you had the name of an array contained in a variable, any idea how to pass that information to the user form?

Thanks for your help,

David

Passing An Array To A Userform?
I have the following code in a userform "frmAddTypes":
Code:Option Explicit

Dim mTypes() As String

Public Property Get Types() As String
    Types() = mTypes()
End Property

Public Property Let Types(pTypeArray() As String)
    mTypes() = pTypeArray()
End Property


My calling procedure looks like this:
Code:Private Sub cmdAddNewType_Click()
    Dim TypeArray() As String
    
    Call GetSetTypes(TypeArray(), GetTypes)
    frmAddTypes.Types = TypeArray()
    
End Sub

I keep getting the error "Compile Error: Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray or an invalid set final parameter."

(btw- the actual grammar in this error message from MS is Shocking!)

Can someone help?

|
+--JDMils
|
+--VB6
+--VB Dot Net
|
+-- Navman GPS Forums @ http://forum.jdmils.com
|

Controls Without Events
Are there any controls having no events? If yes, how to control them?

R: Controls Events
You must put bifore the name of the form:
frmMyForm.MyControl_Click()
or
Public frm As MyForm
Frm = MyForm
frm.Mycontrol.Button_Click
hope this help
Ciao

-----Messaggio originale-----

Controls Events
Hello every body
I have a form with some controls. i have a reference set to a variable to each of the form elements. now can i use the variable_Click() to activate the event of the particular control in a module or class module.
can anyone help me. thanks in advance.

with regards
vignesh.( email@removed )

Events Of All Controls
How to handle messages comming on all the controls pasted on a single form?
For example, I paste few buttons, 2 or 3 text boxes etc. and whenever i click on any or these control, same function should be called.

Of course I can call the common code from all the routines separately but I want to execute a routine as a event-routine that is executed with the events of all the controls of the single form

Add Controls With Events At Runtime
I want to be able to add controls to an Excel worksheet and then interact with them at runtime. How can I do this?

Specifically, my application automatically queries a constantly changing Access database every five minutes. It then creates a row on the worksheet for each record in the query. Depending on the information in the database at the time, sometimes there are zero records...other times there are 50 or 60. What I want to be able to do is add a command button and label for each record/row on the Excel worksheet that I can use to trigger other actions. For example, when I click the command button associated with the record/row, a new worksheet will be created.

Any ideas or help would greatly appreciated! Thanks in advance!!

Link Events Between 2 Controls
Usually I don't post but I've had a hard time searching for this one...


I created my own UI buttons and effects using Frames that contain ImageBoxes (not a problem) and Lables, which are giving me problems. Basically, the user see the whole frame as the "button" but if their mouse is over the label text and they click the Frame_Click event doesn't fire, just the Label_Click.

I know I could rig this to work by calling Frame_Click in the Label_Click routine but it's more complex then that. I'm using some home-brewed "Hover Buttons" and the user needs to be able to drag/drop files and text to the buttons as well. To "rig" up all these needs for 10 buttons would result in a couple hundred lines of repetitive and non-productive code.

What I want to do is somehow defer all Label events to the Frame event handlers and I have no idea how to create this "link" between the two.

Any pointers would be great.

Thanks,
Shane

Creating Controls And Events
Hi all,
I have a question of creating controls from nothing. I mean... well, I've got this code:

Code:
Option Explicit

Private tn As Double
Private txt As TextBox

Private Sub Command1_Click()
Set txt = Controls.Add("VB.TextBox", "txt" & tn)
Set txt.Container = Form1
txt.Visible = True
txt.Text = "TextBox" & tn
tn = tn + 1
End Sub
My question is: how can I handle the events of those controls created?
Well, in this case there's only one control created but it's the same...

Thanks!

Events In ActiveX Controls
Will an ActiveX control wait for any code in an event handler to finish executing before processing the code after the RaiseEvent call?

Example:

VB Code:
'In the ActiveX control...   MsgBox "1"   RaiseEvent Call("2")   MsgBox "3"   '...rest of code 'In the calling applicationPublic Sub MyControl_Call(Msg As String)   MsgBox MsgEnd Sub


Will I get 1,2,3?

Turn Off Controls Events
I want to turn off the events for a text box, is this possibly? Basically I want to be able to do a:

txtbox.text = "hello"


and not have any of the textbox's events called (such as, the change event), then I want to turn the events for the textbox back on, and have it function normally.

Thanks,

Tony

Runtime Controls Events
does a control created at runtime can recieve key/mouse/timer
events? i tried creating routines at design time but nothing works.
here is the code:

Private Sub lblTest_Click(Index as Integer)
MsgBox Index
End Sub

this code doesn't displays the index number of the control but
when i clicked on the 2 original labels. the index is displayed.
what gives?

the code to create controls is:

Private Sub cmdCreate_Click()
Dim ind As Integer

Load lblTest(ind)

lblTest(ind).Top = lblTest(ind -1).Top + 500
lblTest(ind).Left = lblTest(ind -1).Left
lblTest(ind).Visible = True

End Sub

I'm stomp!

Lines As Controls With Events?
I'm looking to create line controls like those in Microsoft Access either in the Relationship building window, or the Create query window where you drag from a field in one table to a field in another table.

I need the line to have simple events, such as Click and RightClick so I can select a line, then right click to show a popup menu.

However, I'm a bit stuck...well, that's an understatment.  I just plain have no clue where to start.

Does anyone have any sample code or suggestions?

Controls Events Call
Hi

i have a subroutin that has a parameter with type of ListView.

Code:Public Sub lvAutosizeControl(lv As ListView)


I want call listview.ItemClick method in my subroutin. But i dont know what do i do.
Every body can help me?

How To Block Events In Vb6 For Controls
Dear Sir:

I've been trying to block any mouse event for textbox in vb6.
No any success. I'd really appreciate your help.

Sincerely,
Gagik Antonyan

Copyright © 2005-08 www.BigResource.com, All rights reserved