使动态复选框执行并执行操作c#

时间:2015-09-17 22:34:27

标签: c# winforms

我正致力于让程序能够让用户设置活动窗口的目标。

我的代码存在两个问题,如果我选择的路径错误或有更好的路径,也许有人可以告诉我。

  1. 窗口输出仅显示进程名称的16个字符。
  2. 我已经列出了复选框,但不知道如何动态分配它们以进行更改,使TextBox.Text发生更改。

    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();
        }               
    }
    }
    

1 个答案:

答案 0 :(得分:1)

使用moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")).normalized; float _speed = Input.GetKey(KeyCode.LeftShift) ? runspeed : speed; p.AddForce(moveDirection * _speed * Time.deltaTime); 获取单选按钮的text属性:

((System.Windows.Forms.RadioButton)sender).Text

当事件发生时,发件人会提交对引发事件的控件的引用,以便您可以访问发件人控件的属性。