1

I'm working on making a program give the ability to allow the user to set a target of an active window.

I have two problems with my code, perhaps someone can let me know if the path i've chosen is wrong or there is a better path.

  1. The Window output is only showing 16 characters of the name of the process.
  2. I have the check box listed but don't know how to dynamically assign them to do the change where it will make the TextBox.Text Change.

    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 Answer 1

1

Use ((System.Windows.Forms.RadioButton)sender).Text to get the text property of your radio button:

private void radioButtons_CheckChanged(object sender, EventArgs e)
{
     textBox1.Text= ((System.Windows.Forms.RadioButton)sender).Text;
}

When an event raises, the sender cotains a reference to your control that raised the event, so you can access properties of sender control.

Sign up to request clarification or add additional context in comments.

4 Comments

Worked perfectly. Now I just need to figure out why the checkbox fields are only showing about an inch in width of the text. Like ( ) name of program but shows ( ) name o.
When creating radio buttons, set radioButtons[i].Width=200 for example.
@BrianPulaski did the problem with Width resolved?
Yes, much appreciated! still getting used to trying to do system generated functions in a form, much helpful :) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.