简易记事本
加载系统字体
InstalledFontCollection myFont
= new InstalledFontCollection();
FontFamily
[] ff
= myFont
.Families
;
ArrayList list
= new ArrayList();
int count
= ff
.Length
;
for(int i
= 0; i
< count
; i
++)
{
toolStripComboBox1
.Items
.Add(ff
[i
].Name
);
}
字体加粗
if (textBox1
.Font
.Bold
== true)
{
textBox1
.Font
= new Font(textBox1
.Font
, FontStyle
.Regular
);
}
else
{
textBox1
.Font
= new Font(textBox1
.Font
, FontStyle
.Bold
);
}
字体倾斜
if (textBox1
.Font
.Italic
== true)
{
textBox1
.Font
= new Font(textBox1
.Font
, FontStyle
.Regular
);
}
else
{
textBox1
.Font
= new Font(textBox1
.Font
, FontStyle
.Italic
);
}
修改字体类型
string fontName
= toolStripComboBox1
.Text
;
float fontSize
= float.Parse(toolStripComboBox2
.Text
);
textBox1
.Font
= new Font(fontName
, fontSize
);
修改字号
private void toolStripComboBox2_SelectedIndexChanged(object sender
, EventArgs e
)
{
string fontName
= toolStripComboBox1
.Text
;
float fontSize
= float.Parse(toolStripComboBox2
.Text
);
textBox1
.Font
= new Font(fontName
, fontSize
);
}
保存
private void toolStripButton2_Click(object sender
, EventArgs e
)
{
if (textBox1
.Text
.Trim() != "")
{
if (this.Text
== "")
{
saveFileDialog1
.Filter
= ("文本文档(*.txt)|*.txt");
if (saveFileDialog1
.ShowDialog() == DialogResult
.OK
)
{
string path
= saveFileDialog1
.FileName
;
StreamWriter sw
= new StreamWriter(path
, false);
sw
.WriteLine(textBox1
.Text
.Trim());
sw
.Flush();
sw
.Close();
}
}else
{
string path
= this.Text
;
StreamWriter sw
= new StreamWriter(path
, false);
sw
.WriteLine(textBox1
.Text
.Trim());
sw
.Flush();
sw
.Close();
}
}else
{
MessageBox
.Show("空文档不能保存", "信息提示", MessageBoxButtons
.YesNo
, MessageBoxIcon
.Error
);
}
}
打开
private void toolStripButton1_Click(object sender
, EventArgs e
)
{
openFileDialog1
.Filter
= ("文本文档(*.txt)|*.txt");
if (openFileDialog1
.ShowDialog() == DialogResult
.OK
)
{
string path
= openFileDialog1
.FileName
;
StreamReader sr
= new StreamReader(path
, Encoding
.UTF8
);
string text
= sr
.ReadToEnd();
textBox1
.Text
= text
;
this.Text
= path
;
this.toolStripLabel1
.Text
= "";
sr
.Close();
}
}
退出
private void frmChild_FormClosing(object sender
, FormClosingEventArgs e
)
{
if (this.toolStripLabel1
.Text
== "*")
{
DialogResult dr
= MessageBox
.Show("文件尚未保存,确定要退出吗?", "提示", MessageBoxButtons
.YesNo
, MessageBoxIcon
.Question
);
if (dr
== DialogResult
.Yes
)
{
this.Dispose();
}else
{
e
.Cancel
=true;
}
}
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-6006.html