Solved

Assign Custom Tasks At Runtime

Posted on 2004-09-29
28
241 Views
Last Modified: 2010-05-18
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
Comment
Question by:maykut
  • 15
  • 13
28 Comments
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187135
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187161
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
 
LVL 1

Author Comment

by:maykut
ID: 12187222
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
 
LVL 1

Author Comment

by:maykut
ID: 12187226
similar to vb.net how you enter the code the user decides what happens.
0
 
LVL 1

Author Comment

by:maykut
ID: 12187233
also the textboxes are added at runtime to.
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187338
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187396
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
 
LVL 1

Author Comment

by:maykut
ID: 12187403
can you show me a example
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187466
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
 
LVL 1

Author Comment

by:maykut
ID: 12187491
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187508
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
 
LVL 1

Author Comment

by:maykut
ID: 12187517
can you give me an example. a sample is all I need.
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187528
ooops :'(, give me few minutes
0
 
LVL 4

Accepted Solution

by:
eozz_2000 earned 30 total points
ID: 12187597
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187605
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
 
LVL 1

Author Comment

by:maykut
ID: 12187615
ok thanks I'll try it out.
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12187641
Then... the answer is accepted) :)
0
 
LVL 1

Author Comment

by:maykut
ID: 12195778
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12197202
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
 
LVL 1

Author Comment

by:maykut
ID: 12197358
how do you point the button you want to the textbox you want?
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12200516
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
 
LVL 1

Author Comment

by:maykut
ID: 12222119
can you show me an example please.
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12222156
mmm... 30 points... :S
0
 
LVL 1

Author Comment

by:maykut
ID: 12222183
thats all I have. If I had more why not.
0
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12222253
:), 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
 
LVL 1

Author Comment

by:maykut
ID: 12222519
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
 
LVL 4

Expert Comment

by:eozz_2000
ID: 12223638
no, it doesn't matters... even you can handle events in other forms
0
 
LVL 1

Author Comment

by:maykut
ID: 12223650
Ok thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now