Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

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.
0
maykut
Asked:
maykut
  • 15
  • 13
1 Solution
 
eozz_2000Commented:
when you create the buttons you can add his click handler to add other controls in the place where you put your button:

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 :)
0
 
eozz_2000Commented:
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
0
 
maykutAuthor Commented:
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
maykutAuthor Commented:
similar to vb.net how you enter the code the user decides what happens.
0
 
maykutAuthor Commented:
also the textboxes are added at runtime to.
0
 
eozz_2000Commented:
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 :(
0
 
eozz_2000Commented:
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...
0
 
maykutAuthor Commented:
can you show me a example
0
 
eozz_2000Commented:
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.ToString
    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 :)
0
 
maykutAuthor Commented:
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.
0
 
eozz_2000Commented:
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.
0
 
maykutAuthor Commented:
can you give me an example. a sample is all I need.
0
 
eozz_2000Commented:
ooops :'(, give me few minutes
0
 
eozz_2000Commented:
I have created a simple class:

Public Class ownButton
    Inherits System.Windows.Forms.Button

    Private pCommandText As String

    Public Property CommandText() As String
        Get
            Return Me.pCommandText
        End Get
        Set(ByVal Value As String)
            Me.pCommandText = Value
        End Set
    End Property

    Public Sub New(ByVal CommandText As String, Optional ByVal Text As String = "Button")
        Me.pCommandText = CommandText
        Me.Text = Text
    End Sub
End Class

In my Form I put this:

    Private Sub frmTestOwnButton_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create the first button
        Dim btn As New ownButton("My Command", "Hi")
        btn.Location = New Point(15, 15)
        Me.Controls.Add(btn)
        AddHandler btn.Click, AddressOf Me.btn_Click
        'Create the second button
        Dim btn1 As New ownButton("Hello", "Other one")
        btn1.Location = New Point(50, 50)
        Me.Controls.Add(btn1)
        AddHandler btn1.Click, AddressOf Me.btn_Click
    End Sub

    Private Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
   'Here I handle all buttons, but the action depend on
   'the command of the Button
        Select Case CType(sender, ownButton).CommandText
            Case "My Command"
                MsgBox("This is my command")
            Case "Hello"
                MsgBox("This is the second button")
        End Select
    End Sub
0
 
eozz_2000Commented:
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
0
 
maykutAuthor Commented:
ok thanks I'll try it out.
0
 
eozz_2000Commented:
Then... the answer is accepted) :)
0
 
maykutAuthor Commented:
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.
0
 
eozz_2000Commented:
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
0
 
maykutAuthor Commented:
how do you point the button you want to the textbox you want?
0
 
eozz_2000Commented:
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.
0
 
maykutAuthor Commented:
can you show me an example please.
0
 
eozz_2000Commented:
mmm... 30 points... :S
0
 
maykutAuthor Commented:
thats all I have. If I had more why not.
0
 
eozz_2000Commented:
:), 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).CommandText
    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
0
 
maykutAuthor Commented:
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
0
 
eozz_2000Commented:
no, it doesn't matters... even you can handle events in other forms
0
 
maykutAuthor Commented:
Ok thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 15
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now