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. Web Development
  3. ASP.NET
  4. Create Button at runtime!

Create Button at runtime!

Scheduled Pinned Locked Moved ASP.NET
xml
7 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
    Sr Frank
    wrote on last edited by
    #1

    I have to create buttons,forms at runtime after reading XML file. XML file will contain numbers of buttons and froms which i have to create run time. Please let me know right way.

    D A 2 Replies Last reply
    0
    • S Sr Frank

      I have to create buttons,forms at runtime after reading XML file. XML file will contain numbers of buttons and froms which i have to create run time. Please let me know right way.

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      Where do you want to place these buttons? On the dynamic forms?

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      S 1 Reply Last reply
      0
      • D dan sh

        Where do you want to place these buttons? On the dynamic forms?

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        S Offline
        S Offline
        Sr Frank
        wrote on last edited by
        #3

        Windows Forms and Buttons both will be created at runtime.

        D 1 Reply Last reply
        0
        • S Sr Frank

          Windows Forms and Buttons both will be created at runtime.

          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          You are in the wrong forum then. You can create a user control which exposes a property where you can set the button count. That user control will in turn have a TableLayoutPanel or a FlowLayoutPanel which will ensure the layout comes out good. Now, when you set the button count, you can add cells to the TableLayoutPanel and add buttons to it. You can also have a delegate exposed from the user control which will act as a common click event for the button and which can be handled anywhere. Then, create a form and add this user control to it.

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          S 1 Reply Last reply
          0
          • D dan sh

            You are in the wrong forum then. You can create a user control which exposes a property where you can set the button count. That user control will in turn have a TableLayoutPanel or a FlowLayoutPanel which will ensure the layout comes out good. Now, when you set the button count, you can add cells to the TableLayoutPanel and add buttons to it. You can also have a delegate exposed from the user control which will act as a common click event for the button and which can be handled anywhere. Then, create a form and add this user control to it.

            50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

            S Offline
            S Offline
            Sr Frank
            wrote on last edited by
            #5

            I really appreciate if you could provide me sample code or any useful link.

            D 1 Reply Last reply
            0
            • S Sr Frank

              I really appreciate if you could provide me sample code or any useful link.

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #6

              I don't have a sample code for this for now. You can try this: 1. Create a user control with a tablelayoutpanel in it. 2. Add a method to it that takes in one integer parameter. In that method, add rows and columns to the panel and add buttons in it. 3. Search for "delegates and events" or "c# expose events from user control" to understand how to expose the click event for the buttons and handle it in the creating form.

              50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

              1 Reply Last reply
              0
              • S Sr Frank

                I have to create buttons,forms at runtime after reading XML file. XML file will contain numbers of buttons and froms which i have to create run time. Please let me know right way.

                A Offline
                A Offline
                Abhishek Sur
                wrote on last edited by
                #7

                Do like this :

                Button newButton = new Button();
                newButton.Location = new Point(30,50); // Put your calculated point here.
                newButton.Text = "Click Me";
                newButton.Size=new Size(50,25);
                newButton.Click += new EventHandler(newButton_Click);
                this.Controls.Add(newButton);

                Place your Eventhandler as newButton_Click.

                public void newButton_Click(object sender, EventArgs e)
                {
                // Do all you need.
                }

                Please do post in right forum next time... :doh:

                Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


                My Latest Articles-->** Simplify Code Using NDepend
                Basics of Bing Search API using .NET
                Microsoft Bing MAP using Javascript

                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