C#教學經驗談(2):會跑的按鈕
2024-07-21 02:18:06
供稿:網友
為了讓學生對事件有一個初步的印象,特意設計了一個趣味程序,界面較簡單,就是提一個簡單的問題,要你點yes或no按鈕來回答問題。但當你去點yes按鈕的時候,這個yes按鈕會滿窗口的跑,不讓你去點它。
這個趣味程序是在教學過程中臨時穿插進去的。使用這個程序的目的,是讓學生對c#中對事件的處理有一個初步的印象,了解事件要經過注冊和實現這兩步,另外通過這個趣味程序,讓學生了解random類和point類的使用。
本程序的全部代碼如下,請注意紅字的代碼,其余的代碼都是設計器自動生成的。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace loveme
{
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.label label1;
private system.windows.forms.button yesbutton;
private system.windows.forms.button nobutton;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗體設計器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
this.label1 = new system.windows.forms.label();
this.yesbutton = new system.windows.forms.button();
this.nobutton = new system.windows.forms.button();
this.suspendlayout();
//
// label1
//
this.label1.font = new system.drawing.font("monotype corsiva", 36f, system.drawing.fontstyle.italic, system.drawing.graphicsunit.point, ((system.byte)(0)));
this.label1.forecolor = system.drawing.color.forestgreen;
this.label1.location = new system.drawing.point(40, 72);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(320, 80);
this.label1.tabindex = 0;
this.label1.text = "do you love me?";
//
// yesbutton
//
this.yesbutton.location = new system.drawing.point(56, 248);
this.yesbutton.name = "yesbutton";
this.yesbutton.tabindex = 1;
this.yesbutton.text = "yes";
this.yesbutton.click += new system.eventhandler(this.yesbutton_click);
this.yesbutton.mousemove += new system.windows.forms.mouseeventhandler(this.yesbutton_mousemove);
//
// nobutton
//
this.nobutton.location = new system.drawing.point(240, 248);
this.nobutton.name = "nobutton";
this.nobutton.tabindex = 1;
this.nobutton.text = "no";
this.nobutton.click += new system.eventhandler(this.nobutton_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(400, 317);
this.controls.add(this.yesbutton);
this.controls.add(this.label1);
this.controls.add(this.nobutton);
this.name = "form1";
this.text = "想說愛我不容易";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void nobutton_click(object sender, system.eventargs e)
{
messagebox.show("oh,bye-bye!","i'm sorry",messageboxbuttons.ok,messageboxicon.warning);
this.close();
}
private void yesbutton_click(object sender, system.eventargs e)
{
messagebox.show("thank you, i'm very happy!","happy",messageboxbuttons.ok,messageboxicon.information);
}
private void yesbutton_mousemove(object sender, system.windows.forms.mouseeventargs e)
{
random rand = new random(unchecked((int)datetime.now.ticks));
int x=rand.next(0,this.size.width-100);
int y=rand.next(0,this.size.height-50);
point point=new point(x,y);
yesbutton.location=point;
}
}
}