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,这样我们才能够调用。
多多指正!!!!