• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 106
  • Last Modified:

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

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
BlakeMcKenna
Asked:
BlakeMcKenna
  • 8
  • 6
  • 3
  • +1
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
BlakeMcKennaAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
FarWestCommented:
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
 
BlakeMcKennaAuthor Commented:
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
 
it_saigeDeveloperCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
FarWestCommented:
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
 
BlakeMcKennaAuthor Commented:
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
 
FarWestCommented:
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
 
BlakeMcKennaAuthor Commented:
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
 
FarWestCommented:
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
 
BlakeMcKennaAuthor Commented:
I'm not sure what you mean! The code in the preceding post resides in the Form.
0
 
FarWestCommented:
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
 
BlakeMcKennaAuthor Commented:
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
 
FarWestCommented:
no I mean something like
Me.Cursor = Windows.Forms.Cursors.WaitCursor
0
 
BlakeMcKennaAuthor Commented:
I figured out the Event thing. Finally it makes sense to me now. Now I have everything working the way it needs to....
0
 
BlakeMcKennaAuthor Commented:
Thank you all for your input!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 8
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now