WinForms:如何获取触发事件的按钮名称用于文本文件保存
解决WinForms中保存时获取当前选中预设按钮名称的问题
核心思路
通过在Form类中添加一个类级字段,记录用户最后点击的Preset按钮名称,后续保存操作直接读取该字段即可定位目标文件。
具体修改步骤
1. 添加类级字段记录当前预设
在fmMain类的顶部添加私有字段,用于存储最后点击的Preset按钮名称:
private string _currentPresetName;
2. 在预设按钮点击事件中更新字段
修改btnPreset_Click方法,加载文件时同步记录当前按钮的名称:
private void btnPreset_Click(object sender, EventArgs e) { string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ShirohanaAi\ShirohanaPresets"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } Button btn = (Button)sender; // 记录当前选中的预设按钮名称 _currentPresetName = btn.Name; string textFile = path + @"\"+ btn.Name + ".txt"; if (File.Exists(textFile)) { string[] line = File.ReadAllLines(textFile); rtbPrompt.Text = line[0]; rtbNegative.Text = line[1]; } else { MissingPresetError(); } }
3. 在保存按钮事件中使用记录的预设名称
修改btnSave_Click方法,读取记录的预设名称构建文件路径,并处理未选中预设的边界情况:
private void btnSave_Click(object sender, EventArgs e) { // 检查是否已选中预设 if (string.IsNullOrEmpty(_currentPresetName)) { MessageBox.Show("请先选择一个预设按钮!", "未选择预设"); return; } string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ShirohanaAi\ShirohanaPresets"; string textFile = path + @"\" + _currentPresetName + ".txt"; // 将编辑后的内容写入对应文件 using (StreamWriter sw = File.CreateText(textFile)) { sw.WriteLine(rtbPrompt.Text); sw.WriteLine(rtbNegative.Text); } MessageBox.Show("保存成功!", "提示"); }
4. 移除无用代码
原代码中btnGetID方法无实际作用,直接删除该方法,并在CreateButtons方法中移除对应的事件绑定:
// 删除这一行事件绑定 // newButton.Click += new EventHandler(btnGetID);
修改后的完整代码
using System; using System.IO; using System.Windows.Forms; namespace Shirohana { public partial class fmMain : Form { // 记录当前选中的预设按钮名称 private string _currentPresetName; public fmMain() { InitializeComponent(); } public async void WaitSomeTime(Form item) { await Task.Delay(3000); item.Close(); } private void Form1_Load(object sender, EventArgs e) { // Set up the prompt text box int xPrompt, yPrompt; rtbPrompt.Font = lblPrompt.Font; pnlPrompt.Width = rtbPrompt.Width + 10; pnlPrompt.Height = rtbPrompt.Height + 10; xPrompt = rtbPrompt.Location.X; yPrompt = rtbPrompt.Location.Y; pnlPrompt.Location = new Point(xPrompt-5,yPrompt-5); lblPrompt.Location = new Point(xPrompt-5,yPrompt-lblPrompt.Height-5); int xNegative, yNegative; rtbNegative.Font = lblPrompt.Font; pnlNegative.Width = rtbNegative.Width + 10; pnlNegative.Height = rtbNegative.Height + 10; xNegative = rtbNegative.Location.X; yNegative = rtbNegative.Location.Y; pnlNegative.Location = new Point(xNegative - 5, yNegative - 5); lblNegative.Location = new Point(xNegative - 5, yNegative - lblNegative.Height - 5); CreateButtons(); } public void MissingPresetError() { MessageBox.Show("Please make sure Preset text file exists and is placed in the ShirohanaPresets folder.", "Preset File Missing!"); } public void CreateButtons() { string fpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ShirohanaAi\ShirohanaPresets"; int FileNumber = 1; int fileCount = 20-1; for (int i = 0; i < fileCount; i++) { FileNumber++; Button newButton = new Button(); this.Controls.Add(newButton); newButton.Text = "PST "+FileNumber; newButton.Size = new Size(105, 50); newButton.Font = Preset1.Font; newButton.FlatStyle = FlatStyle.Flat; newButton.FlatAppearance.BorderSize = 0; newButton.BackColor = ColorTranslator.FromHtml("206, 100, 0"); newButton.ForeColor = ColorTranslator.FromHtml("251, 248, 248"); newButton.Margin = new Padding(8, 8, 8, 8); newButton.Name = "Preset"+FileNumber; newButton.Click += new EventHandler(btnPreset_Click); pnlPresets.Controls.Add(newButton); string create_path = fpath + @"\Preset" + FileNumber + ".txt"; if (!File.Exists(create_path)) { using (StreamWriter sw = File.CreateText(create_path)) { sw.WriteLine("PROMPT HERE - PRESET "+FileNumber); sw.WriteLine("NEGATIVE HERE"); } } } } private void btnPreset_Click(object sender, EventArgs e) { string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ShirohanaAi\ShirohanaPresets"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } Button btn = (Button)sender; _currentPresetName = btn.Name; string textFile = path + @"\"+ btn.Name + ".txt"; if (File.Exists(textFile)) { string[] line = File.ReadAllLines(textFile); rtbPrompt.Text = line[0]; rtbNegative.Text = line[1]; } else { MissingPresetError(); } } private void btnSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(_currentPresetName)) { MessageBox.Show("请先选择一个预设按钮!", "未选择预设"); return; } string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\ShirohanaAi\ShirohanaPresets"; string textFile = path + @"\" + _currentPresetName + ".txt"; using (StreamWriter sw = File.CreateText(textFile)) { sw.WriteLine(rtbPrompt.Text); sw.WriteLine(rtbNegative.Text); } MessageBox.Show("保存成功!", "提示"); } #region Copy Buttons private void btnCopyPrompt_Click(object sender, EventArgs e) { if (rtbPrompt.Text != "") { System.Windows.Forms.Clipboard.SetText(rtbPrompt.Text); new ToolTip().Show("Copied Prompt Info!",this, rtbPrompt.Location.X + 100 , rtbPrompt.Location.Y, 1000); } else { new ToolTip().Show("Empty!", this, rtbPrompt.Location.X + 100, rtbPrompt.Location.Y, 1000); } } private void btnCopyNegative_Click(object sender, EventArgs e) { if (rtbNegative.Text != "") { System.Windows.Forms.Clipboard.SetText(rtbNegative.Text); new ToolTip().Show("Copied Negative Prompt Info!",this, rtbNegative.Location.X + 170, rtbNegative.Location.Y, 1000); } else { new ToolTip().Show("Empty!", this, rtbNegative.Location.X + 170, rtbNegative.Location.Y, 1000); } } #endregion } }
内容的提问来源于stack exchange,提问作者ShirohanaAi
相关产品推荐
相关产品推荐

