Help on dinamic button creation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lupus
    New Member
    • Feb 2007
    • 6

    Help on dinamic button creation

    I guess what I have to do is quite simple, but I'm really stuck!
    I need to create, in a form, a particular number "i" of "Button"s (i is given at runtime), whose reference name should be "button1","butt on2", ...
    I mean something like: Button button1 = new System.Windows. Forms.Button(); ...
    So I tried with a "for" looping i times, ech time to create a new Button called button; I wrote a function that, given parameter i, returns the string "button<i>" .
    This function is called each loop, but I can't manage to make this "button<i>" string become the reference name of the new created button, thus getting a separate object from the previous.
    I hope to have been clear.
    Thanx a lot in advance, for whoever'll try to help me!

    Davide
    //Trying desperetely to end up my thesis on time
  • enreil
    New Member
    • Jan 2007
    • 86

    #2
    You can creat an Array or an ArrayList of Buttons and stash your buttons in there. You can write a single method to hand the click event and then associate it with each button as it is created. Note: You'll need to do this in the Page_Init method, so that everytime your page is refreshed, the buttons are recreated.

    Hope that helps.

    Comment

    • lupus
      New Member
      • Feb 2007
      • 6

      #3
      Originally posted by enreil
      You can creat an Array or an ArrayList of Buttons and stash your buttons in there. You can write a single method to hand the click event and then associate it with each button as it is created. Note: You'll need to do this in the Page_Init method, so that everytime your page is refreshed, the buttons are recreated.

      Hope that helps.
      Thank you for the advice, I think I understood..jus t one thing: by Page_Init you mean the constructor of the form i'm talkin about, right?
      I have a problem, then:
      The form I have to create as I told u (AppWin) is called inside the main Application.Run (new StartUp), where StartUp is another form that is needed at the beginning to perform some initial operation and to decide how AppWin must be looking like.
      But when I call the constructor of AppWin, how can I turn the control of the application on this form instead than on the previous one?
      I mean, as how my program works by now, AppWin is created and after its constructor ends, the first form StartUp is again shown and active.
      Instead, how can I have AppWin active in full screen mode so that the buttons can be clickable?

      Thanx very much!!!

      Comment

      • enreil
        New Member
        • Jan 2007
        • 86

        #4
        Duh, I should have read your first post more clearly. Of coures there's no Page_Init in Forms.

        After you create an instance of AppWin with the constructor, use the AppWin.Show() method to display the form. When you are done using the form, use the Dispose() method.

        Hope that helps.


        Originally posted by lupus
        Thank you for the advice, I think I understood..jus t one thing: by Page_Init you mean the constructor of the form i'm talkin about, right?
        I have a problem, then:
        The form I have to create as I told u (AppWin) is called inside the main Application.Run (new StartUp), where StartUp is another form that is needed at the beginning to perform some initial operation and to decide how AppWin must be looking like.
        But when I call the constructor of AppWin, how can I turn the control of the application on this form instead than on the previous one?
        I mean, as how my program works by now, AppWin is created and after its constructor ends, the first form StartUp is again shown and active.
        Instead, how can I have AppWin active in full screen mode so that the buttons can be clickable?

        Thanx very much!!!

        Comment

        • kuzen
          New Member
          • Dec 2006
          • 20

          #5
          I hope I did not misunderstand you
          Following is what I did in VB, seems to be working
          Code:
           
          For i As Integer = 0 To 5
                      Dim Button As New System.Windows.Forms.Button
                      Me.SuspendLayout()
                      Button.Location = New System.Drawing.Point(112, 96 + i * 96)
                      Button.Name = "Button" + i.ToString
                      Button.TabIndex = 0
                      Button.Text = "Button" + i.ToString
                      Me.Controls.Add(Button)
          Next
          You definitely know you can play with the object location and size
          Regards

          Comment

          Working...