Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 30th, 2010, 02:53 PM
Authorized User
 
Join Date: Jan 2010
Posts: 31
Thanks: 5
Thanked 2 Times in 2 Posts
Default Calculator

First of all, I am really impressed with this book, I can hardly put it down, the best computer book Ive read for 10 years or so. (currently on chap7)

My question is about the calculator example.
I wanted to build a calculator that looks like a typical calculator eg the windows one, with 9 number buttons and + - * / = buttons

It seems a bit clunky to have 13 button events in the code behind file..all with similar code, and you can't use the same id with different values, is there a better way?

Adam
 
Old January 31st, 2010, 05:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Adam,

Thank you for your kind words. Good to hear you like the book so much...

You *can* hook up the same handler to multiple buttons. Simply assign the OnClick to the same handler (C#) or specify multiple controls in the Handles section of the handler in VB.

So, for example in C# you can do this:
Code:
 
<asp:Button ID="Button1" runat="server" Text="1" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="2" onclick="Button1_Click" />
Then in Code Behind, you can cast the Sender to a Button and get its Text which holds the number:

Code:
 
protected void Button1_Click(object sender, EventArgs e)
{
  Button myButton = sender as Button;
  if (myButton != null)
  {
    int value = Convert.ToInt32(myButton.Text);
  }
}
However, this is not the best solutionn. The application breaks if you use text numbers (one, two and so on) or add other markup to the button. So, it's better to use a CommandName and CommandArgument:

Code:
 
<asp:Button ID="Button1" runat="server" Text="1" onclick="Button1_Click" CommandName="InputNumber" CommandArgument="1" />
<asp:Button ID="Button2" runat="server" Text="2" onclick="Button1_Click" CommandName="InputNumber" CommandArgument="2" />
<asp:Button ID="Button3" runat="server" Text="+" onclick="Button1_Click" CommandName="Operate" CommandArgument="+" />
Notice how the numeric buttons have an CommandName of InputNumber (arbitrarily chosen) and a CommandArgument with the number they represent. The Plus button has a CommandName of Operate. Now, in code behind you can do stuff like this:

Code:
 
protected void Button1_Click(object sender, EventArgs e)
{
  Button myButton = sender as Button;
  if (myButton != null)
  {
    switch (myButton.CommandName)
    {
      case "InputNumber":
        int value = Convert.ToInt32(myButton.CommandArgument);
        // Do what you need to do with the number here
        break;
      case "Operate":
        string theOperator = myButton.CommandArgument ;
        // Do what you need to do with the operator here
        break;
    }
  }
}
This way, inside the Click handler you can look at the CommandName (a number is clicked, or an operator is clicked) and the CommandArgument (to find out which number or operator was clicked).

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
AdamPembs (January 31st, 2010)
 
Old January 31st, 2010, 02:48 PM
Authorized User
 
Join Date: Jan 2010
Posts: 31
Thanks: 5
Thanked 2 Times in 2 Posts
Default

Thanks Imar, that helped a lot

Now Im having some fun with all the maths...strings, datatype conversions. Will post when Ive done it.





Similar Threads
Thread Thread Starter Forum Replies Last Post
calculator mojtaba rashidi Visual Studio 2005 0 March 17th, 2008 07:29 AM
Calculator DweeLer Other Programming Languages 1 November 18th, 2005 08:13 AM
calculator kale_tushar C++ Programming 1 January 28th, 2004 01:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.