Hack Forums
[C#] Change Form title from another Form. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Change Form title from another Form. (/showthread.php?tid=4618834)



Change Form title from another Form. - Shedletsky - Jan 3, 2015

(Using WinForms btw)
Alright so I made Options.cs open From Form1 using a toolStripButton with this code:
Code
namespace Application
{
    public partial class Form1 : Form
    {
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            Options Options = new Options();
            Options.ShowDialog();
        }
    }
}

and on the Options form I have this:

Code
namespace Application
{
    public partial class Options : Form
    {
        public void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            f1.Text = textBox1.Text;
            f1.Refresh();
            this.Hide();
        }
    }
}

And this is where the problem arises. There isn't any errors and Form1.Text remains the same, the reason I have a Refresh in there is because I noticed that if I did ShowDialog then on the second Form1 the title changed. Any suggestions?

Sorry I'm a new to C# 😊


* - +Haze - Jan 3, 2015

There's no need to instantiate form 1.
Just do.

Form1.Text = "balh blah blah";


* - Shedletsky - Jan 3, 2015

(Jan 3, 2015 08:05 PM)+Haze Wrote:  There's no need to instantiate form 1.
Just do.

Form1.Text = "balh blah blah";

Can you give me an example? I'm trying to make it that whatever the user inputs into the textbox, form1.text is changed to that.

(Jan 3, 2015 08:12 PM)Odus Wrote:  Use a sledgehammer to crack a nut why don't you?

Code
Form1.Text = textBox1.Text;

Huh? 😧


* - Odus - Jan 3, 2015

(Jan 3, 2015 08:25 PM)Shedletsky Wrote:  Can you give me an example? I'm trying to make it that whatever the user inputs into the textbox, form1.text is changed to that.

The line I just gave you does exactly that.


* - Shedletsky - Jan 3, 2015

(Jan 3, 2015 08:30 PM)Odus Wrote:  The line I just gave you does exactly that.

I receive "An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Text.get' c:\users\.."

My code:
Code
public void button1_Click(object sender, EventArgs e)
      {
      Form1.Text = textBox1.Text;
      }



* - Dialog - Jan 4, 2015

(Jan 3, 2015 08:05 PM)+Haze Wrote:  There's no need to instantiate form 1.

There is, in C#. Thankfully.

(Jan 3, 2015 09:28 PM)Odus Wrote:  My bad, hadn't touched C# in a while.
This would mean OP has to create a new form every time he wants different text on it.
Just set its text by keeping a reference to the parent form.

Options.cs:
[Image: 3QH0jY6.png]

Now use this in Form1 (or any other form):
Options options = new Options(this);

I'd rather use a sort of "input box" for this nonetheless.

E.g.

Options.cs:
[Image: SjWMyMB.png]

Form1.cs:
[Image: Dp71gL2.png]


* - Shedletsky - Jan 4, 2015

(Jan 4, 2015 02:02 AM)Dialog Wrote:  There is, in C#. Thankfully.

This would mean OP has to create a new form every time he wants different text on it.
Just set its text by keeping a reference to the parent form.

Options.cs:
[Image: 3QH0jY6.png]

Now use this in Form1 (or any other form):
Options options = new Options(this);

I'd rather use a sort of "input box" for this nonetheless.

E.g.

Options.cs:
[Image: SjWMyMB.png]

Form1.cs:
[Image: Dp71gL2.png]

Ah thanks! :D I managed to get it working! 👍