c# - 如何在 C# 中保存和加载文本框字体?
问题描述
我想将文本框字体保存在 txt 文件中
这是我的代码
System.IO.File.WriteAllText("path", txtNotepad.Font.ToString());
然后我想通过以下代码从 TXT 文件中读取字体
String fontName = System.IO.File.ReadAllText("path");
如何通过 FontName 更改 TextBox 字体?
解决方案
如果您正在使用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;
这种方法可以防止您重新发明轮子并为其他文化格式提供处理。
推荐阅读
- javascript - 我得到 throw new ERR_SERVER_ALREADY_LISTEN(); 当我尝试使用 node.js 运行我的程序时。我关错了吗?
- javascript - Strapi:创建导入工作的最佳方式是什么?
- python - 如何从python导出多个文件
- fortran - 主成分分析 -> 逆/反转模式
- reactjs - 如何处理 axios 错误并显示带有错误消息的模态
- c# - C# 应用程序仅在工作区文件夹中工作
- javascript - 以 navigator.userAgent 为例的 JavaScript 箭头函数
- java - 在子类中获取私有访问错误
- c# - Unity 中的我的 GameObject(Sword) 未绑定到 Player (AnotherGameObject)。它停留在 (0f, 0f, 0f)
- firebase - 如何从 firebase 颤振中获取 fieldValue.arrayUnion 数据