ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

winform 系统对话框、右键菜单、状态栏(补充)

2020-12-06 17:33:40  阅读:308  来源: 互联网

标签:toolStripStatusLabel1 dlg 状态栏 fontdig 对话框 void 右键 new MyDialog


一、状态栏

效果:

代码:

    public partial class Form3 : Form
    {
        // 状态标签
        ToolStripStatusLabel toolStripStatusLabel1;
        public Form3()
        {
            InitializeComponent();
            toolStripStatusLabel1 = new ToolStripStatusLabel();
            toolStripStatusLabel1.Image = Properties.Resources.Green;
            toolStripStatusLabel1.Name = "toolStripStatusLabel1";
            toolStripStatusLabel1.Size = new Size(200, 17);
            toolStripStatusLabel1.Text = "状态";

            statusStrip1.Items.Add(toolStripStatusLabel1);
        }
    }

二、右键菜单

效果:

代码:

        // listBox 鼠标释放时触发
        private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // 坐标处,项的索引
                int index = listBox1.IndexFromPoint(e.Location);
                if (index >= 0)
                {
                    listBox1.SetSelected(index, true);
                    // 编辑,删除可以用
                    MenuItem_Edit.Enabled = true;
                    MenuItem_Del.Enabled = true;
                }
                else
                {
                    listBox1.ClearSelected();
                    MenuItem_Edit.Enabled = false;
                    MenuItem_Del.Enabled = false;
                }
                // 显示有菜单
                this.contextMenuStrip1.Show(listBox1, e.Location);
            }
        }

 

三、系统对话框

效果:

代码:

        // 打开文件对话框
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.DefaultExt = ".cs";
            dlg.Filter = "c#源文件 | *.cs";
            dlg.InitialDirectory = Path.GetFullPath(".");
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string fileName = dlg.FileName;
                MessageBox.Show("选中了:"+fileName);
            }
        }

        // 保存文件
        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "新的文本文件";
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text document (.txt) | *.txt";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string fileName = dlg.FileName;
                MessageBox.Show("选中了:" + fileName);
            }
        }

        // 选中目录
        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.SelectedPath = Path.GetFullPath(".");
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string path = dlg.SelectedPath;
                MessageBox.Show("选中了:" + path);
            }
        }

        //  选择颜色 对话框
        private void button4_Click(object sender, EventArgs e)
        {
            ColorDialog MyDialog = new ColorDialog();
            // Keeps the user from selecting a custom color.
            MyDialog.AllowFullOpen = false;
            // Allows the user to get help. (The default is false.)
            MyDialog.ShowHelp = true;
            // Sets the initial color select to the current text color.
            MyDialog.Color = button4.ForeColor;

            // Update the text box color if the user clicks OK 
            if (MyDialog.ShowDialog() == DialogResult.OK)
                button4.ForeColor = MyDialog.Color; 
        }

        // 选择字体 对话框
        private void button5_Click(object sender, EventArgs e)
        {
            FontDialog fontdig = new FontDialog();
            fontdig.ShowColor = true;
            fontdig.Font = button5.Font;
            fontdig.Color = button5.ForeColor;

            if (fontdig.ShowDialog() != DialogResult.Cancel)
            {
                button5.Font = fontdig.Font;
                button5.ForeColor = fontdig.Color;
            }
        }

 

标签:toolStripStatusLabel1,dlg,状态栏,fontdig,对话框,void,右键,new,MyDialog
来源: https://blog.csdn.net/tiz198183/article/details/110746382

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有