.NET基础-15-控件|MDI窗体控件

窗体控件属性

//用来获取或者设置窗体的名称
            //Name
            //获取或者设置窗体的大小
            this.Size = new Size(600, 500);
            //获取或者设置窗体运行时的起始位置
            this.StartPosition = FormStartPosition.CenterScreen;
            //设置一个值,设置窗体处于最大化/最小化/还是原状态
            //this.WindowState = FormWindowState.Normal;
            //设置或者返回窗体标题的文字
            this.Text = "我也爱你哦 思密达";
            //设置窗体的宽好高
            //this.Width = 100;
            //this.Height = 200;
            //设置窗体相对于屏幕的起始位置,和属性StartPosition冲突,且StartPosition优先级高
            this.Left = 100;
            this.Top = 200;
            //控制窗体的标题栏中是否显示控制框,true显示
            this.ControlBox = true;
            //控制最大化/最小化按钮是否显示,必须两个都为false才不显示??只有一个为false只是不能用??
            this.MaximizeBox = false;
            this.MinimizeBox = true;
            //按下Enter键相当于单击按钮
            this.AcceptButton = button1;
            //按下Esc相当于单击按钮
            this.CancelButton = button1;
            //设置是否为有模式显示窗体,这是只读的,补鞥呢赋值
            //this.Modal
            //是否自动显示滚动条
            this.AutoScroll = true;
            //设置背景色
            this.BackColor = Color.Red;
            //设置背景图片
            //this.BackgroundImage=
            //是否对交互做出相应,设置整个界面不能编辑,不能操作
            //this.Enabled = false;
            //设置文本字体样式
            //this.Font =
            //设置控件的前景色
            this.button1.ForeColor = Color.Red;
            //设置该窗体是否为子窗体
            //this.IsMdiChild = true;
            //设置该窗体是否为子窗体的容器
            this.IsMdiContainer = true;
            //设置改窗体是否显示
            this.Visible = true;
            //隐藏改窗体
            //this.Hide();
            //强制使工作区无效,并重绘自己和任何子控件
            this.Refresh();
           
            //事件
            //Load事件:该事件在窗体加载到内存时发生,即在第一次显示窗体前发生
            //Activated事件:该事件在窗体激活时发生
            //Deactivate事件:该事件在窗体失去焦点成为不活动窗体时发生
            //Resize事件:该事件在改变窗体大小时发生
            //Paint事件:该事件在重绘窗体时发生
            //Click事件:该事件在用户单击窗体时发生
            //DoubleClick事件:该事件在用户双击窗体时发生
            //Closed事件:该事件在关闭窗体时发生

            //Form2 fr = new Form2();
            ////显示字窗体
            //fr.Show();
            ////将窗体显示为模式对话框
            ////fr.ShowDialog();
            ////激活窗体并给他焦点
            //fr.Activate();
            ////关闭子窗体

            //fr.Close();


单选和多选

        /// 单选checkBox
        ///
        /// 多选radioButton
        /// 如果不对RadioButton进行分组,则界面上所有的RadioButton都是只能单选的
        /// 想要让他们分开则可以使用容器GroupBox进行分组

            //多选默认选择吃饭
            this.checkBox1.Checked = true;
            //单选性别默认选择男
            this.radioButton1.Checked = true;
            //单选婚姻状况默认选择未婚
            this.radioButton4.Checked = true;



图片控件PictureBox


public Form1()
        {
            InitializeComponent();


            //设置图片样式
            this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        }

        //保存当前图片的下标
        int i = 0;
        //保存图片的全路径
        string[] path;

        private void Form1_Load(object sender, EventArgs e)
        {
            //为图片赋值一个图片的路径
            //Image.FromFile表示图片的来源路径
            this.pictureBox1.Image = Image.FromFile(@"C:\Users\Administrator\Desktop\QQ图片20180226171956.jpg");


            //获取E:\桌面备份\BQ文件夹下的所有格式为jpg的图片完整路径
            path = Directory.GetFiles(@"E:\桌面备份\BQ", "*.jpg");
            this.pictureBox1.Image = Image.FromFile(path[i]);
        }



        /// <summary>
        /// 上一张
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Click(object sender, EventArgs e)
        {
            i--;
            if (i < 0)
            {
                i = path.Length - 1;
            }
            this.pictureBox1.Image = Image.FromFile(path[i]);
        }



        /// <summary>
        /// 下一张
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            i++;
            if (i > path.Length - 1)
            {
                i = 0;
            }
            this.pictureBox1.Image = Image.FromFile(path[i]);
        }


ComboBox


 public Form1()
        {
            InitializeComponent();


            //循环添加数据进去
            for (int i = 1; i <= 7; i++)
            {
                comboBox1.Items.Add("星期" + i);
            }
            //选中第一个
            comboBox1.SelectedIndex = 0;

            //设置控件comboBox2只可以下拉选择而不能输入
            comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
        }


        /// <summary>
        /// 当下标改变的时候发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //赋值之前清空comboBox2
            comboBox2.Items.Clear();
            //如果comboBox1的下标是1则显示第二天
            if (comboBox1.SelectedIndex == 1)
            {
                comboBox2.Items.Add("第二天");
            }
            else//否则显示非第二天
            {
                comboBox2.Items.Add("非第二天");
            }
            //默认选择第一条数据
            comboBox2.SelectedIndex = 0;
        }


ListBox


 public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 保存图片的路径
        /// </summary>
        List<string> path = new List<string>();


        private void Form1_Load(object sender, EventArgs e)
        {
            //使用Directory获取所有格式为jpg的图片
            string[] str = Directory.GetFiles(@"E:\桌面备份\BQ", "*.jpg");
            int len = str.Length;
            for (int i = 0; i < len; i++)
            {
                //把图片的名称添加到listBox1集合中
                listBox1.Items.Add(Path.GetFileName(str[i]));
                //把图片的全路径添加到变量path中
                path.Add(str[i]);
            }

            //设置默认选中第一个
            listBox1.SelectedIndex = 0;
            //为图片加载当前选中的名称的图片
            pictureBox1.Image = Image.FromFile(path[listBox1.SelectedIndex]);

            //设置图片的显示样式
            pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
        }


        /// <summary>
        /// 如果选择的下标改变的时候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(path[listBox1.SelectedIndex]);
        }


OpenFileDialog

        private void button1_Click(object sender, EventArgs e)
        {
            //创建打开对话框的对象
            OpenFileDialog ofd = new OpenFileDialog();
            //设置打开对话框的标题
            ofd.Title = "请选择文件哦亲~~~";
            //设置可以多选
            ofd.Multiselect = true;
            //设置初始化目录
            ofd.InitialDirectory = @"E:\桌面备份\BQ";
            //设置打开文件的类型
            ofd.Filter = "图片|*.jpg|音频文件|*.mp4|文本类型|*.txt|全部|*.*";
            //显示
            ofd.ShowDialog();

            //获得所有选中文件的全路径
            string[] str = ofd.FileNames;
            int len = str.Length;
            for (int i = 0; i < len; i++)
            {
                listBox1.Items.Add(Path.GetFileName(str[i]));
            }
        }


SaveFileDialog

private void button1_Click(object sender, EventArgs e)
        {
            //创建保存文本框的对象
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件的保存路径
            sfd.InitialDirectory = @"C:\Users\Administrator\Desktop\test";
            //设置保存文本框的提示信息
            sfd.Title = "请选择要保存的路径";
            //设置保存的文件名
            sfd.FileName = "2";
            //
            sfd.Filter = "文本文件|*.txt|全部文件|*.*";
            sfd.ShowDialog();

            //获取文件保存的完整路径
            string path = sfd.FileName;
            //判断路径不能为空
            if (path == "")
            {
                return;
            }
            //获取需要保存的文本
            string str = this.textBox1.Text;
            //创建流的对象
            FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
            //把文本内容转换为字节
            byte[] byt = System.Text.Encoding.Default.GetBytes(str);
            //把字节写入的文件
            fsWrite.Write(byt, 0, byt.Length);
            //释放资源
            fsWrite.Dispose();
            //关闭流
            fsWrite.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //创建颜色的对象
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            //为文本框赋值颜色
            this.textBox1.BackColor = cd.Color;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //创建字体的对象
            FontDialog fd = new FontDialog();
            fd.ShowDialog();
            //为文本框赋值字体
            this.textBox1.Font = fd.Font;
        }


你爱我吗??



private void timer1_Tick(object sender, EventArgs e)
        {

            Point p1 = this.PointToClient(MousePosition);

            this.lblPoint.Text = p1.ToString();
        }

        private void btnLove_Click(object sender, EventArgs e)
        {
            MessageBox.Show("我也爱你哦 思密达...");
            this.Close();
        }

        private void btnUnLove_MouseEnter(object sender, EventArgs e)
        {
            Random r = new Random();

            int maxHeight = this.ClientSize.Height - this.btnUnLove.Height;
            int maxWidth = this.ClientSize.Width - this.btnUnLove.Width;

            this.btnUnLove.Location = new Point(r.Next(0, maxWidth + 1), r.Next(0, maxHeight + 1));
        }



MDI窗体控件



public Form1()
        {
            InitializeComponent();

            //使用IsMdiContainer属性设置窗体Form1为容器,界面的颜色会改变
            //只要用来做主窗体使用,加入菜单实现对子窗体的范围控制
            //this.IsMdiContainer = true;
        }

        private void 现实子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 fm2 = new Form2();
            //限制子窗体在父窗体的范围内出现
            fm2.MdiParent = this;
            fm2.Show();

            Form3 fm3 = new Form3();
            //限制子窗体在父窗体的范围内
            fm3.MdiParent = this;
            fm3.Show();

            Form4 fm4 = new Form4();
            //限制子窗体在父窗体的范围内
            fm4.MdiParent = this;
            fm4.Show();

        }

        private void 纵向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //LayoutMdi在父窗体中排列子窗体
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void 横向排列ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //LayoutMdi在父窗体中排列子窗体
            LayoutMdi(MdiLayout.TileHorizontal);
        }





猜你喜欢

转载自blog.csdn.net/m0_37532448/article/details/80321569