日期:2014-05-18 浏览次数:21015 次
private void button1_Click(object sender, EventArgs e) { //打开FontDialog 选择字体样式并保存 FontDialog fd = new FontDialog(); fd.ShowColor = true; if (fd.ShowDialog() == DialogResult.OK) { object[] obj = new object[] { fd.Font, fd.Color }; BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = File.Open("aa.dll", FileMode.Create)) { formatter.Serialize(stream, obj); } } } private void button2_Click(object sender, EventArgs e) { object[] obj; BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = File.Open("aa.dll", FileMode.Open)) { obj = (object[])formatter.Deserialize(stream); } Font font = (Font)obj[0]; Color color = (Color)obj[1]; //读取保存的字体样式信息并设置 this.textBox1.Font = font; this.textBox1.ForeColor = color; }
------解决方案--------------------
formload 事件读取处加上判断
youPath1 = Application.StartupPath + @"\\文字格式.dll"; if (File.Exists(youPath1)) { object[] obj; BinaryFormatter formatter = new BinaryFormatter(); using (FileStream stream = File.Open(youPath1, FileMode.Open)) { obj = (object[])formatter.Deserialize(stream); } Font font = (Font)obj[0]; Color color = (Color)obj[1]; txt1.Font = font; }