读书人

.net简单计算器的有关问题(有代码)

发布时间: 2011-12-26 23:09:58 作者: rapoo

.net简单计算器的问题(有代码)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler Me.btnAdd.Click, AddressOf Calculate
AddHandler Me.btnSubtract.Click, AddressOf Calculate
AddHandler Me.btnMultiply.Click, AddressOf Calculate
AddHandler Me.btnDivide.Click, AddressOf Calculate
End Sub

Public Sub Calculate(ByVal sender As Object, ByVal e As System.EventArgs)
Dim op1 As Integer = CType(Me.TextBox1.Text, Integer)
Dim op2 As Integer = CType(Me.TextBox2.Text, Integer)
Dim result As Integer
Select Case CType(sender, button).CommandName
Case "Add "
result = op1 + op2
Case "Subtract "
result = op1 - op2
Case "Multiply "
result = op1 * op2
Case "Divide "
' Divide two numbers and return an integer result.
If op2 > 0 Then
result = op1 \ op2
Else
result = 0
End If
Case Else
' Error handling code here.


End Select
Label2.Text = result.ToString()
End Sub

运行是,提示:Select Case CType(sender, button).CommandName出错,请问该怎么改,谢谢!

[解决办法]
先保存在一个string中,再case
[解决办法]
一大堆if...elseif...的代码可以说是豆腐渣工程。

你要明确自己的设计思维到底倾向于哪个方向,到底是方法依据对象来分别定义(对不同类型的btn重载不同的Calculate),还是对象依据方法来定义(你那种把几种类型拼在方法内的做法)。
[解决办法]
运行是,提示:Select Case CType(sender, button).CommandName出错,请问该怎么改,谢谢!
=============================================
悄悄地说一句对于vb代码的语法格式有点不太熟悉
应该是你的前台代码有问题了,前面加了CommandName CommandArgument OnCommand
<asp:Button id= "btnAdd " runat= "server " Text= "+ " CommandName= "Add " OnCommand= "btnAdd_Click " > </asp:Button>
.....................后面的不一一列举了
<asp:Button id= "btnSubtract " runat= "server " Text= "+ " CommandName= "Subtract " > </asp:Button>
<asp:Button id= "btnMultiply " runat= "server " Text= "+ " CommandName= "Multiply "> </asp:Button>
<asp:Button id= "btnDivide " runat= "server " Text= "+ " CommandName= "Divide "> </asp:Button>

这里有一个msdn示例了
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemWebUIWebControlsButtonClassCommandNameTopic.htm

<%@ Page Language= "C# " AutoEventWireup= "True " %>

<html>
<head>

<script runat= "server ">

void CommandBtn_Click(Object sender, CommandEventArgs e)
{

switch(e.CommandName)
{

case "Sort ":

// Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break;

case "Submit ":

// Display a message for the Submit button being clicked.
Message.Text = "You clicked the Submit button ";

// Test whether the command argument is an empty string ( " ").
if((String)e.CommandArgument == " ")
{
// End the message.
Message.Text += ". ";
}
else
{
// Display an error message for the command argument.
Message.Text += ", however the command argument is not recogized. ";
}
break;

default:

// The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized. ";
break;

}

}

void Sort_List(string commandArgument)
{

switch(commandArgument)
{

case "Ascending ":

// Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button. ";
break;

case "Descending ":



// Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button. ";
break;

default:

// The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized. ";
break;

}

}

</script>

</head>

<body>

<form runat= "server ">

<h3> Button CommandName Example </h3>

Click on one of the command buttons.

<br> <br>

<asp:Button id= "Button1 "
Text= "Sort Ascending "
CommandName= "Sort "
CommandArgument= "Ascending "
OnCommand= "CommandBtn_Click "
runat= "server "/>

 

<asp:Button id= "Button2 "
Text= "Sort Descending "
CommandName= "Sort "
CommandArgument= "Descending "
OnCommand= "CommandBtn_Click "
runat= "server "/>

<br> <br>

<asp:Button id= "Button3 "
Text= "Submit "
CommandName= "Submit "
OnCommand= "CommandBtn_Click "
runat= "server "/>

 

<asp:Button id= "Button4 "
Text= "Unknown Command Name "
CommandName= "UnknownName "
CommandArgument= "UnknownArgument "
OnCommand= "CommandBtn_Click "
runat= "server "/>

 

<asp:Button id= "Button5 "
Text= "Submit Unknown Command Argument "
CommandName= "Submit "
CommandArgument= "UnknownArgument "
OnCommand= "CommandBtn_Click "
runat= "server "/>

<br> <br>

<asp:Label id= "Message " runat= "server "/>

</form>

</body>
</html>

读书人网 >asp.net

热点推荐