C# 鼠标点击ToolStripStatuslabel 在线修改Text属性并存储加载显示Text属性
在实际项目中为方便了解视觉软件的使用性,可能需要添加一些小而稍微实用的功能:一个StipStatus控件上的Label按钮属性Text需要修改并保存,软件重启后能够自动加载修改后的属性名。
定义变量
public static string controlsText = System.Windows.Forms.Application.StartupPath + "\\相关属性\\";
//子窗体窗口属性名修改
private string FilePathText = VProClass.controlsText + "窗口属性名.txt";
主窗体中的代码
#region 修改子窗体窗口属性 名
private void SaveStatusLabelsText(string text, int toolRow)
{
// 读取文件的所有行
string[] rowLines = System.IO.File.ReadAllLines(FilePathText);
// 创建一个新的字符串列表,用于存储更新后的内容
List<string> newLines = new List<string>();
try
{
// 插入内容到指定位置
if (rowLines.Length >= 0)//需要考虑文本为空的情况
{
using (StreamWriter writer = System.IO.File.AppendText(FilePathText))
{
if (toollabelTag == 1)
writer.WriteLine(text);
if (toollabelTag == 2)
writer.WriteLine(text);
if (toollabelTag == 3)
writer.WriteLine(text);
if (toollabelTag == 4)
writer.WriteLine(text);
if (toollabelTag == 5)
writer.WriteLine(text);
if (toollabelTag == 6)
writer.WriteLine(text);
}
}
// 插入内容到指定位置
if (rowLines.Length == Convert.ToInt32(ValuePropagation.count))
{
for (int g = 0; g < rowLines.Length; g++)
{
if (g == toolRow - 1)
{
rowLines[g] = text;
}
//newLines.Add(rowLines[g]);
}
// 将更新后的内容写回到文件中
System.IO.File.WriteAllLines(FilePathText, rowLines);
}
}
catch (Exception ex)
{
MessageBox.Show($"保存文本时出错:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadStatusLabelsText()
{
if (System.IO.File.Exists(FilePathText))
{
try
{
using (StreamReader reader = new StreamReader(FilePathText))
{
// 读取文件的所有行到一个字符串数组中
string[] lines = System.IO.File.ReadAllLines(FilePathText);
for (int k = 0; k < lines.Length; k++)
{
if (k == 0)
displayControls[0].相机编号1.Text = reader.ReadLine();
if (k == 1)
displayControls[1].相机编号2.Text = reader.ReadLine();
if (k == 2)
displayControls[2].相机编号3.Text = reader.ReadLine();
if (k == 3)
displayControls[3].相机编号4.Text = reader.ReadLine();
if (k == 4)
displayControls[4].相机编号5.Text = reader.ReadLine();
if (k == 5)
displayControls[5].相机编号6.Text = reader.ReadLine();
}
}
}
catch (Exception ex)
{
MessageBox.Show($"加载文本时出错:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public void clearLabelText(int toolTag)
{
if (System.IO.File.Exists(FilePathText))
{
try
{
using (StreamReader reader = new StreamReader(FilePathText))
{
// 读取文件的所有行到一个字符串数组中
List<string> linesRow = new List<string>(System.IO.File.ReadAllLines(FilePathText));
if (toolTag == 1 && toolTag < linesRow.Count)
{
string specifiedLineRow = linesRow[toolTag - 1];
if (specifiedLineRow != null)
{
linesRow.RemoveAt(toolTag - 1);//删除指定行
}
}
if (toolTag == 2 && toolTag < linesRow.Count)
{
string specifiedLineRow = linesRow[toolTag - 1];
if (specifiedLineRow != null)
{
linesRow.RemoveAt(toolTag - 1);//删除指定行
}
}
if (toolTag == 3 && toolTag < linesRow.Count)
{
string specifiedLineRow = linesRow[toolTag - 1];
if (specifiedLineRow != null)
{
linesRow.RemoveAt(toolTag - 1);//删除指定行
}
}
if (toolTag == 4 && toolTag < linesRow.Count)
{
string specifiedLineRow = linesRow[toolTag - 1];
if (specifiedLineRow != null)
{
linesRow.RemoveAt(toolTag - 1);//删除指定行
}
}
if (toolTag == 5 && toolTag < linesRow.Count)
{
string specifiedLineRow = linesRow[toolTag - 1];
if (specifiedLineRow != null)
{
linesRow.RemoveAt(toolTag - 1);//删除指定行
}
}
if (toolTag == 6 && toolTag < linesRow.Count)
{
string specifiedLineRow = linesRow[toolTag - 1];
if (specifiedLineRow != null)
{
linesRow.RemoveAt(toolTag - 1);//删除指定行
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"加载文本时出错:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void 相机编号1_Click(object sender, EventArgs e)
{
//输入Text属性名
var label = sender as ToolStripStatusLabel;
toollabelTag = 1;
if (label != null)
{
// 弹出输入框让用户输入新的文本
input = Microsoft.VisualBasic.Interaction.InputBox("请输入新的文本:", "修改子窗体名", label.Text);
if (!string.IsNullOrEmpty(input))
{
label.Text = input;
clearLabelText(toollabelTag);
// 保存修改后的文本
SaveStatusLabelsText(label.Text, toollabelTag);
}
}
}
#endregion
主窗体Load事件中添加
//修改子窗体名
displayControls[0].相机编号1.Click += 相机编号1_Click;
// 加载保存的文本
LoadStatusLabelsText();