通用对话框,调用另一个窗体,查找对话框

Form1:


源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sy22
{
    public partial class Form1 : Form
    {
        private Form2 dlg;          //首先定义一个form2
        public Form1()
        {
            InitializeComponent();
            dlg = new Form2();      //申明form2
            dlg.Owner = this;       //form2属于form1
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();//打开文件对话框
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();//保存文件对话框
        }

        private void 浏览ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();//浏览文件对话框
        }

        private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)//字体对话框
        {
            fontDialog1.Font = textBox1.Font;
            if (fontDialog1.ShowDialog() == DialogResult.OK)
                textBox1.Font = fontDialog1.Font;
           
        }

        private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)//颜色对话框
        {
            colorDialog1.Color = textBox1.ForeColor;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
                textBox1.ForeColor=colorDialog1.Color;
        }

        private void 自定义ToolStripMenuItem_Click(object sender, EventArgs e)//自定义对话框,当点击自定义时,调用form2,
        {
            dlg.Text = "查找";
            dlg.ShowDialog();//调用对话框的ShowDialog(),调用form2
        }
    }
}


一、字体对话框

首先在窗体中添加一个字体对话框fontDialog1 ,

        fontDialog1.Font = textBox1.Font;
  //将字体对话框中的字体样式设置为你,控件中的字体样式,
          if (fontDialog1.ShowDialog() == DialogResult.OK)
                textBox1.Font = fontDialog1.Font;
   //当你点击字体对话框中确定的时候,控件中的字体样式就会改变

二、颜色对话框

colorDialog1
同理字体对话框的设置

Form2:

源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sy22
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
                MessageBox.Show("请输入源内容");
            else
            {
                Form1 mf = (Form1)this.Owner;//在这里需要调用form1,所以申明form1,且form1是form2的主人
                if (mf.textBox1.Text.IndexOf(textBox1.Text, 0) == -1)
                {
                    MessageBox.Show("已到达文本末尾未找到", "提示", MessageBoxButtons.OK);
                }
                else
                {
                    this.Close();
                    mf.textBox1.Select(mf.textBox1.Text.IndexOf(textBox1.Text, 0), textBox1.TextLength);
                    mf.Activate();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

查找对话框:


 if (textBox1.Text == "")
                MessageBox.Show("请输入源内容");
//当我们输入的查找内容为空的时候,会出现提示信息,提示我们要输入内容。
            else
            {
                Form1 mf = (Form1)this.Owner;//在这里需要调用form1,所以申明form1,且form1是form2的主人
                if (mf.textBox1.Text.IndexOf(textBox1.Text, 0) == -1)//如果返回的是-1代表没有找到
                {
                    MessageBox.Show("已到达文本末尾未找到", "提示", MessageBoxButtons.OK);
                }
                else
                {
                    this.Close();//关闭当前窗口
                    mf.textBox1.Select(mf.textBox1.Text.IndexOf(textBox1.Text, 0), textBox1.TextLength);
//如果找到了,就选中找到的 mf.textBox1.Select();方法,第一个属性是开始索引,第二个属性是长度。

                    mf.Activate();
                }
            }

注意:

在这里有一些要注意的地方,在form2中调用form1中的控件,一开始我无论如何都调用不了form1中的textBox1控件,最后发现form1中的textBox1的访问属性是私有的 ,所以我们不能访问。Modifiers属性改成public,这样我们才能够调用。


多多指正!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不染心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值