namespace firstly #当前命名空间控件
{
public partial class Form1 : Form
{
public Form1() #第一个窗体
{
InitializeComponent();
}
}
}
winform 代码结构
1.常用控件的认识
修改winform 应用程序的入口点
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
//程序位于 Program.cs
namespace firstly
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//决定那个窗体先被执行
Application.Run(new Form22());
// Application.Run(new Form1());
}
}
}
修改程序入口点
可以通过修改 Form1.Designer.cs 来实现 对控件进行修改 (一般 图形进行控制)
button1.Enabled = false; 是指不可用
Application.Exit(); 关闭总窗体
Form.Close(); 关闭当前的窗体
(1).基本控件
1.Label 标签
功能 :显示窗体文本
常用属性和方法 事件 :Text
Hide 即是Visible 属性设置为True 控件也不可见
Show 相当于Visible属性设置为True
Click: 单击控件
eg:
在一个界面 ,在此打开一个新的窗体,(当前窗体隐藏)
//当前界面隐藏
2 linkLabel1.LinkVisited = true; //确认可以被访问
3 Form2 f = new Form2();
4 f1.Show();
5 this.Hide(); // 当前隐藏
2.TextBox 文本框的控件和 Button 按钮
功能 接受或显示用户文本信息
常用属性和方法 事件 :
MaxLength 文本框输入最大字符
Multiline 是否可以在文本框输入多行
Passwordchar 密码输入框
Readonly 文本框中的字体为可读
Text 检索在控件输入的文本
Clear 删除现有的所有文本
Show 相当于Visible属性设置为True
KeyPress 用户按一个键结束时将发生该事件
Botton控件
Enabled 是否启用
PerFormClick Button中Click时间
Click 点击触发的
eg 用户登录
private void button2_Click(object sender, EventArgs e) //取消
{
clear(); //调用自己构造的方法
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty || textBox2.Text == string.Empty)
{
MessageBox.Show("信息禁止为空!","登录提示");
clear();
return;
}
if (!textBox1.Text.Equals("admin") || !textBox2.Text.Equals("admin")) # 输入比对
{
MessageBox.Show("用户名称或密码为空!", "登录提示");
clear();
return;
}
else
{
MessageBox.Show("欢迎您登录本系统!","消息提示");
clear();
}
}
//由于每次都调用 所以生成一个方法 更有效率
public void clear()
{
textBox1.Clear();
textBox2.Clear();
textBox2.Focus();
}
用户登录
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool ISLOGIN = false;
//就加在其构造函数的前面。这东西我们等下要用。
//然后加一个属性,该属性用来判别是否登录
public bool isLogin
{
get
{
return this.ISLOGIN;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text == "qq" && this.textBox2.Text == "qq")
{
this.ISLOGIN = true;
//frmEditor f2 = new frmEditor();
//f2.ShowDialog();
this.Close();//登陆成功才关闭登陆登陆窗体
}
else
{
MessageBox.Show("非法用户名或密码,请再重试一次!");
}
}
}
}
登录成功后才会关闭
Form1.MaximizeBox=false 窗体不允许最大化
text1.PasswordChar='*';
3.ListBox 列表框 控件
功能显示多行文本信息 , 提供用户选择
常用属性和方法 事件 :
Items : 列表框的具体内容
SelectionMode 列表框类型 单选 ,多选, 不可选择
SelectedIndex 指定选中行索引,默认第一行为0
SelectedItem 被选择的文本内容
SelectedItems ListBox的选择列表集合