第三步 增加响应用户事件代码
还有最后一步就可以大功告成了就是增加一个方法来捕捉按钮点击事件这里就是指从摄氏到华氏的按钮点击代码
private void bnCtoF_Click(Object sender EventArgs e) {
double dTempCel = ;
double dTempFah = ;
try { dTempCel = tTempCelTextToDouble(); }
catch(Exception) {
tTempCelClear();
tTempFahClear();
return;
}
dTempFah = *dTempCel+;
tTempFahText = dTempFahToString();
tTempFahFocus();
tTempFahSelectionStart = ;
tTempFahSelectionLength = ;
tTempCelFocus();
tTempCelSelectionStart = ;
tTempCelSelectionLength = ;
}
第四行到第八行(也就是try 区中的一切)取回Celsius(摄氏)文本框中的数值如果它是一个双字节数就将其存储在dTempCel中否则就清除两个文本框并退出接着用存储在dTempCel
中的值我们用第 行中的公式将相同的温度存储在Fahrenheit中将这个新的数值在 Fahrenheit(华氏)文本框中显示 然后将光标放在每个文本框中以便将指针设置到开头(如果不将指针设置到开头我们就会看到一个长长的数字的结尾要看开头就必须滚动鼠标)
以下是Fahrenheit按钮的代码它将完成同样的任务只不过是相反的处理
private void bnFtoC_Click(Object sender EventArgs e) {
double dTempCel = ;
double dTempFah = ;
try { dTempFah = tTempFahTextToDouble(); }
catch(Exception) {
tTempCelClear();
tTempFahClear();
return;
}
dTempCel = (dTempFah)/;
tTempCelText = dTempCelToString();
tTempCelFocus();
tTempCelSelectionStart = ;
tTempCelSelectionLength = ;
tTempFahFocus();
tTempFahSelectionStart = ;
tTempFahSelectionLength = ;
}
接着我们需要将适当的点击事件捕捉方法与按钮的 Click事件联系起来要完成这一步我们将以下两行放在类的构造器中:
bnCtoFClick += new EventHandler(thisbnCtoF_Click);
bnFtoCClick += new EventHandler(thisbnFtoC_Click);
最后请看完整的代码
using System;
using SystemWinForms;
public class TempConverter : SystemWinFormsForm {
Label lTempFah = new Label();
Label lTempCel = new Label();
TextBox tTempFah = new TextBox();
TextBox tTempCel = new TextBox();
Button bnCtoF = new Button();
Button bnFtoC = new Button();
public TempConverter() {
thisSetSize();
thisBorderStyle = FormBorderStyleFixedDialog;
thisText = +C -> +F / +F -> +C ;
thisStartPosition = FormStartPositionCenterScreen;
thisHelpButton = false;
thisMaximizeBox = false;
tTempCelTabIndex = ;
tTempCelSetSize();
tTempCelSetLocation();
lTempCelTabStop = false;
lTempCelText = C;
lTempCelSetSize( );
lTempCelSetLocation();
tTempFahTabIndex = ;
tTempFahSetSize();
tTempFahSetLocation();
lTempFahTabStop = false;
lTempFahText = F;
lTempFahSetSize();
lTempFahSetLocation();
bnCtoFTabIndex = ;
bnCtoFText = C to F;
bnCtoFSetSize();
bnCtoFSetLocation();
bnCtoFClick += new EventHandler(thisbnCtoF_Click);
bnFtoCTabIndex = ;
bnFtoCText = F to C;
bnFtoCSetSize();
bnFtoCSetLocation();
bnFtoCClick += new EventHandler(thisbnFtoC_Click);
thisControlsAdd(tTempCel);
thisControlsAdd(lTempCel);
thisControlsAdd(tTempFah);
thisControlsAdd(lTempFah);
thisControlsAdd(bnCtoF);
thisControlsAdd(bnFtoC);
//= new Control [] { tTempCel lTempCel tTempFah lTempFah bnCtoF bnFtoC };
}
public static void Main() {
ApplicationRun( new TempConverter() );
}
private void bnCtoF_Click(Object sender EventArgs e) {
double dTempCel = ;
double dTempFah = ;
try { dTempCel = tTempCelTextToDouble(); }
catch(Exception) {
tTempCelClear();
tTempFahClear();
return;
}
dTempFah = *dTempCel+;
tTempFahText = dTempFahToString();
tTempFahFocus();
tTempFahSelectionStart = ;
tTempFahSelectionLength = ;
tTempCelFocus();
tTempCelSelectionStart = ;
tTempCelSelectionLength = ;
}
private void bnFtoC_Click(Object sender EventArgs e) {
double dTempCel = ;
double dTempFah = ;
try { dTempFah = tTempFahTextToDouble(); }
catch(Exception) {
tTempCelClear();
tTempFahClear();
return;
}
dTempCel = (dTempFah)/;
tTempCelText = dTempCelToString();
tTempCelFocus();
tTempCelSelectionStart = ;
tTempCelSelectionLength = ;
tTempFahFocus();
tTempFahSelectionStart = ;
tTempFahSelectionLength = ;
}
}
结 语
到此为止你看到了如何用C#进行编程的一个完整过程这个例子虽然很简单但是麻雀虽小五髒俱全理解其中的原理后就可以大显身手充分发挥C#的强大功能了