Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Withevents in c# 1

Status
Not open for further replies.

MadJock

Programmer
May 25, 2001
318
GB
Folks,

I am working through an ASP.NET book that focusses on vb.net. In order to learn better, I'm trying to convert the examples to C#.

Unfortunately, I've got stuck with event handling and can't find any reference I understand on the web.

The application is a currency converter (simple one!) that uses an aspx file and a cs file.

CS File code:
Code:
using Microsoft.VisualBasic;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public class CurrencyConverter : Page {

	protected HtmlGenericControl Result;
	protected HtmlInputButton Convert;
	protected HtmlInputText US;
	protected delegate void MyEvent();

	private void Convert_ServerClick(Object sender, EventArgs e) {
		decimal USAmount = Decimal.Parse(US.Value);
		decimal EuroAmount = (USAmount * (decimal) 1.12);
		Result.InnerText = USAmount.ToString() + " US Dollars = " + EuroAmount.ToString() + " Euros ";
	}

}

ASPX File code:
Code:
<%@ Page Language=&quot;C#&quot; Inherits=&quot;CurrencyConverter&quot; Src=&quot;CurrencyConverter.cs&quot; AutoEventWireup=&quot;False&quot; %>
<html>
<body>
	<form method=&quot;post&quot; runat=&quot;server&quot;>
		<div>
			Convert:  
			<input type=&quot;text&quot; id=&quot;US&quot; runat=&quot;server&quot; />  US Dollars to Euros.
			<br /> <br />
			<input type=&quot;submit&quot; value=&quot;OK&quot; id=&quot;Convert&quot; runat=&quot;server&quot; />
			<div style=&quot;FONT_WEIGHT: bold&quot; id=&quot;Result&quot; runat=&quot;server&quot;></div>
		</div>
	
	</form>
</body>
</html>

The example in VB.NET shows the following two lines which I do not know how to reproduce in c#:
Code:
' It's the WithEvents keyword throwing me here
Protected WithEvents Convert as HtmlInputButton
and
Code:
' it's the handles keyword throwing me here!
Private Sub Convert_ServerClick(blah) Handles Convert.ServerClick
I think I will need to use a delegate in c#, but am unure of what to do with it (you'll see I've declared one in my C# class).

Any advice would be greatly appreciated.

Thanks,

Graeme

website:
 
You does not need to use a delegate in c#.
The line &quot;protected HtmlInputButton Convert;&quot; in C# is equal to
the line &quot;Protected WithEvents Convert as HtmlInputButton&quot; in VB.NET.

The line &quot;this.Convert.ServerClick += new System.EventHandler(this.Convert_ServerClick);&quot; in C# is equal to
the line &quot;Private Sub Convert_ServerClick(blah) Handles Convert.ServerClick&quot; in VB.NET.

The following code will work.

CurrencyConvert.aspx
<%@ Page language=&quot;c#&quot; Codebehind=&quot;CurrencyConvert.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;Test.CurrencyConvert&quot; %>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; >
<HTML>
<HEAD>
<title>CurrencyConvert</title>
<meta name=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 7.0&quot;>
<meta name=&quot;CODE_LANGUAGE&quot; Content=&quot;C#&quot;>
<meta name=&quot;vs_defaultClientScript&quot; content=&quot;JavaScript&quot;>
<meta name=&quot;vs_targetSchema&quot; content=&quot; </HEAD>
<body MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;CurrencyConvert&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<div>
Convert: <input type=&quot;text&quot; id=&quot;US&quot; runat=&quot;server&quot; NAME=&quot;US&quot;> US Dollars to
Euros.
<br>
<br>
<input type=&quot;submit&quot; value=&quot;OK&quot; id=&quot;Convert&quot; runat=&quot;server&quot; NAME=&quot;Convert&quot;>
<div style=&quot;FONT_WEIGHT: bold&quot; id=&quot;Result&quot; runat=&quot;server&quot;><FONT face=&quot;MS UI Gothic&quot;></FONT></div>
</div>
</form>
</body>
</HTML>


CurrencyConvert.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Test
{
/// <summary>
///
/// </summary>
public class CurrencyConvert : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText US;
protected System.Web.UI.HtmlControls.HtmlInputButton Convert;
protected System.Web.UI.HtmlControls.HtmlGenericControl Result;

private void Page_Load(object sender, System.EventArgs e)
{
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
//
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
///
///
/// </summary>
private void InitializeComponent()
{
this.Convert.ServerClick += new System.EventHandler(this.Convert_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Convert_ServerClick(object sender, System.EventArgs e)
{
decimal USAmount = Decimal.Parse(US.Value);
decimal EuroAmount = (USAmount * (decimal) 1.12);
Result.InnerText = USAmount.ToString() + &quot; US Dollars = &quot; + EuroAmount.ToString() + &quot; Euros &quot;;
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top