【问题标题】:How to save and load textBox Font in c#?如何在 C# 中保存和加载文本框字体?
【发布时间】:2021-04-17 20:45:12
【问题描述】:
我想将文本框字体保存在 txt 文件中
这是我的代码
System.IO.File.WriteAllText("path", txtNotepad.Font.ToString());
那我想通过下面的代码从TXT文件中读取字体
String fontName = System.IO.File.ReadAllText("path");
如何通过 FontName 更改 TextBox 字体?
【问题讨论】:
标签:
c#
windows
visual-studio
file
【解决方案1】:
如果您正在使用Font 类,请考虑使用提供的FontConverter,而不是尝试自己解析System.Drawing.Font.ToString() 的结果。
这是一个例子:
// Use the provided converter to get and store a culture-invariant string.
var converter = new FontConverter();
string fontInfo = converter.ConvertToInvariantString(txtNotepad.Font);
File.WriteAllText("path", fontInfo);
// Retrieve the string and set the Font property on your TextBox.
fontInfo = File.ReadAllText("path");
txtNotepad.Font = converter.ConvertFromInvariantString(fontInfo) as Font;
这种方法可以防止您重新发明轮子,并为其他文化格式提供处理。