-1

like i said in the title i want to change the text of a selected window using C#. (not only the forms/windows that i have in my application) Is it possible?

especially for anyone who doesent understand, example: i open notepad, and notepad is selected. i click on some other window and then it is selected.

3 Answers3

1

you can use the Text property

public partial class FormMain : Form
{
    public FormMain()
    {
        InitializeComponent();
        this.Text = "This Is My Title";
    }
}
Fady Sadek
  • 1,091
  • 1
  • 13
  • 22
1

Selected Window? If by selected you mean active form then try this:-

 foreach(Form frm in Application.OpenForms)
 {
            if (frm.TopMost)
            {
                frm.Text = "Your title";
            }
 }

Edit: Try this code. This will rename title of any windows process. I have used notepad and wordpad as example

private void button1_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("notepad");
            if (processes.Length > 0)
            {
                SetWindowText(processes[0].MainWindowHandle, "This is My Notepad");
                // Renaming title of 1st notepad instance
                //processes[0]
            }
            else
            {
                MessageBox.Show("Please start atleast one notepad instance..");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcessesByName("wordpad");
            if (processes.Length > 0)
            {
                SetWindowText(processes[0].MainWindowHandle, "This is My wordpad");
                // Renaming title of 1st notepad instance
                //processes[0]
            }
            else
            {
                MessageBox.Show("Please start atleast one wordpad instance..");
            }
        }
Sumit Parakh
  • 1,098
  • 9
  • 19
0

The title of a window can be changed by setting the Text property of the form, which is documented here.

Codor
  • 17,447
  • 9
  • 29
  • 56