Solved

How to tell when  UserControl has fulfilled it's purpose?

Posted on 2015-02-10
18
91 Views
Last Modified: 2015-02-10
The heading for this thread is pretty lame but I didn't know what else to say. With that said, I have created several UserControls that act in a similar fashion. They each consist of a Button and several textboxes. When the User clicks on a button, a series of instructions happen that end up populating the textboxes.

What I need to be able to do is tell when the processing of these UC's are finished. So I'm thinking that I need to do something with an event associated with the UC itself and not any of the controls that comprise the UC. I'm just not sure what I need to check for and what event to use. Hope that is a clear explanation.

Thanks!
0
Comment
Question by:BlakeMcKenna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +1
18 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 167 total points
ID: 40600867
So you have several UserControls that are very similar, and need to know when they are all "complete"?

This would be a good candidate for an Interface with a Custom Event and a Property:
Public Interface Instructions
    ReadOnly Property IsCompleted As Boolean
    Event Complete(ByVal sender As Control)
End Interface

Open in new window

You can have more parameters in the "Complete" event to pass out more information.  If they all pass out a variable number of strings you could make the second parameter a "List(Of String)"; or you could add a different kind of Property to it maybe.

Now make your UserControls Implement the Interface and raise the Event when appropriate:
Public Class ucInstructionsA
    Implements Instructions

    Private _Completed As Boolean = False
    Public Event Complete(sender As Control) Implements Instructions.Complete

    Public ReadOnly Property IsCompleted As Boolean Implements Instructions.IsCompleted
        Get
            Return _Completed
        End Get
    End Property

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Doesn't have to be in the Button click, 
        ' but from somewhere check the "state" of your UserControl
        ' and toggle the _Completed flag and Raise the Event:
        If True Then
            _Completed = True
            RaiseEvent Complete(Me)
        End If
    End Sub

End Class

Open in new window


Now over in the Form, you maintain a List of all the Instruction UserControls and check to see if they are all "Complete" whenever you receive an Event from one of them:
Public Class Form1

    Private InstructionSet As New List(Of Instructions)

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Here I've hard-coded them, but this could be dynamic in nature,
        ' either searching the form for all of the controls,
        ' or maybe if you create them at run-time:
        InstructionSet.Add(UcInstructionsA1)
        AddHandler UcInstructionsA1.Complete, AddressOf instr_Complete
        InstructionSet.Add(UcInstructionsB1)
        AddHandler UcInstructionsB1.Complete, AddressOf instr_Complete
        InstructionSet.Add(UcInstructionsC1)
        AddHandler UcInstructionsC1.Complete, AddressOf instr_Complete
    End Sub

    Private Sub instr_Complete(sender As Control)
        Dim AllCompleted As Boolean = InstructionSet.All(Function(x) x.IsCompleted)
        If AllCompleted Then
            MessageBox.Show("All Done!")
        End If
    End Sub

End Class

Open in new window

0
 

Author Comment

by:BlakeMcKenna
ID: 40600922
Mike,

All I need to do is check a value of a textbox control from within the UC. Forgive me but I'm not really understanding all that code...
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40600941
Okay.  I'll walk you through it.

Do you need to check the TextBox when the Button is clicked?

Do you have a fixed number of UserControls on your Form?...or are you adding them at run-time?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 12

Expert Comment

by:FarWest
ID: 40600982
Mike, you already directed the solution to the events area when you said
What I need to be able to do is tell when the processing of these UC's are finished. So I'm thinking that I need to do something with an event associated with the UC itself and not any of the controls that comprise the UC. I'm just not sure what I need to check for and what event to use. Hope that is a clear explanation.
if you just want to check value in any UC, you just need a public property in the UC you have, or maybe making the required control as public member in the UC to be accessed as MyUC1Instance.textbox1.value
0
 

Author Comment

by:BlakeMcKenna
ID: 40600989
I have 3 UC's on my Windows Form. They are placed there in design mode. When I click the Command Button (there is 1 Button per UC), a series of calculations are performed and then a value is derived and inserted into one of two TextBox controls within the UC. So, I need to be able to check that TextBox value from the Windows Form so I can move focus to another UC. Since my knowledge is limited  on this subject, I'm really bad at explaining what I need. I hope I've explained it enough... :)
0
 
LVL 34

Assisted Solution

by:it_saige
it_saige earned 167 total points
ID: 40601015
So following Mike's advice, you would fire an event when the calculations are performed.  I am assuming that you are passing focus because each UC is either:
A.  Dependent upon the calculation(s) performed by the previous UC(s).
B.  Independent of the calculation(s) performed by the previous UC(s) [which implies that the main form is somehow dependent on all calculations being performed in the UC(s)]

If the UC's are dependent on each other then they would listen for the event to be fired.  When fired they could focus themselves.

If the mainform is dependent, then it would listen for the event to be fired.  When fired it would focus on the next UC.

Which of these identify your scenario?

-saige-
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 40601042
The devil is in the details.  Are the three UserControls of the same TYPE?...and just three different instances?  Or have you designed three different kinds of UserControl and each one on your Form is a different type?

If they are all the same type then it is much easier to code (without an Interface).

If they are all different types then the Interface approach can be useful.
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40601051
mike you can either you use events (add your own or use an existing one) or make a loop with sleep timer to check UCs text boxes values
for first option if you don't want to get into user defined event complications you can make the control change Cursor  when it finished and check the CursorChanged event in your main form
0
 

Author Comment

by:BlakeMcKenna
ID: 40601087
OK,

Below is the shortened version of the actual sub-routine that populates the TextBox (txtExcitationBefore) that I need to check from my Windows Form (the same form on which the UC resides). At the end of the routine, I have this statement:

    Public Sub CheckExcitationValues()
        Try
            EH.ErrorMessage = String.Empty

            If CInt(cmbExcitation.Text.Length) > 0 Then
                Dim currentLimit As String = PSCurrentLimit                 ' Set the default current limit as 400 mA.

                frmCalibration_3.sExpectedExci = CShort(cmbExcitation.Text)

                If gblnExciBefore Then
                    GetExcitation(frmCalibration_3.sExpectedExci.ToString, sglExci, currentLimit)
                    If EH.ErrorMessage > String.Empty Then
                        GoTo ProcessMessage
                    End If

                    If sglExci <> UnrealExci Then
                        txtExcitationBefore.Text = GetDecimalPlaces(CStr(sglExci), 2) & " Vdc"                'TextBox that needs populated
                    End If

                    gblnExciBefore = False
                    cboPrintOnCert.Checked = True
                End If

            RaiseEvent ReceivedExcitation()             'What do I do with this statement?

ProcessMessage:

        Catch ex As Exception
            EH.ErrorMessage = "usrExcitationData/CheckExcitationValues() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try
    End Sub

Open in new window


At the beginning of the UC class, I have the following declaration:

Public Event ReceivedExcitation()

Open in new window


I don't know what to do from this point!
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40601106
try to use my suggested solution and use existing CursorChanged event
in the main form you just select this  UC event from events  list and put the code to change focus
0
 

Author Comment

by:BlakeMcKenna
ID: 40601149
Once the TextBox for this particular UC has a value, at that point, the Form needs check this TextBox and if it has a value, it then enables a GroupBox control on the Form.

FarWest, I tried using the CursorChanged Event and it didn't work. It fired way before it was suppose to. The code is below:

    Private Sub usrExcitationCreep_CursorChanged(sender As Object, e As EventArgs) Handles usrExcitationCreep.CursorChanged
        Try
            EH.ErrorMessage = String.Empty

            If usrExcitationCreep.txtExcitationBefore.Text.Length > 0 Then
                grpCreepTestParms.Enabled = True
            End If

        Catch ex As Exception
            EH.ErrorMessage = "frmCalibration_3/usrExcitationCreep_CursorChanged() - " & ex.Message & "...Contact Engineering!" & "~E"
        End Try

        EH.ProcessMessages(Me, sbr, EH.ErrorMessage)
    End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:FarWest
ID: 40601165
it is usual that some events fired on loading just like resize .. etc
but you included the textbox check so even if it is fired this not mean completed while length of textbox = 0
//
did you make the code that change the cursor when calculation finished inside the uc code?
0
 

Author Comment

by:BlakeMcKenna
ID: 40601202
I'm not sure what you mean! The code in the preceding post resides in the Form.
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40601233
I mean you should change the cursor of the UC inside the UC when it is finished, so the event will be forced to fire in the main form
0
 

Author Comment

by:BlakeMcKenna
ID: 40601298
When you say change the cursor...are you meaning take the Focus off the Button or the TextBox in the UC and if so, how do I do that?
0
 
LVL 12

Assisted Solution

by:FarWest
FarWest earned 166 total points
ID: 40601325
no I mean something like
Me.Cursor = Windows.Forms.Cursors.WaitCursor
0
 

Author Comment

by:BlakeMcKenna
ID: 40601339
I figured out the Event thing. Finally it makes sense to me now. Now I have everything working the way it needs to....
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 40601343
Thank you all for your input!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp.Net Session Question 2 47
Need to Interpret Json in Vb.net ( .net 3.5  framework) 11 50
Read oracle BLOB in vb.net 2 24
Adding Rows to Grid 4 32
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…
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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