?
Solved

How can I program the leave event when using code generated array of controls.

Posted on 2011-10-09
10
Medium Priority
?
356 Views
Last Modified: 2012-05-12
I am fairly new to VB.net express 2010 and would like help with problem. I have a form that is populated with one line of controls at a time, there is a maximum of twenty lines. Each line consists of text boxes and list boxes. What I want to do is update the text in one of the textbox's when one of the others has lost focus. For example looking at line #1.
controlArray(2,0) is a 'Quantity' textbox
controlArray(5,0) is a 'Price Unit' textbox, when this text box looses focus update...
controlArray(6,0)  'Extended Price' textbox.
I have seen some addHandler examples but cant get the syntax right with my example.
0
Comment
Question by:dearness
10 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36939627
If the controls are dynamically created then AddHandler is the way to go...

The problem is usually figuring out which control to update based on the "source" control of the event.  You can use the "sender" parameter in the event to get a reference to the control that fired the event.  Search your array and look for a match to figure out what Index it was.  Then you can use that index to figure out what other controls need to be updated.

Another approach is to use a naming convention for the dynamic controls that can be parsed to get the index of the source control.  Then you can build a string that holds the name of the other controls with the index appended and pass that to Controls.Find() to get a reference to them.
0
 
LVL 15

Expert Comment

by:x77
ID: 36939672
The AddHandler syntax is easy, but each event has own definition and its dificult to remember it.

  1 - I add a private var to get VbIde assist me:

  Private WithEvents QuantityTb As TextBox

  2 - I Select my created var on Upper-Left  Selection combo in Code Pane.
  3 - I select each event I want Manage on Upper-Right Selection combo In Code Pane

Now I have each Method I need to Control Events.
Now I Remove The Created Var - QuantityTb - and inspect list of Errors.

Go each error, Cut (Send to Clipboard ) the "Handles ...., Paste it on Notepad ...

Now I go to zone where I need include AddHandler and Write:

    AddHandler  - Paste from Notepad Line-  , addressof   - Paste from Notepad Line and replace  "." to "_"


0
 

Author Comment

by:dearness
ID: 36939740
Ilde Mind,
Thanks for you suggestions. AddHandler is new to me, trying examples from google but no luck yet.
I was hopeing this would work
AddHandler controlArray(5, i).leave, AddressOf updateExtPrice
I
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36939754
It will work...can you show us more code please?
0
 

Author Comment

by:dearness
ID: 36939778
I get a syntax error that 'leave' is not an event of 'object
'
Sub updateExtPrice()
        MsgBox("Event was captuured")
    End Sub

    Sub addLine(ByVal lineNumber As Integer)
        For i = 0 To controlArray.GetUpperBound(0) - 1
            Me.Controls.Add(controlArray(i, lineNumber - 1))

            'Leave is not an event of Object
            AddHandler controlArray(5, i).leave, AddressOf updateExtPrice

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36939786
Your error, 'leave' is not an event of Object, is generated because your Array is declared as type Object.

Change it so that it is of the type of control in your Array...such as TextBox.
*You may be able to get away with using the base Type "Control".
0
 

Author Comment

by:dearness
ID: 36939790
I have included the array configuration code
'setup array of controls
        For i = 0 To 19
            controlArray(0, i) = New TextBox
            controlArray(1, i) = New TextBox
            controlArray(2, i) = New TextBox
            controlArray(3, i) = New TextBox
            controlArray(4, i) = New ComboBox
            controlArray(5, i) = New TextBox
            controlArray(6, i) = New TextBox
        Next

Open in new window

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36939793
The declaration, not the assignments, needs to be a TextBox.

Something like:

    Private controlArray(,) As TextBox ' <-- Not "Object"
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36939807
Do you get some error message, or is it simply that the event is not called.

AddHandler implies that you have created a method with the right signature (list of parameters).

That means that in your code, AddHandler will work only if you have a method called updtateExtPrice that has an Object as its first parameter, and a second one that is an EventArgs.

Once you have associated any control with AddHandler, this event should be called everytime the cursor leaves the control.

Inside the event, you can identify use the calling control with code such as the following:
Private Sub TextBox1_Leave(sender As Object, e As System.EventArgs) Handles TextBox1.Leave

    Dim yourControl As Control = DirectCast(sender, Control)

    yourControl.Text = "your text"
    yourControl.BackColor = Color.AliceBlue
    '...

End Sub

Open in new window

If all of your controls are of the same type, say TextBoxes, you would be better to call DirectCast with the type of the control. That would enable you to use all the property of that type of controls:
Private Sub TextBox1_Leave(sender As Object, e As System.EventArgs) Handles TextBox1.Leave

    Dim yourControl As Control = DirectCast(sender, TextBox)

    yourControl.Text = "your text"
    yourControl.BackColor = Color.AliceBlue
    yourControl.MaxLength = 8  'This would not work in the first piece of code because Control does not have MaxLength
    '...

End Sub

Open in new window

If you have different types of controls using the event, and need to differentiate between their types in order to access specific properties of those instead of the common ones found on the Control type, you can use the TypeOf operator:
Private Sub TextBox1_Leave(sender As Object, e As System.EventArgs) Handles TextBox1.Leave

    Dim yourControl As Control = DirectCast(sender, Control)

    yourControl.Text = "your text"
    yourControl.BackColor = Color.AliceBlue
    '...
    
    If TypeOf sender Is TextBox Then
        DirectCast(sender, TextBox).MaxLength = 8
    Else
        If TypeOf sender Is Label Then
            DirectCast(sender, Label).TextAlign = ContentAlignment.MiddleCenter
        End If
    End If

End Sub

Open in new window

If that is not sufficient, post your updateExtPrice event procedure.
0
 

Author Comment

by:dearness
ID: 36939809
Idle Mind,
Thanks, that got rid of the syntax error and i now get an event firing, How ever I had to change the combobox in the array to a textbox. At least I am getting further a head.
Thanks for your time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

864 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