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. C#
  4. User Control function C#

User Control function C#

Scheduled Pinned Locked Moved C#
helpcsharpquestion
24 Posts 5 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.
  • I Ismael_1999

    Hi, Ralf. What to do so when the user clicks on the Save buttons it first verify the fields? I mean: the verification must ocur just when the user clicks on the Save buttons. You know how to do this? Thanks.

    R Offline
    R Offline
    Ralf Meier
    wrote on last edited by
    #10

    Hi ... in my opinion it would be not so good to generally allow the using of the Save-Button if the state of the data-field is not checked. In my applications I prefered allways the way of "check input when changes" and give a valid-information (and allow further action). If you want to do it the other way round you should give an event to "outside" of your UserControl to do the check and the Check itself has to start (when all data is valid) perhaps a public method inside your UserControl which does the action (or raise another Event). What should happen if the Check isn't valid ? Who should give the Message to the User - the UserControl ? Or a Messagebox from the Ckeck-method ? You see : there are serverall ways to do this ...

    I 1 Reply Last reply
    0
    • I Ismael_1999

      Hi, Gerry. My idea was to create a validation function in the main program and when the user clicks on the Save buttons, before anything it calls that validation function. Do you know how to do this? Thanks.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #11

      "Save" is form-level function; primitive controls and UC's are bit players to the form. In the Save / button event (on the form), retrieve the "data object" (view model) which consolidates all the UI / form's input, validate it, then save the contents or display an error.

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      R 1 Reply Last reply
      0
      • L Lost User

        "Save" is form-level function; primitive controls and UC's are bit players to the form. In the Save / button event (on the form), retrieve the "data object" (view model) which consolidates all the UI / form's input, validate it, then save the contents or display an error.

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        R Offline
        R Offline
        Ralf Meier
        wrote on last edited by
        #12

        When reading this discussion my actual question is : why a UserControl ? For me a UC is an object which is used more than one time (in a different context). Basicly (so my opinion) we have a Data-Input object (Form) - so when I see it in OOP-context the question (for me) must be : which is my main-object and which are the sub-objects with which functionality ? This is also (or primary) a reply to the OP ...

        I L 2 Replies Last reply
        0
        • R Ralf Meier

          Hi ... in my opinion it would be not so good to generally allow the using of the Save-Button if the state of the data-field is not checked. In my applications I prefered allways the way of "check input when changes" and give a valid-information (and allow further action). If you want to do it the other way round you should give an event to "outside" of your UserControl to do the check and the Check itself has to start (when all data is valid) perhaps a public method inside your UserControl which does the action (or raise another Event). What should happen if the Check isn't valid ? Who should give the Message to the User - the UserControl ? Or a Messagebox from the Ckeck-method ? You see : there are serverall ways to do this ...

          I Offline
          I Offline
          Ismael_1999
          wrote on last edited by
          #13

          Hi, Ralf. I used your idea and it's working fine. The only (minor) problem is that the system can't show the user a message to tell him what to correct. Anyway, as I said, is a minor problem. If you have any sugestion about this, please let me know. And thank you for your help.

          R 1 Reply Last reply
          0
          • R Ralf Meier

            When reading this discussion my actual question is : why a UserControl ? For me a UC is an object which is used more than one time (in a different context). Basicly (so my opinion) we have a Data-Input object (Form) - so when I see it in OOP-context the question (for me) must be : which is my main-object and which are the sub-objects with which functionality ? This is also (or primary) a reply to the OP ...

            I Offline
            I Offline
            Ismael_1999
            wrote on last edited by
            #14

            Ralf. I'm developing a system with many forms and many of them use these buttons. And these forms have different fields in different amouunts.

            1 Reply Last reply
            0
            • I Ismael_1999

              Hi, Ralf. I used your idea and it's working fine. The only (minor) problem is that the system can't show the user a message to tell him what to correct. Anyway, as I said, is a minor problem. If you have any sugestion about this, please let me know. And thank you for your help.

              R Offline
              R Offline
              Ralf Meier
              wrote on last edited by
              #15

              Perhaps I have ... Give me some more details about your project ... Basicly the Textbox (or whereever you do the input) "knows" what kind of data is required. Here you can decide directly what is wrong ... But perhaps you write some more ...

              I 1 Reply Last reply
              0
              • R Ralf Meier

                When reading this discussion my actual question is : why a UserControl ? For me a UC is an object which is used more than one time (in a different context). Basicly (so my opinion) we have a Data-Input object (Form) - so when I see it in OOP-context the question (for me) must be : which is my main-object and which are the sub-objects with which functionality ? This is also (or primary) a reply to the OP ...

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #16

                Without seeing his "form", I can make few assumptions about the merits of his architecture. I have a "user control" that presents "books", complete with tables of contents, incremental searching, etc. This UC itself contains more UC's: virtual keyboard controls (single and split), content sizing sliders, navigators, etc. I'm trying to describe, in general terms, my "model" for "model <-> view"; UC's are fat controls. In "data object" terms, I sometimes put a public interface on the UC, and pass the whole thing around. "Live" if you will. All depends on how you build it ... and I "love" code-behind. My apps are also in production.

                It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                1 Reply Last reply
                0
                • R Ralf Meier

                  Perhaps I have ... Give me some more details about your project ... Basicly the Textbox (or whereever you do the input) "knows" what kind of data is required. Here you can decide directly what is wrong ... But perhaps you write some more ...

                  I Offline
                  I Offline
                  Ismael_1999
                  wrote on last edited by
                  #17

                  Hi, Ralf.
                  My system has a main form which is used as an MDI Parent. From it the user can access many other screens to control the system. Some of these screens make the registration of clients, suppliers, banks and others. In these registration screens, there are fields connected to columns in tables. When fields are mandatory, the system must verify what was written there. That’s my problem. At this moment this verification is working well, but it does not warn the user about a wrong entry or an empty field which should be filled. My User Control now has the following code:
                  private void SalvarButton_Click(object sender, EventArgs e)
                  {
                  if (PermSalvar)
                  {
                  ClickSalvar?.Invoke(this, e);
                  EmEdicao = false;
                  }
                  }
                  I created a variable (PermSalvar) which indicates whether the data can be saved. In the main program there is a routine named ValidaCampos which is called in the leave moment of the fields of interest, as you can see below.
                  private void ValidaCampos()
                  {
                  if (tb_Nome.Text == "")
                  botoes1.PermSalvar = false;
                  else
                  botoes1.PermSalvar = true;

                          if ((tb\_Id.Text == "") || (!int.TryParse(tb\_Id.Text, out int cp)))
                              botoes1.PermSalvar = false;
                          else
                              botoes1.PermSalvar = true;
                      }
                      private void tb\_Nome\_Leave(object sender, EventArgs e)
                      {
                          ValidaCampos();
                      }
                      private void tb\_Id\_Leave(object sender, EventArgs e)
                      {
                          ValidaCampos();
                      }
                  

                  So, if you could give me any advice, it would be very welcome.
                  Thank you.

                  L R 2 Replies Last reply
                  0
                  • I Ismael_1999

                    Hi, Ralf.
                    My system has a main form which is used as an MDI Parent. From it the user can access many other screens to control the system. Some of these screens make the registration of clients, suppliers, banks and others. In these registration screens, there are fields connected to columns in tables. When fields are mandatory, the system must verify what was written there. That’s my problem. At this moment this verification is working well, but it does not warn the user about a wrong entry or an empty field which should be filled. My User Control now has the following code:
                    private void SalvarButton_Click(object sender, EventArgs e)
                    {
                    if (PermSalvar)
                    {
                    ClickSalvar?.Invoke(this, e);
                    EmEdicao = false;
                    }
                    }
                    I created a variable (PermSalvar) which indicates whether the data can be saved. In the main program there is a routine named ValidaCampos which is called in the leave moment of the fields of interest, as you can see below.
                    private void ValidaCampos()
                    {
                    if (tb_Nome.Text == "")
                    botoes1.PermSalvar = false;
                    else
                    botoes1.PermSalvar = true;

                            if ((tb\_Id.Text == "") || (!int.TryParse(tb\_Id.Text, out int cp)))
                                botoes1.PermSalvar = false;
                            else
                                botoes1.PermSalvar = true;
                        }
                        private void tb\_Nome\_Leave(object sender, EventArgs e)
                        {
                            ValidaCampos();
                        }
                        private void tb\_Id\_Leave(object sender, EventArgs e)
                        {
                            ValidaCampos();
                        }
                    

                    So, if you could give me any advice, it would be very welcome.
                    Thank you.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #18

                    I question the validity of logic that flags an error (and continues) and then turns it off under another condition further on. "Initialize" once.

                            if (tb\_Nome.Text == "")
                                botoes1.PermSalvar = false;
                            else
                                botoes1.PermSalvar = true;
                    
                    
                            if ((tb\_Id.Text == "") || (!int.TryParse(tb\_Id.Text, out int cp)))
                                botoes1.PermSalvar = false;
                            else
                                botoes1.PermSalvar = true;
                    

                    It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                    I 1 Reply Last reply
                    0
                    • I Ismael_1999

                      Hi, Ralf.
                      My system has a main form which is used as an MDI Parent. From it the user can access many other screens to control the system. Some of these screens make the registration of clients, suppliers, banks and others. In these registration screens, there are fields connected to columns in tables. When fields are mandatory, the system must verify what was written there. That’s my problem. At this moment this verification is working well, but it does not warn the user about a wrong entry or an empty field which should be filled. My User Control now has the following code:
                      private void SalvarButton_Click(object sender, EventArgs e)
                      {
                      if (PermSalvar)
                      {
                      ClickSalvar?.Invoke(this, e);
                      EmEdicao = false;
                      }
                      }
                      I created a variable (PermSalvar) which indicates whether the data can be saved. In the main program there is a routine named ValidaCampos which is called in the leave moment of the fields of interest, as you can see below.
                      private void ValidaCampos()
                      {
                      if (tb_Nome.Text == "")
                      botoes1.PermSalvar = false;
                      else
                      botoes1.PermSalvar = true;

                              if ((tb\_Id.Text == "") || (!int.TryParse(tb\_Id.Text, out int cp)))
                                  botoes1.PermSalvar = false;
                              else
                                  botoes1.PermSalvar = true;
                          }
                          private void tb\_Nome\_Leave(object sender, EventArgs e)
                          {
                              ValidaCampos();
                          }
                          private void tb\_Id\_Leave(object sender, EventArgs e)
                          {
                              ValidaCampos();
                          }
                      

                      So, if you could give me any advice, it would be very welcome.
                      Thank you.

                      R Offline
                      R Offline
                      Ralf Meier
                      wrote on last edited by
                      #19

                      Some time ago I created an application with a similar behaviour (normally I create HMI-applications for machines). I found that the users sometimes entered senseless data into a Textbox. So I created (with the TextChanged-Event) a method to those Textboxes which data has a special format (for example : it must have a legal date or an Integer value or a Phone-number or ...) and give a User-message (MessageBox) to correct this. In the same time I disable the Save-Button. Perhaps this approach helps you ...

                      I 1 Reply Last reply
                      0
                      • L Lost User

                        I question the validity of logic that flags an error (and continues) and then turns it off under another condition further on. "Initialize" once.

                                if (tb\_Nome.Text == "")
                                    botoes1.PermSalvar = false;
                                else
                                    botoes1.PermSalvar = true;
                        
                        
                                if ((tb\_Id.Text == "") || (!int.TryParse(tb\_Id.Text, out int cp)))
                                    botoes1.PermSalvar = false;
                                else
                                    botoes1.PermSalvar = true;
                        

                        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

                        I Offline
                        I Offline
                        Ismael_1999
                        wrote on last edited by
                        #20

                        I test al fields that must be filled. In one case in this form, the contents must be integer. If all fields are correct the variable PermSalvar receives a true value. If at least one of them is not correctly filled, that variable (which is inside the User Control and has false as its default value) remains false. And the Save action only performs if the variable is true. Do you have any sugestion? Plase tell me. Thanks.

                        1 Reply Last reply
                        0
                        • R Ralf Meier

                          Some time ago I created an application with a similar behaviour (normally I create HMI-applications for machines). I found that the users sometimes entered senseless data into a Textbox. So I created (with the TextChanged-Event) a method to those Textboxes which data has a special format (for example : it must have a legal date or an Integer value or a Phone-number or ...) and give a User-message (MessageBox) to correct this. In the same time I disable the Save-Button. Perhaps this approach helps you ...

                          I Offline
                          I Offline
                          Ismael_1999
                          wrote on last edited by
                          #21

                          I tried your suggestion some time ago. It worked fine, but I could not prevent the buttons to change their enable status. That’s why I’m trying something different.
                          I continue accepting suggestions and thanks.

                          R 1 Reply Last reply
                          0
                          • I Ismael_1999

                            I tried your suggestion some time ago. It worked fine, but I could not prevent the buttons to change their enable status. That’s why I’m trying something different.
                            I continue accepting suggestions and thanks.

                            R Offline
                            R Offline
                            Ralf Meier
                            wrote on last edited by
                            #22

                            Why don't you can disable the Button ? Please explain ...

                            I 1 Reply Last reply
                            0
                            • R Ralf Meier

                              Why don't you can disable the Button ? Please explain ...

                              I Offline
                              I Offline
                              Ismael_1999
                              wrote on last edited by
                              #23

                              Enable and disable the buttons depend on which buttons was pressed. When the user enters the screen, the Save and Cancel buttons are disabled. The other 3 are enabled. When user presses New or Edit buttons, the enable state changes. It's programmed in the User Control, which is generic.

                              R 1 Reply Last reply
                              0
                              • I Ismael_1999

                                Enable and disable the buttons depend on which buttons was pressed. When the user enters the screen, the Save and Cancel buttons are disabled. The other 3 are enabled. When user presses New or Edit buttons, the enable state changes. It's programmed in the User Control, which is generic.

                                R Offline
                                R Offline
                                Ralf Meier
                                wrote on last edited by
                                #24

                                There is no contradiction betwenn my suggestion and your realisation ... If your UserControl has one (or more) Property, which gives this further information to it (for example "the entered data is not valid") you can add all the behaviour you want to have. If you want to work with an UserControl for those functionality it only depends on the "interface" to the rest of the application.

                                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