前言:
??作者簡介:我是笑霸final,一名熱愛技術(shù)的在校學生。
??個人主頁:個人主頁1 || 笑霸final的主頁2
??系列專欄:《作業(yè)專欄》
??如果文章知識點有錯誤的地方,請指正!和大家一起學習,一起進步??
??如果感覺博主的文章還不錯的話,??點贊?? + ??關(guān)注?? + ??收藏??
一、實驗目的
1.掌握窗體應用程序的創(chuàng)建及其構(gòu)成。
2.掌握控件的添加、控件的布局調(diào)整以及屬性的設(shè)置。
3.掌握命令按鈕、文本框、標簽控件、單選按鈕、復選框的使用。
4.掌握消息對話框的使用。
二、實驗任務
第一題
1.創(chuàng)建Windows窗體應用程序,設(shè)計一個用戶登錄窗口,如圖3.1所示,要求輸入用戶名和密碼(設(shè)用戶名為admin,密碼為123),輸入正確后利用消息框顯示如圖3.2所示歡迎信息“登錄成功,歡迎使用本系統(tǒng)”,點擊消息框中“確定”按鈕后,打開另一個窗體,如圖3.3所示;輸入不正確顯示如圖3.4所示消息框,若錯誤3次則程序結(jié)束。
要求:
(1)用戶名或密碼均不能為空,否則顯示消息框如圖3.5所示。
(2) 除了點擊“登錄”按鈕實現(xiàn)登錄驗證外,當在密碼文本框中輸入密碼后直接按Enter鍵也能實現(xiàn)登錄效果。
源代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Experiment_3
{
public partial class Form1 : Form
{
int RrrorCount = 0;//用來統(tǒng)計 密碼和用戶名輸入錯誤的次數(shù)
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
textBox1.Focus(); //當在文本框1中檢查到回車鍵時,直接將焦點轉(zhuǎn)入TextBox2
}
}
private void button1_Click(object sender, EventArgs e)
{
String user=textBox1.Text;
String password =textBox2.Text;
if(user.Equals("")|| password.Equals(""))
{//當密碼和用戶名為空時(必須同時都有值才不會進入此判斷)
MessageBox.Show("密碼和用戶名均不能為空", "登陸", MessageBoxButtons.OK);
}else if("admin".Equals(user)&& int.Parse(password) == 123)
{
//密碼和用戶名匹配成功時
MessageBox.Show("登陸成功,歡迎使用本系統(tǒng)", "登陸", MessageBoxButtons.OK);
Form f2 = new Form2();//創(chuàng)建第二個窗口實例
f2.Show();//打開第二個窗口
}
else if(!"admin".Equals(user) || ! (int.Parse(password) == 123))
{
MessageBox.Show("用戶名或者密碼錯誤,請重新輸入", "登陸", MessageBoxButtons.OK);
RrrorCount++;
textBox1.Clear();//清除輸入內(nèi)容
textBox2.Clear();//清除輸入內(nèi)容
if (RrrorCount >= 3)
{//說明已經(jīng)輸入錯誤三次了程序結(jié)束
this.Close();
return;
}
}
}
}
}
運行結(jié)果
第二題
2.創(chuàng)建Windows窗體應用程序,利用文本框、單選按鈕、分組控件、復選框設(shè)計界面,如圖3.6所示,實現(xiàn)根據(jù)選擇控件的單擊實現(xiàn)實時改變文本框中文字顯示效果的功能。
源代碼
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Experiment_3_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton1_Click(object sender, EventArgs e)
{
//幾個按鈕綁定得共享事件
RadioButton btn =(RadioButton)sender;
if (btn.Text.Equals("宋體"))
{
//設(shè)置宋體 其他不變 下面得else if 同理
richTextBox1.Font=
new Font("宋體", richTextBox1.Font.Size, richTextBox1.Font.Style);
}
else if (btn.Text.Equals("黑體"))
{
richTextBox1.Font =
new Font("黑體", richTextBox1.Font.Size, richTextBox1.Font.Style);
}
else if (btn.Text.Equals("楷體"))
{
richTextBox1.Font =
new Font("楷體", richTextBox1.Font.Size, richTextBox1.Font.Style);
}
else if (btn.Text.Equals("隸書"))
{
richTextBox1.Font =
new Font("隸書", richTextBox1.Font.Size, richTextBox1.Font.Style);
}
}
private void radioButton5_Click(object sender, EventArgs e)
{
//幾個按鈕綁定得共享事件
RadioButton btn = (RadioButton)sender;
//設(shè)置字體大小 其他不變 下面得else if 同理
if (btn.Text.Equals("10"))
{
richTextBox1.Font =
new Font(richTextBox1.Font.FontFamily,10, richTextBox1.Font.Style);
}
else if (btn.Text.Equals("15"))
{
richTextBox1.Font =
new Font(richTextBox1.Font.FontFamily, 15, richTextBox1.Font.Style);
}
else if (btn.Text.Equals("20"))
{
richTextBox1.Font =
new Font(richTextBox1.Font.FontFamily, 20, richTextBox1.Font.Style);
}
else if (btn.Text.Equals("25"))
{
richTextBox1.Font =
new Font(richTextBox1.Font.FontFamily, 25, richTextBox1.Font.Style);
}
}
private void radioButton5_CheckedChanged(object sender, EventArgs e)
{
}
private void Form1_Activated(object sender, EventArgs e)
{
}
private void Form1_Click(object sender, EventArgs e)
{
}
private void checkBox1_Click(object sender, EventArgs e)
{
if (this.checkBox1.Checked)
{//當 粗體被選中時
richTextBox1.Font =
new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, FontStyle.Bold);
}
else
{//當 粗體未被選中時
richTextBox1.Font =
new Font(richTextBox1.Font.FontFamily, richTextBox1.Font.Size, FontStyle.Regular);
}
}
}
}
運行結(jié)果
第三題
3.創(chuàng)建Windows窗體應用程序,實現(xiàn)“上網(wǎng)問卷調(diào)查”,調(diào)查內(nèi)容有:年齡,每天平均上網(wǎng)時間,上網(wǎng)主要做什么,如圖3.7所示。當回答完問題,點擊“確定”按鈕后,彈出“調(diào)查結(jié)果”消息框,顯示用戶的選擇,如圖3.8所示。
using System;
using System.Windows.Forms;
namespace Experiment_3_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//創(chuàng)建一些初始化得提示字符串
string myAge = "你的年齡為:";
string myTime = "你的上網(wǎng)時間為:";
string myDo = "你上網(wǎng)主要是:";
//選擇年齡
if (age1.Checked)//10歲被選中時
{
myAge += age1.Text ;
}
else if (age2.Checked)//11-17歲被選中
{
myAge += age2.Text;
}
else if (age3.Checked)//18-40歲被選中
{
myAge += age3.Text ;
}
else if (age4.Checked)
{
myAge += age4.Text ;
}
else
{
myAge += "未選擇年齡" ;
}
//選擇上網(wǎng)時間
if (time1.Checked)//小于1小時
{
myTime += time1.Text ;
}
else if (time1.Checked)//1-2小時
{
myTime += time2.Text ;
}
else if (time3.Checked)//1-3小時
{
myTime += time3.Text ;
}
else if (time4.Checked)//4小時以上
{
myTime += time4.Text ;
} else
{
myTime += "未選擇上網(wǎng)時長";
}
//上網(wǎng)干啥
if (checkBox1.Checked)//瀏覽咨詢
{
myDo += "瀏覽咨詢、";
}
if (checkBox2.Checked)//聊天
{
myDo += "聊天、";
}
if (checkBox3.Checked)//購物
{
myDo += "購物、";
}
if (checkBox4.Checked)//看視頻
{
myDo += "看視頻、";
}
if (checkBox5.Checked)//收發(fā)郵件
{
myDo += "收發(fā)郵件、";
}
if (checkBox6.Checked)
{
myDo += "其他、";
}
myDo=myDo.Remove(myDo.Length-1, 1);//移除多余得、號
myDo += "。";//添加句號
MessageBox.Show(myAge + "\n" + myTime + "\n" + myDo,"調(diào)查結(jié)果");
}
}
}
運行結(jié)果
第四題
4.創(chuàng)建Windows窗體應用程序,完成若干加法計算,初始界面如圖3.9所示,加數(shù)和被加數(shù)均為二位數(shù),程序運行時顯示第一道題目,輸入答案,點擊“確定”按鈕后,在標簽上顯示算式并進行是否正確的判斷,同時顯示下一道題目,完成若干計算后,單擊“結(jié)束”按鈕后,顯示“共完成X題,正確:X,錯誤:X,得分:X”的信息,如圖3.10所示。
提示:點擊“確定”后顯示算式的標簽和點擊“結(jié)束”后顯示最終評價的標簽使用二個標簽。文章來源:http://www.zghlxwxcb.cn/news/detail-448669.html
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Experiment_3_4
{
public partial class Form1 : Form
{
Random ramdom = new Random();//創(chuàng)建隨機數(shù)對象
int in1, in2;//隨機兩數(shù)
int correct=0, error=0;
private void button1_Click(object sender, EventArgs e)
{
//點擊確定時
try
{
if (int.Parse(textBox1.Text) == in1 + in2)
{
//正確時
correct++;
output1.Text += in1 + "+" + in2 + "=" + textBox1.Text + "?" + '\n';
}
else
{
//錯誤時
error++;
output1.Text += in1 + "+" + in2 + "=" + textBox1.Text + "?" + '\n';
}
}
catch(Exception ee)
{//發(fā)生異常
MessageBox.Show("請輸入數(shù)字");
textBox1.Text = "";
}
//輸入框清空
textBox1.Text = "";
textBox1.Focus();//textBox1重新獲取 焦點
output1.Visible = true;
//更新數(shù)據(jù)
Form1_Activated(null,null);
}
private void button2_Click(object sender, EventArgs e)
{
//結(jié)束按鈕
double score = correct*1.0 / (correct + error)*100;
//顯示數(shù)據(jù)
output2.Text = "一共完成" + (correct + error)
+ "題" + ",正確" + correct + "題" + ",錯誤" + error + "題," +
"得分:" + score.ToString("f2") + "分";
output2.Visible = true;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Activated(object sender, EventArgs e)
{
//窗口激活時發(fā)生的時間
in1 = ramdom.Next(10,100);
in2 = ramdom.Next(10, 100);
input1.Text = in1.ToString();
input2.Text = in2.ToString();
}
}
}
運行結(jié)果文章來源地址http://www.zghlxwxcb.cn/news/detail-448669.html
到了這里,關(guān)于實驗三 Windows窗體的設(shè)計及常用控件(1)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!