none
Button Click Event RRS feed

  • Question

  • Hi everyone,

    If my program was to have alot of buttons for example a calculator, whats the best wat to create and manage the button clicks? So far I have written pretty much the same code over and over to create the buttons apart from coordinates and caption. Also what is the best way to handle the button click event as I have a really long IF STATEMENT.

    Is this the only way?

    Kirk.

    Tuesday, November 30, 2010 11:53 AM

Answers

  • You should probably use arrays to store the buttons and additional data.  Then check the buttons in the ButtonClicked event.  Somewhere along the way you will need a few If ..Then. ElseIF .. to process the buttons pressed.  Simple example here:

    For i = 1 To 9
      x = 30*(1+Math.Remainder(i-1,3))
      y = 30*(1+Math.Floor((i-1)/3))
      button[i] = Controls.AddButton(i,x,y)
      buttonValue[i] = i
    EndFor

    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      buttonClicked = Controls.LastClickedButton
      For i = 1 To Array.GetItemCount(button)
        If (button[i] = buttonClicked) Then
          GraphicsWindow.ShowMessage("Button clicked has value "+buttonValue[i],"Information")
        EndIf
      EndFor
    EndSub

    • Marked as answer by Kirkkaf Thursday, December 2, 2010 11:48 AM
    Tuesday, November 30, 2010 4:58 PM
    Moderator

All replies

  • You should probably use arrays to store the buttons and additional data.  Then check the buttons in the ButtonClicked event.  Somewhere along the way you will need a few If ..Then. ElseIF .. to process the buttons pressed.  Simple example here:

    For i = 1 To 9
      x = 30*(1+Math.Remainder(i-1,3))
      y = 30*(1+Math.Floor((i-1)/3))
      button[i] = Controls.AddButton(i,x,y)
      buttonValue[i] = i
    EndFor

    Controls.ButtonClicked = OnButtonClicked

    Sub OnButtonClicked
      buttonClicked = Controls.LastClickedButton
      For i = 1 To Array.GetItemCount(button)
        If (button[i] = buttonClicked) Then
          GraphicsWindow.ShowMessage("Button clicked has value "+buttonValue[i],"Information")
        EndIf
      EndFor
    EndSub

    • Marked as answer by Kirkkaf Thursday, December 2, 2010 11:48 AM
    Tuesday, November 30, 2010 4:58 PM
    Moderator
  • OK, I'm a newbie with SmallBasic, but I've been a Visual Basic Developer during one of my careers.  Right now I'm teaching technology to kids and have found the first part of the introducing tutorial very useful for teaching BASIC programming, and the kids love it!.  I have also been teaching Terrapin Logo, and I'm going to give the turtle graphics a shot to see if that would be a good replacement for Terrapin Logo.  

    But the graphics part of smallbasic, such as the buttons problem above.  Why wouldn't you just use Visual Basic for anything with buttons and graphics?  What a pain to keep track of an array of buttons!

     

    Wednesday, December 8, 2010 4:56 AM
  • mayelamingi,

    Yes, any of the .Net languages are much more powerful than SmallBasic for developing windows applications.  However they do require quite a lot more learning to do anything useful with buttons etc.  SmallBasic is as you can see a learning language and probably not the choice for those who already have a knowledge of Visual Studio, VB, C# etc.  SmallBasic has the great advantage of a very small command set and simple IDE (unlike the .Net languages). 

    The small set of commands firstly makes it easier to get started, but also forces programmers to do most of the work themselves handling buttons and events for example - while this is not ideal for a professional development language, it does encourage learning how and why programming works (or doesn't).

    Also, SmallBasic does have enough to introduce ideas of events, arrays, conditions, loops, string manipulations, graphics, file handling, web program sharing, subroutines, sounds, GUI design etc - the hope is that programmers who get started with SmallBasic can move onto one of the many more advanced enviorments such as VS Express, when they feel comfortable with SmallBasic and start to hits its performance and other limitations such as you raised. 

    There is always a temptation to extend the features, but this has so far been resisted, retaining a Small language that is ideal for children and first time programers.  Extensions can be written in .Net languages to enhance SmallBasic and writing these is often a good stepping-stone to something like VB.

    These are just my opinions, not MS to which I have no affiliation.

    Wednesday, December 8, 2010 7:18 PM
    Moderator
  • This is my first post on here!

    I'm doing a basic program that uses buttons too. I was just wondering how am I meant to know to type all that code (the sums) beside the x and y variables (after the = signs)?

    I understand what it does, but how do I know to write that? Just through experience?

    Sunday, October 7, 2012 10:00 PM
  • Welcome.

    Basically yes, through experience.

    First people tend to not use loops so much and for example:

    button1 = Controls.AddButton(1,30,30) buttonValue1 = 1 button2 = Controls.AddButton(2,60,30) buttonValue2 = 2

    etc

    Then they realise arrays can help:

    button[1] = Controls.AddButton(1,30,30) buttonValue[1] = 1 button[2] = Controls.AddButton(2,60,30) buttonValue[2] = 2

    etc

    Then using a loop, in this case the position of a 3 by 3 collection of 9 buttons makes the arithmatic a bit more complicated, but it could have been done this way:

    value = 0
    For i = 1 To 3
      For j = 1 To 3
        value = value+1
        x = 30*j
        y = 30*i
        button[i][j] = Controls.AddButton(i,x,y)
        buttonValue[i][j] = value
      EndFor
    EndFor

    But it was more natural to have the buttons as one array, although this did complicate their positioning.

    So, yes experience and doing it the long way first (feels simpler, but makes the rest of the code harder) then thinking this could be done easier with less repeated and simpler code.

    Sunday, October 7, 2012 10:16 PM
    Moderator
  • Thank you very much litdev!

    I'm a new programmer! Hope I don't ask any silly questions.

    Monday, October 8, 2012 3:23 PM
  • thanks for this, very helpful
    Friday, February 15, 2013 8:05 PM
    Moderator