Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [VB.NET 2008] How to rise an event of an object in a form from another form

[VB.NET 2008] How to rise an event of an object in a form from another form

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharphardwarehelptutorial
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    Hello everyone, my application is a SmartDevice application and it runs on a device with Windows Embedded Compact 7 OS, so it relies on Compact Framwork 3.5. Here is my problem (simplified): I have two forms, in the first form there is a Textbox and when it gets focus the second form pops up (I use form2.ShowDialog()). The second form is a little numeric keypad that I use to write numbers in the textbox of the first form. This second form has also an OK button that close the form. Is there a way to rise the TextChanged event of the textbox in the first form from the click event of the OK button in the second form (the OK button click doesn't write anything in the textbox)? Thanks in advance.

    D 1 Reply Last reply
    0
    • S steve_9496613

      Hello everyone, my application is a SmartDevice application and it runs on a device with Windows Embedded Compact 7 OS, so it relies on Compact Framwork 3.5. Here is my problem (simplified): I have two forms, in the first form there is a Textbox and when it gets focus the second form pops up (I use form2.ShowDialog()). The second form is a little numeric keypad that I use to write numbers in the textbox of the first form. This second form has also an OK button that close the form. Is there a way to rise the TextChanged event of the textbox in the first form from the click event of the OK button in the second form (the OK button click doesn't write anything in the textbox)? Thanks in advance.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      No, because the OK button Click code should not know ANYTHING about the form that created and showed it. Since you're using ShowDialog in the parent form, you could just call the method that does whatever you have in the TextChanged event handler. You DID move your TextChanged event code to a seperate method, correct? Don't put your work code in the event handler. Put it in its own seperate method and call that from the TextChanged event handler code. That way you can call it fdrom anywhere else you need to in your code without having to worry about how you're needlessly going to trigger events.

      A guide to posting questions on CodeProject

      How to debug small programs
      Dave Kreskowiak

      S 1 Reply Last reply
      0
      • D Dave Kreskowiak

        No, because the OK button Click code should not know ANYTHING about the form that created and showed it. Since you're using ShowDialog in the parent form, you could just call the method that does whatever you have in the TextChanged event handler. You DID move your TextChanged event code to a seperate method, correct? Don't put your work code in the event handler. Put it in its own seperate method and call that from the TextChanged event handler code. That way you can call it fdrom anywhere else you need to in your code without having to worry about how you're needlessly going to trigger events.

        A guide to posting questions on CodeProject

        How to debug small programs
        Dave Kreskowiak

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        Thanks Dave, you have answered my question: I can't do what I wanted to do... I have not moved my TextChanged event code to a seperate method, but I have an alibi! In my application there are many textbox in different pages (UserControls) that call the same numeric keypad, so in the code in the keypad there is no reference to a specific method. The keypad just knows which textbox is the caller and it writes in it. It works but I wanted to improve it because the TextChanged event is raised anytime I write a digit from the keypad and not when I close the keypad clicking the OK button. I wanted to do the opposite. I can avoid the TextChanged event with a flag but to rise it by the OK button I have to use also this button to write something, for example I could write and delete a space. Thank you.

        D 1 Reply Last reply
        0
        • S steve_9496613

          Thanks Dave, you have answered my question: I can't do what I wanted to do... I have not moved my TextChanged event code to a seperate method, but I have an alibi! In my application there are many textbox in different pages (UserControls) that call the same numeric keypad, so in the code in the keypad there is no reference to a specific method. The keypad just knows which textbox is the caller and it writes in it. It works but I wanted to improve it because the TextChanged event is raised anytime I write a digit from the keypad and not when I close the keypad clicking the OK button. I wanted to do the opposite. I can avoid the TextChanged event with a flag but to rise it by the OK button I have to use also this button to write something, for example I could write and delete a space. Thank you.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          steve_9496613 wrote:

          I have not moved my TextChanged event code to a seperate method, but I have an alibi!

          No you don't. It doesn't matter at all what the situation is. Moving the code to a separate method is always better than leaving it the event handler method. The sole exception being events that expect a return value in the form of a modified event argument, such as FormClosing. If the keypad knows which control it's "typing" in that's OK and even useful in this case. The keypad doesn't necessarily need its own TextBox to type into, but does need some way of display the entered text without showing it in the target textbox. When OK is hit, the control then copies the text into the target TextBox thereby raising the TextChanged event in the target control only once.

          A guide to posting questions on CodeProject

          How to debug small programs
          Dave Kreskowiak

          S 1 Reply Last reply
          0
          • D Dave Kreskowiak

            steve_9496613 wrote:

            I have not moved my TextChanged event code to a seperate method, but I have an alibi!

            No you don't. It doesn't matter at all what the situation is. Moving the code to a separate method is always better than leaving it the event handler method. The sole exception being events that expect a return value in the form of a modified event argument, such as FormClosing. If the keypad knows which control it's "typing" in that's OK and even useful in this case. The keypad doesn't necessarily need its own TextBox to type into, but does need some way of display the entered text without showing it in the target textbox. When OK is hit, the control then copies the text into the target TextBox thereby raising the TextChanged event in the target control only once.

            A guide to posting questions on CodeProject

            How to debug small programs
            Dave Kreskowiak

            S Offline
            S Offline
            steve_9496613
            wrote on last edited by
            #5

            Dave Kreskowiak wrote:

            No you don't. It doesn't matter at all what the situation is. Moving the code to a separate method is always better than leaving it the event handler method. The sole exception being events that expect a return value in the form of a modified event argument, such as FormClosing.

            ...you make me feel guilty...! :sigh: I will keep in mind this rule but, please, can you explain me why it is always better? In my specific situation the code in the event handler is executed only when the event raises, I mean I don't need to execute that code in any other place (and I think this is a common situation). I could create a new method, cut the code in the event handler and past it in the method and then call the method in the event handler but what is the benefit?

            Dave Kreskowiak wrote:

            The keypad doesn't necessarily need its own TextBox to type into, but does need some way of display the entered text without showing it in the target textbox. When OK is hit, the control then copies the text into the target TextBox thereby raising the TextChanged event in the target control only once.

            You are right, this could be a good idea. I use this approach in a full alphanumeric keyboard that is too big to let the user see the field in witch he is typing but I had not thought of adopting this method for the numeric keypad. Thank you.

            D 1 Reply Last reply
            0
            • S steve_9496613

              Dave Kreskowiak wrote:

              No you don't. It doesn't matter at all what the situation is. Moving the code to a separate method is always better than leaving it the event handler method. The sole exception being events that expect a return value in the form of a modified event argument, such as FormClosing.

              ...you make me feel guilty...! :sigh: I will keep in mind this rule but, please, can you explain me why it is always better? In my specific situation the code in the event handler is executed only when the event raises, I mean I don't need to execute that code in any other place (and I think this is a common situation). I could create a new method, cut the code in the event handler and past it in the method and then call the method in the event handler but what is the benefit?

              Dave Kreskowiak wrote:

              The keypad doesn't necessarily need its own TextBox to type into, but does need some way of display the entered text without showing it in the target textbox. When OK is hit, the control then copies the text into the target TextBox thereby raising the TextChanged event in the target control only once.

              You are right, this could be a good idea. I use this approach in a full alphanumeric keyboard that is too big to let the user see the field in witch he is typing but I had not thought of adopting this method for the numeric keypad. Thank you.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              steve_9496613 wrote:

              what is the benefit?

              The benefit is code reuse. Both in this project and in future ones. It's far easier to just call a method passing in the data it needs instead of trying to figure out how to call it, with the data it needs, and you have to bundle that into the two parameters that an event handler needs, sender and eventargs.

              A guide to posting questions on CodeProject

              How to debug small programs
              Dave Kreskowiak

              S 1 Reply Last reply
              0
              • D Dave Kreskowiak

                steve_9496613 wrote:

                what is the benefit?

                The benefit is code reuse. Both in this project and in future ones. It's far easier to just call a method passing in the data it needs instead of trying to figure out how to call it, with the data it needs, and you have to bundle that into the two parameters that an event handler needs, sender and eventargs.

                A guide to posting questions on CodeProject

                How to debug small programs
                Dave Kreskowiak

                S Offline
                S Offline
                steve_9496613
                wrote on last edited by
                #7

                OK, understood. Thanks

                D 1 Reply Last reply
                0
                • S steve_9496613

                  OK, understood. Thanks

                  D Offline
                  D Offline
                  DarkChuky CR
                  wrote on last edited by
                  #8

                  mmm I think you can do what you need if:

                  TextBoxOnFocus()
                  {
                  if (NumericPadDialog.ShowDialog() == Response.OK)
                  {
                  TextBoxOnOK(); //this method should be the one you want to trigger when done
                  }
                  }

                  and in your NumericPadDialog you must make the OK button work as close (or hide) and return the Response.OK, probably this button already does the exit or hide, you just need to add the Response, just notice how to play with dialog Modal mode. Well that is the idea.

                  S 1 Reply Last reply
                  0
                  • D DarkChuky CR

                    mmm I think you can do what you need if:

                    TextBoxOnFocus()
                    {
                    if (NumericPadDialog.ShowDialog() == Response.OK)
                    {
                    TextBoxOnOK(); //this method should be the one you want to trigger when done
                    }
                    }

                    and in your NumericPadDialog you must make the OK button work as close (or hide) and return the Response.OK, probably this button already does the exit or hide, you just need to add the Response, just notice how to play with dialog Modal mode. Well that is the idea.

                    S Offline
                    S Offline
                    steve_9496613
                    wrote on last edited by
                    #9

                    Thanks for your reply. I use the TextBox OnFocus event to call a function that sets some variables used by the NumericPad to know who is the caller and then shows the NumericPad. This function is used by all the textbox in the application so it has no reference with the specific code executed in the TextChanged event of each textbox (that is different one from the other). For this reason I think that your solution is not suitable to my problem but thank you anyway for your efforts.

                    D 1 Reply Last reply
                    0
                    • S steve_9496613

                      Thanks for your reply. I use the TextBox OnFocus event to call a function that sets some variables used by the NumericPad to know who is the caller and then shows the NumericPad. This function is used by all the textbox in the application so it has no reference with the specific code executed in the TextChanged event of each textbox (that is different one from the other). For this reason I think that your solution is not suitable to my problem but thank you anyway for your efforts.

                      D Offline
                      D Offline
                      DarkChuky CR
                      wrote on last edited by
                      #10

                      Cool, I see... Then I think you must use a Delegate, then in your onFocus you assign the current delegate to the action of you textBox OK, and in the OK event of the NumericPad you call the delegate... of course in your delegate method you will need to handle the release of the Delegate after a click (be sure to release it, you don't want to have a different textbox to trigger the OK of another textbox) At this moment can not give you a code snipe, will try later...

                      S 1 Reply Last reply
                      0
                      • D DarkChuky CR

                        Cool, I see... Then I think you must use a Delegate, then in your onFocus you assign the current delegate to the action of you textBox OK, and in the OK event of the NumericPad you call the delegate... of course in your delegate method you will need to handle the release of the Delegate after a click (be sure to release it, you don't want to have a different textbox to trigger the OK of another textbox) At this moment can not give you a code snipe, will try later...

                        S Offline
                        S Offline
                        steve_9496613
                        wrote on last edited by
                        #11

                        Thank you. I'm not sure I understood well what you mean. I try to explain in more detail what I do. In a form, call it FormA, I have four TextBox and in the Load event of the form I assign a delegate to the GotFocus event of the TextBoxes:

                          AddHandler (AutoIncTB.GotFocus), AddressOf FormMain.TB\_GotFocus\_1\_MAX\_RIP
                          AddHandler (RipTB.GotFocus), AddressOf FormMain.TB\_GotFocus\_1\_MAX\_RIP
                          AddHandler (DuratFinaleHTB.GotFocus), AddressOf FormMain.TB\_GotFocus\_0\_23
                          AddHandler (DuratFinaleMTB.GotFocus), AddressOf FormMain.TB\_GotFocus\_0\_59
                        

                        In FormMain there are some functions TB_GotFocus_XXXX that assign different values to some variables and then show the NumericPad.

                        Public Sub TB_GotFocus_0_23(ByVal sender As System.Object, ByVal e As System.EventArgs)
                        CGlobali.ValoreCambiato = False
                        Ctrl(0) = sender
                        Ctrl(1) = 0
                        Ctrl(2) = 23
                        PhBt.Focus()
                        Tastiera.ShowDialog() 'NumericPad
                        End Sub

                        The NumericPad "reads" the variables and understands what is the TextBox from which it was called and other things. Using the NumericPad I can write in the TextBox. Each TextBox has its own TextChanged event that is fired any time I write a digit, not when I push the OK button unless I write something also with the OK button (and this is what I do in this moment... but I could also use a flag... mmmm....). You talk about textbox OK, did you mean the TextChanged event? I hope this can explain better what I'm doing.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups