Member Avatar for Elemen7s

Hi, ive just started C# and have a little problem. The below code is the program.

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;

namespace killer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {
//when this button is clicked, the console opens with a list of the processes
                string str = @"G:\Killer\Finder.exe";
                Process process = new Process();
                process.StartInfo.FileName = str;
                process.Start();
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
//This is just the File > Exit option
            Application.Exit();
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
//now I need that when the user inputs text in "texBox" , the text entered will save in the var "killname" 
            string killname;
            killname = textBox.Text;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            {
                Process[] procs = Process.GetProcessesByName("killname"); // Then here instead of killname (the var) it inputs the content of the var and kill that process

                foreach (Process proc in procs)
                    proc.Kill();
            }
        }

       

        

       

    }
}

What am I doing wrong ?

Thanks in advanced

Hi Elemen7s! Welcome at DaniWeb!
string killname; is locally defined in the textBox_TextChanged method. So as the method is finished, killname does not exist any more. You cannot use it somewhere else.

And also you have put killname inside doublequotes ( " ).

Here's the working version:

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;

namespace killer
{
    public partial class Form1 : Form
    {

         [B]string killname = String.Empty;[/B]

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            {
//when this button is clicked, the console opens with a list of the processes
                string str = @"G:\Killer\Finder.exe";
                Process process = new Process();
                process.StartInfo.FileName = str;
                process.Start();
            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
//This is just the File > Exit option
            Application.Exit();
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
//now I need that when the user inputs text in "texBox" , the text entered will save in the var "killname" 
            killname = textBox.Text;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            {
               [B] Process[] procs = Process.GetProcessesByName(killname);[/B] // Then here instead of killname (the var) it inputs the content of the var and kill that process

                foreach (Process proc in procs)
                    proc.Kill();
            }
        }
Member Avatar for Elemen7s

Thanks for helping.

i really appreciate it.

Ill try it out and get back

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.