[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Assign Custom Tasks At Runtime

Posted on 2004-09-29
28
Medium Priority
?
248 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 120 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
 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 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