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. An odd predicament with windows forms

An odd predicament with windows forms

Scheduled Pinned Locked Moved C#
helpcsharpwinforms
11 Posts 6 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.
  • N Offline
    N Offline
    nik121
    wrote on last edited by
    #1

    Hello I'm new to C# so you might have to bear with me a little bit but here is my problem. I am trying to make a windows form so that when you press a button, all the buttons disappear from the form. When that happens I am trying to make a different button take its place. You might find this a strange thing to do... but I have my reasons ;P. This is as far as I have gotten. Making a new button... Button newButton = new Button(); (tell me if this is even works, because I'm not really all that sure if it does). And that's all I can come up with. I've tried really hard to make that button appear after the others ones are disposed and I THINK it might have something to do with newButton.Location.X/Y. But that's just a guess. So it would be greatly appreciated if anyone could offer me any help. I do not ask for code I simple ask for a little guidance to my next step. Thanks, Nik!

    L A RaviBeeR L 5 Replies Last reply
    0
    • N nik121

      Hello I'm new to C# so you might have to bear with me a little bit but here is my problem. I am trying to make a windows form so that when you press a button, all the buttons disappear from the form. When that happens I am trying to make a different button take its place. You might find this a strange thing to do... but I have my reasons ;P. This is as far as I have gotten. Making a new button... Button newButton = new Button(); (tell me if this is even works, because I'm not really all that sure if it does). And that's all I can come up with. I've tried really hard to make that button appear after the others ones are disposed and I THINK it might have something to do with newButton.Location.X/Y. But that's just a guess. So it would be greatly appreciated if anyone could offer me any help. I do not ask for code I simple ask for a little guidance to my next step. Thanks, Nik!

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

      After you create the button Button newButton = new Button() you have to set its x and y coordinates, width and height, and add it to the form: this.Controls.Add(newButton);

      N 1 Reply Last reply
      0
      • N nik121

        Hello I'm new to C# so you might have to bear with me a little bit but here is my problem. I am trying to make a windows form so that when you press a button, all the buttons disappear from the form. When that happens I am trying to make a different button take its place. You might find this a strange thing to do... but I have my reasons ;P. This is as far as I have gotten. Making a new button... Button newButton = new Button(); (tell me if this is even works, because I'm not really all that sure if it does). And that's all I can come up with. I've tried really hard to make that button appear after the others ones are disposed and I THINK it might have something to do with newButton.Location.X/Y. But that's just a guess. So it would be greatly appreciated if anyone could offer me any help. I do not ask for code I simple ask for a little guidance to my next step. Thanks, Nik!

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

        Maybe you should have a look at the code that the winforms designer generates - it explains how you'd have to do it manually. (expand the Form.cs file and open the Form.designer.cs file, then expand the "Windows Form Designer generated code" region)

        N 1 Reply Last reply
        0
        • L Lost User

          After you create the button Button newButton = new Button() you have to set its x and y coordinates, width and height, and add it to the form: this.Controls.Add(newButton);

          N Offline
          N Offline
          nik121
          wrote on last edited by
          #4

          Well I thank you greatly for that much of help and I must say that it helped but I am not sure how to set the X and Y variables. I have tried newButton.Location.X, but when I try setting it equal to an int it says that I can't modify the return variable of Location because it is not a variable. Do you know a way to go around it?

          L L 2 Replies Last reply
          0
          • L Lost User

            Maybe you should have a look at the code that the winforms designer generates - it explains how you'd have to do it manually. (expand the Form.cs file and open the Form.designer.cs file, then expand the "Windows Form Designer generated code" region)

            N Offline
            N Offline
            nik121
            wrote on last edited by
            #5

            Wow I can't believe I didn't think of that. Thank you very much you helped me greatly!!! :)

            L 1 Reply Last reply
            0
            • N nik121

              Well I thank you greatly for that much of help and I must say that it helped but I am not sure how to set the X and Y variables. I have tried newButton.Location.X, but when I try setting it equal to an int it says that I can't modify the return variable of Location because it is not a variable. Do you know a way to go around it?

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

              It probably returns a struct (Point), modifying a field of the struct would be kinda useless since as soon as you leave the current scope that struct will be gone (or even if isn't, it's not the same struct as the control has, but a copy) So, what should work: copy the Location to a local, change the X, assign it back to newButton.Location. hey, who downvoted this :| It would work. Nothing bad about it either.


              Last modified: 11hrs 5mins after originally posted --

              1 Reply Last reply
              0
              • N nik121

                Wow I can't believe I didn't think of that. Thank you very much you helped me greatly!!! :)

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

                Can't always think of everything.. you're welcome

                1 Reply Last reply
                0
                • N nik121

                  Well I thank you greatly for that much of help and I must say that it helped but I am not sure how to set the X and Y variables. I have tried newButton.Location.X, but when I try setting it equal to an int it says that I can't modify the return variable of Location because it is not a variable. Do you know a way to go around it?

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

                  Hi, to set the location of a Control do one of these:

                  myControl.Location=new Point(x,y);
                  myControl.Bounds=new Rectangle(x,y,width,height);

                  Both Location and Bounds are structs, i.e. value types; gettin such struct and modifying it won't cut it, you have to provide a struct with the new values. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  1 Reply Last reply
                  0
                  • N nik121

                    Hello I'm new to C# so you might have to bear with me a little bit but here is my problem. I am trying to make a windows form so that when you press a button, all the buttons disappear from the form. When that happens I am trying to make a different button take its place. You might find this a strange thing to do... but I have my reasons ;P. This is as far as I have gotten. Making a new button... Button newButton = new Button(); (tell me if this is even works, because I'm not really all that sure if it does). And that's all I can come up with. I've tried really hard to make that button appear after the others ones are disposed and I THINK it might have something to do with newButton.Location.X/Y. But that's just a guess. So it would be greatly appreciated if anyone could offer me any help. I do not ask for code I simple ask for a little guidance to my next step. Thanks, Nik!

                    A Offline
                    A Offline
                    amit_1986
                    wrote on last edited by
                    #9

                    When u drag and drop a button , set its x, y location to 0 , set height width to 0 .. what would happen ?? Thats your issue right now. Just set these parameters and it will show up.

                    1 Reply Last reply
                    0
                    • N nik121

                      Hello I'm new to C# so you might have to bear with me a little bit but here is my problem. I am trying to make a windows form so that when you press a button, all the buttons disappear from the form. When that happens I am trying to make a different button take its place. You might find this a strange thing to do... but I have my reasons ;P. This is as far as I have gotten. Making a new button... Button newButton = new Button(); (tell me if this is even works, because I'm not really all that sure if it does). And that's all I can come up with. I've tried really hard to make that button appear after the others ones are disposed and I THINK it might have something to do with newButton.Location.X/Y. But that's just a guess. So it would be greatly appreciated if anyone could offer me any help. I do not ask for code I simple ask for a little guidance to my next step. Thanks, Nik!

                      RaviBeeR Offline
                      RaviBeeR Offline
                      RaviBee
                      wrote on last edited by
                      #10

                      nik121 wrote:

                      When that happens I am trying to make a different button take its place.

                      Why not just add the "new" button at design time and set its visibility to hidden? When the time comes to "create" it, just show the button. That will save you having to set all its properties at run time. /ravi

                      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

                      1 Reply Last reply
                      0
                      • N nik121

                        Hello I'm new to C# so you might have to bear with me a little bit but here is my problem. I am trying to make a windows form so that when you press a button, all the buttons disappear from the form. When that happens I am trying to make a different button take its place. You might find this a strange thing to do... but I have my reasons ;P. This is as far as I have gotten. Making a new button... Button newButton = new Button(); (tell me if this is even works, because I'm not really all that sure if it does). And that's all I can come up with. I've tried really hard to make that button appear after the others ones are disposed and I THINK it might have something to do with newButton.Location.X/Y. But that's just a guess. So it would be greatly appreciated if anyone could offer me any help. I do not ask for code I simple ask for a little guidance to my next step. Thanks, Nik!

                        L Offline
                        L Offline
                        Lecutus1
                        wrote on last edited by
                        #11

                        nik121 wrote:

                        Hello I'm new to C# so you might have to bear with me a little bit but here is my problem. I am trying to make a windows form so that when you press a button, all the buttons disappear from the form. When that happens I am trying to make a different button take its place. You might find this a strange thing to do... but I have my reasons ;P. This is as far as I have gotten.

                        Another way to go is add all the buttons your going to use the first time around. Then decide which are to be visible and which are not. If you're putting to a form, all the buttons will have they're properties shown in the properties tab. There you can get the individual poperties, including visiblity. I've got my properties in alphabetical order, just to find them, I hate searching. Then just program behavoir of the buttons Have a good time L.

                        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