Solved

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

Posted on 2015-02-10
18
87 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
  • 8
  • 6
  • 3
  • +1
18 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 167 total points
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
 
LVL 12

Expert Comment

by:FarWest
Comment Utility
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
Comment Utility
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 32

Assisted Solution

by:it_saige
it_saige earned 167 total points
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 12

Expert Comment

by:FarWest
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I'm not sure what you mean! The code in the preceding post resides in the Form.
0
 
LVL 12

Expert Comment

by:FarWest
Comment Utility
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
Comment Utility
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
Comment Utility
no I mean something like
Me.Cursor = Windows.Forms.Cursors.WaitCursor
0
 

Author Comment

by:BlakeMcKenna
Comment Utility
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
Comment Utility
Thank you all for your input!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

9 Experts available now in Live!

Get 1:1 Help Now