C# winform窗体学习

DataGridView

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace WindowsFormsStudys
{
    public partial class DataGridViewStudy : Form
    {
        public DataGridViewStudy()
        {
            InitializeComponent();
        }

        private void DataGridViewStudy_Load(object sender, EventArgs e)
        {
                //数据库信息
                string connectionString = "server=39.107.246.45;port=3306;user=qyxxcj;password=liujin216; database=qyxxcj;charset = utf8;";
                //查询语句
                string SQLString = "SELECT `entid`, `EntName` FROM `Ent_Main` WHERE 1";
               //开始操作数据库
               
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    //创建DataSet类的对象
                    DataSet ds = new DataSet();
                    try
                    {
                        //打开连接
                        connection.Open();
                        MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
                        //使用SqlDataAdapter对象sda将查新结果填充到DataSet对象ds中
                        command.Fill(ds, "ds");
                        //设置表格控件的DataSource属性
                        dataGridView1.DataSource = ds.Tables[0];
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        if (connection != null)
                        {
                            //关闭数据库连接
                            connection.Close();
                        }
                    }
                }

            
             //设置数据表格上显示的列标题
               dataGridView1.Columns[0].HeaderText = "ID";
               dataGridView1.Columns[0].HeaderText = "内容";
              
               //设置数据表格为只读
               dataGridView1.ReadOnly = true;
               //不允许添加行
               dataGridView1.AllowUserToAddRows = false;
               //背景为白色
               dataGridView1.BackgroundColor = Color.White;
               //只允许选中单行
               dataGridView1.MultiSelect = false;
               //整行选中
               dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                //设置颜色
               dataGridView1.Rows[0].DefaultCellStyle.ForeColor = Color.Red;
        }
    }
}

在这里插入图片描述

listBox

 //listBox 加载事件
        private void listBoxStudy_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                //给listBox添加数据
                listBox1.Items.Add("巴拉巴拉巴拉0" + i);
            }

        }

        //双击事件
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show("您点击的是索引" + listBox1.SelectedIndex + "其内容为" + listBox1.Text);
        }
    }

在这里插入图片描述

打开文件对话框

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 WindowsFormsStudys
{
    public partial class _12打开文件对话框 : Form
    {
        public _12打开文件对话框()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //弹出文件操作对话框
            OpenFileDialog opf = new OpenFileDialog();
            //设置对话框标题
            opf.Title = "请选择文件";
            //设置对话框可以多选
            opf.Multiselect = true;
            //设置默认打开的目录
            opf.InitialDirectory = @"F:\";
            //设置文件类型
            opf.Filter = "文本文件|*.txt|音乐文件|*.mp3|所有文件|*.*";
            //显示对话框
            opf.ShowDialog();

            string[] strr = opf.FileNames;

            for (int i = 0; i < strr.Length; i++)
            {
                textBox1.Text += strr[i] + "\r";
                
            }


        }
    }
}

在这里插入图片描述

保存对话框

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 System.IO;

namespace WindowsFormsStudys
{
    public partial class _13保存文件对话框 : Form
    {
        public _13保存文件对话框()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("内容不能为空");
                return;
            }
            SaveFileDialog svf = new SaveFileDialog();
            //设置title
            svf.Title = "请选择保存路径";
            //默认保存地址
            svf.InitialDirectory = @"D:\";
            //设置文件类型
            svf.Filter = "文本文件|*.txt|所有文件|*.*";
            //设置默认的保存文件名
            svf.FileName = "new"; 
            //展示对话框
            svf.ShowDialog();

            //获取保存文件的路径

            string path = svf.FileName;
            if (path == "" || path == "new")
            {
                MessageBox.Show("您选择的路径为空,请重新选择");
                return;
            }

            using (StreamWriter strw = new StreamWriter(path, true))
            {
                strw.WriteLine(textBox1.Text);
            }
            MessageBox.Show("保存成功");
        }
    }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

winform 小练习

timer 轮播图

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 System.IO;

namespace WindowsFormsStudys
{
    public partial class 轮播图 : Form
    {
        int i = 0;
        string[] imgs = Directory.GetFiles("./images");
        public 轮播图()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Add();
        } 

        private void 轮播图_Load(object sender, EventArgs e)
        {
            //设置图片铺面
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = Image.FromFile("./images/1.jpg");
        }

        public void Add()
        {
            i++;
            if (i >= imgs.Length) i = 0;
            pictureBox1.Image = Image.FromFile(imgs[i]);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i--;
            if (i < 0) i = imgs.Length - 1;
            pictureBox1.Image = Image.FromFile(imgs[i]);
        }

        //鼠标进入暂停
        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            timer1.Stop();
        }

        private void button2_MouseMove(object sender, MouseEventArgs e)
        {
            timer1.Stop();
        }
        //鼠标离开
        private void button1_MouseLeave(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            timer1.Stop();
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            timer1.Start();
        }
    }
}

在这里插入图片描述

Do You Love ME

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 WindowsFormsStudys
{
    public partial class Do_You_Love_ME : Form
    {
        public Do_You_Love_ME()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("ME TO 我也喜欢你");
        }

        /// <summary>
        /// 鼠标进入botton时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_MouseEnter(object sender, EventArgs e)
        {
           //拿到窗体的宽高 botton的可活动范围
            int x = this.ClientSize.Width - button2.Width;  //窗体宽度-按钮宽度
            int y = this.ClientRectangle.Height - button2.Height;
            //产生随机
            Random r = new Random();
            //重新定义按钮坐标
            button2.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1));
        }

        private void button2_Click(object sender, EventArgs e)
        {
          

            Point B1P = this.button2.Location;

            this.button2.Location = this.button1.Location;

            this.button1.Location = B1P;
            
        }
    }
}

在这里插入图片描述

石头剪子布

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 WindowsFormsStudys
{
    public partial class _11石头剪子布 : Form
    {
        public _11石头剪子布()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string text = "石头";
            GOGO(text);
        }

        private void GOGO(string text)
        {
            _11WJL wj = new _11WJL();
            //拿到玩家的值
            int wjNber = wj.GetNuber(text);
            label3.Text = text;
            //拿电脑的值
            _11DLL dl = new _11DLL();
            int dlNber = dl.GetNuber();
            label4.Text = dl.Fist;

            label5.Text = CP(wjNber, dlNber);

        }

        /// <summary>
        /// 裁判
        /// </summary>
        /// <param name="n1">玩家的值</param>
        /// <param name="n2">电脑的值</param>
        private string CP(int n1,int n2)
        {
            // 1 石头 2 剪刀 3 布
            if (n1 - n2 == -1 || n1 - n2 == 2)
            {
                return "玩家赢";
            }
            else if (n1 - n2 == 0)
            {
                return "平手";
            }
            else
            {
                return "电脑赢";
            }
        }

        private void _11石头剪子布_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string text = "剪刀";
            GOGO(text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string text = "布";
            GOGO(text);
        }
    }
}

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值