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. Storing Info Between Events

Storing Info Between Events

Scheduled Pinned Locked Moved C#
question
21 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.
  • A Offline
    A Offline
    ASPnoob
    wrote on last edited by
    #1

    Hi all, Suppose I have a button in a windows form app that when clicked just once causes a text box to display one message, and when clicked twice causes the text box to display a different message. I have tried to use an if statement like the following to record the number of mouse clicks but it's not working. int clickCount=1; if (clickCount < 2) { txtDisplay.Text = txtDisplay.Text + "Side1"; clickCount++; } else { txtDisplay.Text = txtDisplay.Text + "Side2"; } It appears that each time the mouse is clicked, clickCount is set to 1 instead of having the value from the previous click. How do I store a value from the previous click event and use it in the current event? Please point me in the right direction, thanks in advance.

    L L 2 Replies Last reply
    0
    • A ASPnoob

      Hi all, Suppose I have a button in a windows form app that when clicked just once causes a text box to display one message, and when clicked twice causes the text box to display a different message. I have tried to use an if statement like the following to record the number of mouse clicks but it's not working. int clickCount=1; if (clickCount < 2) { txtDisplay.Text = txtDisplay.Text + "Side1"; clickCount++; } else { txtDisplay.Text = txtDisplay.Text + "Side2"; } It appears that each time the mouse is clicked, clickCount is set to 1 instead of having the value from the previous click. How do I store a value from the previous click event and use it in the current event? Please point me in the right direction, thanks in advance.

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

      You need to make clickCount a static variable so it is set to 1 at the beginning of the application.

      speaking as ...

      I 1 Reply Last reply
      0
      • L Lost User

        You need to make clickCount a static variable so it is set to 1 at the beginning of the application.

        speaking as ...

        I Offline
        I Offline
        ignrod
        wrote on last edited by
        #3

        Sorry to disagree with you, but this behavior of the static keyword corresponds to C++ and not C#. In fact, if you try to set a local variable ( i.e. defined inside a method ) as static in C#, you will obtain a compiler error. In my opinion, the best option to have clickCount record the number of clicks is to define it as a member variable and not a local one.

        L D 2 Replies Last reply
        0
        • I ignrod

          Sorry to disagree with you, but this behavior of the static keyword corresponds to C++ and not C#. In fact, if you try to set a local variable ( i.e. defined inside a method ) as static in C#, you will obtain a compiler error. In my opinion, the best option to have clickCount record the number of clicks is to define it as a member variable and not a local one.

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

          ignrod wrote:

          but this behavior of the static keyword corresponds to C++ and not C#.

          Sorry to disagree with you but I guess you have not read this[^].

          speaking as ...

          I 1 Reply Last reply
          0
          • I ignrod

            Sorry to disagree with you, but this behavior of the static keyword corresponds to C++ and not C#. In fact, if you try to set a local variable ( i.e. defined inside a method ) as static in C#, you will obtain a compiler error. In my opinion, the best option to have clickCount record the number of clicks is to define it as a member variable and not a local one.

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

            Sorry to disagree with you, but Richard is correct. Static variables work in C#.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            L 1 Reply Last reply
            0
            • A ASPnoob

              Hi all, Suppose I have a button in a windows form app that when clicked just once causes a text box to display one message, and when clicked twice causes the text box to display a different message. I have tried to use an if statement like the following to record the number of mouse clicks but it's not working. int clickCount=1; if (clickCount < 2) { txtDisplay.Text = txtDisplay.Text + "Side1"; clickCount++; } else { txtDisplay.Text = txtDisplay.Text + "Side2"; } It appears that each time the mouse is clicked, clickCount is set to 1 instead of having the value from the previous click. How do I store a value from the previous click event and use it in the current event? Please point me in the right direction, thanks in advance.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Your code seems to suggest that you are maintaining the state of your app in a variable (clickCount) which is declared inside the button Click handler, which means it won't survive the handling of a click, hence it will always restart having the value of 1. You should move the declaration outside the method, i.e. make it a class member. That way, for each instance of the Form (assuming you might create more than one of them), the button would remember its state. You do not need the static keyword, unless you want to have all instances of that Form to share their state (which seems unlikely to me). IMO it is easier and robuster to put the state inside the button itself; there are several ways to achieve this; - one is to create your own MyButton class, inheriting from Button, and taking care of its peculiarities (IMO it would be overkill here); - another is to hide the state in the Tag property, which is a field provided but unused by Windows for all WinForms Controls, so it is available for whatever purpose you decide to use it for. Mind you, it is of type object, so you would need some casting. (I prefer this approach). - and then you could derive the state from existing variables and/or properties; in your example, you could perhaps just check the txtDisplay.Text and when it ends on "Side1" just replace "Side1" by "Side2". (I typically don't do it this way, as it is less robust; e.g. when you change languages, there is more code that needs changing). :)

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              A 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Sorry to disagree with you, but Richard is correct. Static variables work in C#.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Huh? when I try a static inside a method, I do get a compiler error (tried with .NET 2.0) It is the C++ doc, and not the C# doc (unfortunately), that says: When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function. :confused:

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                L D 2 Replies Last reply
                0
                • L Lost User

                  ignrod wrote:

                  but this behavior of the static keyword corresponds to C++ and not C#.

                  Sorry to disagree with you but I guess you have not read this[^].

                  speaking as ...

                  I Offline
                  I Offline
                  ignrod
                  wrote on last edited by
                  #8

                  In fact, I have. If you have a look at the link you just posted, you will see a note in yellow color which says: "The static keyword has more limited uses than in C++. To compare with the C++ keyword, see Static (C++)." So, if you try the following code, it will not compile in C#

                  class ClickCount {

                  static void Main() {
                  	ClickCount click = new ClickCount();
                  	click.IncreaseClickCountByOne();
                  	click.IncreaseClickCountByOne();
                  	click.IncreaseClickCountByOne();
                  }
                  
                  void IncreaseClickCountByOne() {
                  	static int clickCount = 1;
                  	clickCount = clickCount + 1;
                  	Console.WriteLine(clickCount);
                  }
                  

                  }

                  Of course, member variables ( fields ) can be static or nonstatic. Any of them will keep their value between method calls.

                  class ClickCount {

                  static int clickCount = 1;
                  
                  static void Main() {
                  	ClickCount click = new ClickCount();
                  	click.IncreaseClickCountByOne();
                  	click.IncreaseClickCountByOne();
                  	click.IncreaseClickCountByOne();
                  }
                  
                  void IncreaseClickCountByOne() {
                  	clickCount = clickCount + 1;
                  	Console.WriteLine(clickCount);
                  }
                  

                  }

                  class ClickCount {

                  int clickCount = 1;
                  
                  static void Main() {
                  	ClickCount click = new ClickCount();
                  	click.IncreaseClickCountByOne();
                  	click.IncreaseClickCountByOne();
                  	click.IncreaseClickCountByOne();
                  }
                  
                  void IncreaseClickCountByOne() {
                  	clickCount = clickCount + 1;
                  	Console.WriteLine(clickCount);
                  }
                  

                  }

                  Both work exactly in the same way.

                  L 1 Reply Last reply
                  0
                  • I ignrod

                    In fact, I have. If you have a look at the link you just posted, you will see a note in yellow color which says: "The static keyword has more limited uses than in C++. To compare with the C++ keyword, see Static (C++)." So, if you try the following code, it will not compile in C#

                    class ClickCount {

                    static void Main() {
                    	ClickCount click = new ClickCount();
                    	click.IncreaseClickCountByOne();
                    	click.IncreaseClickCountByOne();
                    	click.IncreaseClickCountByOne();
                    }
                    
                    void IncreaseClickCountByOne() {
                    	static int clickCount = 1;
                    	clickCount = clickCount + 1;
                    	Console.WriteLine(clickCount);
                    }
                    

                    }

                    Of course, member variables ( fields ) can be static or nonstatic. Any of them will keep their value between method calls.

                    class ClickCount {

                    static int clickCount = 1;
                    
                    static void Main() {
                    	ClickCount click = new ClickCount();
                    	click.IncreaseClickCountByOne();
                    	click.IncreaseClickCountByOne();
                    	click.IncreaseClickCountByOne();
                    }
                    
                    void IncreaseClickCountByOne() {
                    	clickCount = clickCount + 1;
                    	Console.WriteLine(clickCount);
                    }
                    

                    }

                    class ClickCount {

                    int clickCount = 1;
                    
                    static void Main() {
                    	ClickCount click = new ClickCount();
                    	click.IncreaseClickCountByOne();
                    	click.IncreaseClickCountByOne();
                    	click.IncreaseClickCountByOne();
                    }
                    
                    void IncreaseClickCountByOne() {
                    	clickCount = clickCount + 1;
                    	Console.WriteLine(clickCount);
                    }
                    

                    }

                    Both work exactly in the same way.

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

                    Yes I agree, but that is not what I was suggesting.

                    speaking as ...

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Huh? when I try a static inside a method, I do get a compiler error (tried with .NET 2.0) It is the C++ doc, and not the C# doc (unfortunately), that says: When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function. :confused:

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                      Maybe I was not clear but I was talking about a static class variable.

                      speaking as ...

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        Maybe I was not clear but I was talking about a static class variable.

                        speaking as ...

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        As I can't get a local static to work in C# (as C/C++ would allow me to), I figured you meant a class member, but then I am puzzled as to why you want it to be static at all. And that is why I provided another answer to the OP, trying to state things more clearly. :)

                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                        L 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          As I can't get a local static to work in C# (as C/C++ would allow me to), I figured you meant a class member, but then I am puzzled as to why you want it to be static at all. And that is why I provided another answer to the OP, trying to state things more clearly. :)

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                          Luc Pattyn wrote:

                          I am puzzled as to why you want it to be static at all

                          My C++ brain again ...

                          speaking as ...

                          L 1 Reply Last reply
                          0
                          • L Lost User

                            Luc Pattyn wrote:

                            I am puzzled as to why you want it to be static at all

                            My C++ brain again ...

                            speaking as ...

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            you should kickstart the brain that fits the forum. :-D

                            Luc Pattyn [My Articles] Nil Volentibus Arduum

                            L 1 Reply Last reply
                            0
                            • L Luc Pattyn

                              you should kickstart the brain that fits the forum. :-D

                              Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                              Even with a kick it's slow to react. :(

                              speaking as ...

                              1 Reply Last reply
                              0
                              • L Luc Pattyn

                                Huh? when I try a static inside a method, I do get a compiler error (tried with .NET 2.0) It is the C++ doc, and not the C# doc (unfortunately), that says: When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function. :confused:

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

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

                                Same problem. I should have specified class scope statics.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak

                                1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  Your code seems to suggest that you are maintaining the state of your app in a variable (clickCount) which is declared inside the button Click handler, which means it won't survive the handling of a click, hence it will always restart having the value of 1. You should move the declaration outside the method, i.e. make it a class member. That way, for each instance of the Form (assuming you might create more than one of them), the button would remember its state. You do not need the static keyword, unless you want to have all instances of that Form to share their state (which seems unlikely to me). IMO it is easier and robuster to put the state inside the button itself; there are several ways to achieve this; - one is to create your own MyButton class, inheriting from Button, and taking care of its peculiarities (IMO it would be overkill here); - another is to hide the state in the Tag property, which is a field provided but unused by Windows for all WinForms Controls, so it is available for whatever purpose you decide to use it for. Mind you, it is of type object, so you would need some casting. (I prefer this approach). - and then you could derive the state from existing variables and/or properties; in your example, you could perhaps just check the txtDisplay.Text and when it ends on "Side1" just replace "Side1" by "Side2". (I typically don't do it this way, as it is less robust; e.g. when you change languages, there is more code that needs changing). :)

                                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                                  A Offline
                                  A Offline
                                  ASPnoob
                                  wrote on last edited by
                                  #16

                                  Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.

                                  D L I 5 Replies Last reply
                                  0
                                  • A ASPnoob

                                    Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.

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

                                    What's so hard about it??

                                    SomeButton.Tag = _stateValue_;
                                    

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak

                                    1 Reply Last reply
                                    0
                                    • A ASPnoob

                                      Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #18

                                      And the harder half of the job is getting the state back, which requires a cast as I said:

                                      int myState=(int)myControl.Tag;

                                      And no, I don't have a reference about this subject. Inheriting from a class, or creating a more specialized one, is a fundamental concept in Object-Oriented Programming. There are zillions of articles on the subject, why not try and read Custom Button - Issues with Focus Border, Text Color, etc.[^] (I didn't, it was the first I found using CP search). :)

                                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                                      1 Reply Last reply
                                      0
                                      • A ASPnoob

                                        Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.

                                        L Offline
                                        L Offline
                                        Luc Pattyn
                                        wrote on last edited by
                                        #19

                                        (Sorry for a garbage sentence meant to fool the CP bugs and cache issues so I can get this posted) And the harder half of the job is getting the state back, which requires a cast as I said:

                                        int myState=(int)myControl.Tag;

                                        And no, I don't have a reference about this subject. Inheriting from a class, or creating a more specialized one, is a fundamental concept in Object-Oriented Programming. There are zillions of articles on the subject, why not try and read Custom Button - Issues with Focus Border, Text Color, etc.[^] (I didn't, it was the first I found using CP search). :)

                                        Luc Pattyn [My Articles] Nil Volentibus Arduum

                                        1 Reply Last reply
                                        0
                                        • A ASPnoob

                                          Hi Luc, thanks for replying. I have searched for a way to "put the state inside the button itself" like you have suggested but all I've found were maintaining session state for web apps and not windows form apps. I'm also interested in your suggestion to "hide the state in the Tag property" but I can't find an example of how to do that either. Please give me links to articles on how to do those things I've just mention if you can. Thanks again for your help.

                                          L Offline
                                          L Offline
                                          Luc Pattyn
                                          wrote on last edited by
                                          #20

                                          (Another garbage sentence was needed to get this post accepted amidst bugs and cache issues, sorry for that) And the harder half of the job is getting the state back, which requires a cast as I said:

                                          int myState=(int)myControl.Tag;

                                          And no, I don't have a reference about this subject. Inheriting from a class, or creating a more specialized one, is a fundamental concept in Object-Oriented Programming. There are zillions of articles on the subject, why not try and read Custom Button - Issues with Focus Border, Text Color, etc.[^] (I didn't, it was the first I found using CP search). :)

                                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                                          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