欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-08 22:19:16
...

一、状态栏

效果:

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

代码:

    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);
        }
    }

二、右键菜单

效果:

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

代码:

        // 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);
            }
        }

 

三、系统对话框

效果:

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

代码:

        // 打开文件对话框
        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;
            }
        }

 

相关标签: Winforms