对话框控件延伸文本框制作
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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; using System.IO; namespace WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //时间按钮点击事件 private void 时间日期ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Text = DateTime.Now.ToString("HH:mm yyyy/MM/dd"); } //撤销按钮点击事件 private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo(); } //剪切按钮事件 private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut(); } //复制按钮事件 private void 复制CToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Copy(); } //粘贴按钮事件 private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Paste(); } //删除按钮事件 private void toolStripMenuItem3_Click(object sender, EventArgs e) { textBox1.SelectedText = ""; } //全选按钮事件 private void 全选AToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.SelectAll(); } //查找按钮事件 private void toolStripMenuItem1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(this); f2.Owner = this; f2.Show(); } //替换按钮点击事件 private void 替换ToolStripMenuItem_Click(object sender, EventArgs e) { Form3 f3 = new Form3(this); f3.Owner = this; f3.Show(); } //换行按钮点击事件 private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e) { if (textBox1.WordWrap) { textBox1.WordWrap = false; textBox1.ScrollBars = ScrollBars.Both; } else { textBox1.WordWrap = true; textBox1.ScrollBars = ScrollBars.Vertical; } } //字体按钮点击事件 private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult dll = fontDialog1.ShowDialog(); if (dll == DialogResult.OK) { textBox1.Font = fontDialog1.Font; } } //颜色按钮点击事件 private void 字体ToolStripMenuItem1_Click(object sender, EventArgs e) { DialogResult dll = colorDialog1.ShowDialog(); if (dll == DialogResult.OK) { textBox1.ForeColor = colorDialog1.Color; } } //打开按钮点击事件 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "文本文件|*.txt";//选择文件能打开格式,多个"文本文件|*.txt|文本文档|*.doc|所有文件|*.*" DialogResult dll = openFileDialog1.ShowDialog(); if (dll == DialogResult.OK) { //打开文件路径,以文件的默认编码方式打开 StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default); textBox1.Text = sr.ReadToEnd(); } } string save = ""; private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (save == "")//判断有没有保存路径 { DialogResult dll = saveFileDialog1.ShowDialog(); if (dll == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); sw.Write(textBox1.Text); sw.Flush(); save = saveFileDialog1.FileName; sw.Close(); } } else { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); sw.Write(textBox1.Text); sw.Flush(); sw.Close(); } } //底部菜单栏 private void textBox1_TextChanged(object sender, EventArgs e) { toolStripLabel1.Text = "字数:" + textBox1.TextLength; toolStripLabel2.Text = "行数" + (textBox1.GetLineFromCharIndex(textBox1.TextLength)+1); } } }
form1样式:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 WindowsFormsApplication4 { public partial class Form2 : Form { Form1 F1 = null; public Form2(Form1 f1) { InitializeComponent(); F1 = f1; } int z = 0; private void button1_Click(object sender, EventArgs e) { string s = F1.textBox1.Text; if (z < 0) { z = 0; } z = s.IndexOf(textBox1.Text, z); if (z >= 0) { F1.textBox1.Select(z, textBox1.TextLength); F1.Focus(); z++; } else { MessageBox.Show("找不到" + "\"" + textBox1.Text + "\""); } } } }
form2样式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 WindowsFormsApplication4 { public partial class Form3 : Form { Form1 F1 = null; public Form3(Form1 f1) { InitializeComponent(); F1 = f1; } private void button1_Click(object sender, EventArgs e) { int z = 0; string s = F1.textBox1.Text; if (s.IndexOf(textBox1.Text, z) < 0) { MessageBox.Show("找不到" + "\"" + textBox1.Text + "\""); } for (; ; ) { if (z < 0) { F1.Focus(); break; } z = s.IndexOf(textBox1.Text, z); if (z >= 0) { F1.textBox1.Select(z, textBox1.TextLength); F1.textBox1.SelectedText = textBox2.Text; z++; } } } } }
form3样式
对话框介绍主要都在Form1最后5个点击事件中,有介绍.