maykut
asked on
Assign Custom Tasks At Runtime
Hi
I've created a class to add controls onto a child form at runtime. I can add the controls fine with no problems. How can I assign cutom tasks to controls ie click on button2 to display text on textbox3.
I've created a class to add controls onto a child form at runtime. I can add the controls fine with no problems. How can I assign cutom tasks to controls ie click on button2 to display text on textbox3.
Something like this:
Dim frm As New frmChild()
Private Sub cmd_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim txt As New TextBox()
With txt
.Text = "Hello"
.Location = New Point(50, 50)
End With
Me.frm.Controls.Add(txt)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As New Button()
With cmd
.Text = "Add Textbox"
.Location = New Point(15, 15)
End With
With frm
frm.Controls.Add(cmd)
End With
AddHandler cmd.Click, AddressOf Me.cmd_Click
frm.show()
End Sub
The form frmParent (example) has Button1
Dim frm As New frmChild()
Private Sub cmd_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim txt As New TextBox()
With txt
.Text = "Hello"
.Location = New Point(50, 50)
End With
Me.frm.Controls.Add(txt)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As New Button()
With cmd
.Text = "Add Textbox"
.Location = New Point(15, 15)
End With
With frm
frm.Controls.Add(cmd)
End With
AddHandler cmd.Click, AddressOf Me.cmd_Click
frm.show()
End Sub
The form frmParent (example) has Button1
ASKER
ok cool. But what i would like to do is assign things like the following:
button1 prints out onto a textbox
button2 adds a new button
button3 creates a new database
button4 open database
etc
but it may be changed later to suit my needs.
button1 prints out onto a textbox
button2 adds a new button
button3 creates a new database
button4 open database
etc
but it may be changed later to suit my needs.
ASKER
similar to vb.net how you enter the code the user decides what happens.
ASKER
also the textboxes are added at runtime to.
remember that vb.net is an IDE that has compilator theory... well, at this moment, I can't imagine how to accomplish with your request :(
In your event handler you can put a Select Case statement to know what you want to do (all the possibilities previously set) and then you can let the user choose between the options that you have programed...
ASKER
can you show me a example
you can create an array of strings with your commands:
dim strCommands(9) as String 'With 10 commands
strCommands(0) = "Create a text box"
strCommands(1) = "Delete table tblCustomers"
strCommands(2) = "Draw a line"
...
you can create a listBox with the content of your Strings
after you create your button, create its event handler
AddHandler cmd.Click, AddressOf Me.cmd_Click
private sub cmd_Click(sender as object, e as eventArgs)
select case listBox.SelectedItem.ToStr ing
case "Create a text Box"
'Your code to create a text box
case "Delete table tblCustomers"
'Your code to empty the table
...
end sub
That's what I would do :)
dim strCommands(9) as String 'With 10 commands
strCommands(0) = "Create a text box"
strCommands(1) = "Delete table tblCustomers"
strCommands(2) = "Draw a line"
...
you can create a listBox with the content of your Strings
after you create your button, create its event handler
AddHandler cmd.Click, AddressOf Me.cmd_Click
private sub cmd_Click(sender as object, e as eventArgs)
select case listBox.SelectedItem.ToStr
case "Create a text Box"
'Your code to create a text box
case "Delete table tblCustomers"
'Your code to empty the table
...
end sub
That's what I would do :)
ASKER
ok thats good. I've got all the buttons and textboxes assigned to tags ie as I add button1 the tag is 1 and button2 tag is 2 and so depending on how many I want to add.
its great how you've put it into a listbox, but it doesn't say which button will use it. How can you assign a button to a particular task. You haven't told which button will use this function.
its great how you've put it into a listbox, but it doesn't say which button will use it. How can you assign a button to a particular task. You haven't told which button will use this function.
In my example, only one button make all the tasks depending of the option selected in the listBox, if you want to load the buttons at run time and set then a particular task you can create a new class that inherits from Button class, add a new public property like "ComandText" and ther you can set the command you would like the button to execute.
ASKER
can you give me an example. a sample is all I need.
ooops :'(, give me few minutes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Instead of MsgBox("This is my command") or the other MsgBox you can put the code you need to execute depending of the CommandText of your OwnButton
ASKER
ok thanks I'll try it out.
Then... the answer is accepted) :)
ASKER
just another question I also add textboxes and other controls at runtime too how do I link the buttons say to the textboxes is it the same way.
you only need an event handler when you create the textBox at runtime... like this:
Dim txt As New TextBox()
AddHandler txt.KeyDown, AddressOf Me.txt_KeyDown
and you have writed the following sub before:
Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
End Sub
Its practically the same thing
Dim txt As New TextBox()
AddHandler txt.KeyDown, AddressOf Me.txt_KeyDown
and you have writed the following sub before:
Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
End Sub
Its practically the same thing
ASKER
how do you point the button you want to the textbox you want?
in the code of your Select Case, you can add a Handler when you create your text box, then, when you press again the button check if your text box already exists in the form (with its name), thtat's all.
ASKER
can you show me an example please.
mmm... 30 points... :S
ASKER
thats all I have. If I had more why not.
:), well... when you create your button, and in the code of this button you create a new textbox... you can give it a name related to the button...
private sub btn_Click(sender as object, e as eventargs)
Select Case ctype(sender,ownButton).Co mmandText
Case "ExampleForYou"
dim txt as new TextBox()
with txt
.Name = "ExampleForYou"
.Location = new Point(20,20)
.Text = "ExampleForYou"
end with
end sub
And then to access to this textBox, all you must do is to iterate between the controls in your form:
dim ctl as control
for each ctl in Me.Controls
if typeof(ctl) is textBox then
if ctl.Name = "ExampleForYou" then
msgbox "This is the textBox that I am looking for"
end if
end if
next
private sub btn_Click(sender as object, e as eventargs)
Select Case ctype(sender,ownButton).Co
Case "ExampleForYou"
dim txt as new TextBox()
with txt
.Name = "ExampleForYou"
.Location = new Point(20,20)
.Text = "ExampleForYou"
end with
end sub
And then to access to this textBox, all you must do is to iterate between the controls in your form:
dim ctl as control
for each ctl in Me.Controls
if typeof(ctl) is textBox then
if ctl.Name = "ExampleForYou" then
msgbox "This is the textBox that I am looking for"
end if
end if
next
ASKER
does it matter if I have the controls in seperate classes because I have Buttons in a class called ButtonArray and the Textboxes in a class called TexBoxArray
no, it doesn't matters... even you can handle events in other forms
ASKER
Ok thanks.
dim cmd as new Button()
...
object.controls.add(cmd)
addhandler cmd.click, addressof cmd_click
...
private sub cmd_click(sender as object, e as EventArgs)
'Here you can put your code to add the textboxes
end sub
I hope this could help you :)