使动态复选框执行和动作c#
本文关键字:执行 动态 复选框 | 更新日期: 2023-09-27 18:13:09
我正在编写一个程序,允许用户设置活动窗口的目标。
我的代码有两个问题,也许有人可以让我知道,如果我选择的路径是错误的,或者有一个更好的路径。
- 窗口输出只显示16个字符的进程名。
-
我有复选框列出,但不知道如何动态地分配他们做改变,它会使文本框。文本变化。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; namespace Workspace { public partial class Form5 : Form { string target = File.ReadAllText("./target.txt"); public Form5() { InitializeComponent(); //Shows current target in textbox. string target = File.ReadAllText("./target.txt"); textBox1.Text = target; // Sets a starting point. int total_processes = 0; // Captures proccesses. Process[] processlist = Process.GetProcesses(); //looks at all proccess to separate with titles. foreach (Process process in processlist) { //calculates total proccess with titles. if (!String.IsNullOrEmpty(process.MainWindowTitle)) { total_processes = total_processes + 1; } } // Sets up string array total by number of processes with name. string[] stringArray = new string[total_processes]; //Names each proccess array. int loopnum = 0; foreach (Process process in processlist) { if (!String.IsNullOrEmpty(process.MainWindowTitle)) { stringArray[loopnum] = process.MainWindowTitle; loopnum = loopnum + 1; } } // Generates # of Radio buttons per proccess with name. System.Windows.Forms.RadioButton [] radioButtons = new System.Windows.Forms.RadioButton[total_processes]; for (int i = 0; i < total_processes; ++i) { radioButtons[i] = new RadioButton(); radioButtons[i].Text = stringArray[i]; radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20); radioButtons[i].CheckedChanged += new EventHandler(this.radioButtons_CheckChanged); this.Controls.Add(radioButtons[i]); } } private void radioButtons_CheckChanged(object sender, EventArgs e) { // Dynamic Check box if checked changes textBox1.Text to radioButtons[i].Text } private void button1_Click(object sender, EventArgs e) { System.IO.StreamWriter file = new System.IO.StreamWriter("./target.txt"); file.WriteLine(textBox1.Text); file.Close(); } } }
使用((System.Windows.Forms.RadioButton)sender).Text
获取单选按钮的文本属性:
private void radioButtons_CheckChanged(object sender, EventArgs e)
{
textBox1.Text= ((System.Windows.Forms.RadioButton)sender).Text;
}
当事件引发时,发送方包含对引发该事件的控件的引用,因此您可以访问发送方控件的属性。