-2

I have 2 Winforms (on Visual C#). On Form1 I have a button and when the user clicks it, I want the text of Form2 to change,before I open Form2 (The text that appears on the top left cornet of my Winform).

I tried these (1) (2) but they don't work.

On Form2 I have

public string formtext
{
   get {return this.Text;}
   set {this.Text = value;}
}

This is my button on Form1

public void kryptonButton2_Click(object sender, EventArgs e)
{             
    // Form2
    Form2 form2 = new Form2();
    form2.Text = "Η πόλη του Πειραιά";
 }

Note that I click that button to change the text and then I click on another button to open Form2.

Form2 opens, but the text isn't changed.

Community
  • 1
  • 1
Shevliaskovic
  • 1,562
  • 4
  • 26
  • 43

4 Answers4

1

Your Form2 instance has to be accessible from your text changing routine.

private Form2 m_form2;

public Form1() {
  InitializeComponent();
  m_form2 = null;
}

Now that you have the ground work laid, you will need to call your m_form2 object using Show() and NOT ShowDialog():

private void ShowForm2(string optionalText) {
  if (m_form2 == null) {
    m_form2 = new Form2();
    m_form2.Show();
  } else {
    m_form2.Focus();
  }
  if (!String.IsNullOrEmpty(optionalText)) {
    m_form2.Text = optionalText;
  }
}

With this setup, your button should work for Form2 by modifying your routine to do this:

public void kryptonButton2_Click(object sender, EventArgs e) {             
  ShowForm2(null);
  m_form2.Text = "Η πόλη του Πειραιά";
}

OR using the optionalText parameter:

public void kryptonButton2_Click(object sender, EventArgs e) {             
  ShowForm2("Η πόλη του Πειραιά");
}

You could also do this using delegates. This is a very powerful feature of C#. If you would like to see a code example of that, look at my answer to this question here:

https://stackoverflow.com/a/19146929/153923

UPDATE

It sounds like you may only need an updated version of Mike Cheel's answer.

Try:

public void kryptonButton2_Click(object sender, EventArgs e) {             
    Form2 form2 = new Form2();
    form2.formtext = "Η πόλη του Πειραιά";
    form2.Show(); // Mike left this part out
 }
Community
  • 1
  • 1
  • @Shevliaskovic - I have updated this answer to show how to set using an optional text parameter. –  Dec 02 '13 at 15:39
  • I can only achieve this if I open the new form with the same button that I change the text? – Shevliaskovic Dec 02 '13 at 15:44
  • I was reading the comments you made on Servy's answer, and edited my answer to reflect what it sounds like you are trying to accomplish. If that is all you want to do, try a slightly edited version of Mike Cheel's answer. I will have that added as an update to my answer in about 30 seconds.... –  Dec 02 '13 at 16:19
0

Try:

public void kryptonButton2_Click(object sender, EventArgs e)
{             
    // Form2
    Form2 form2 = new Form2();
    form2.formtext = "Η πόλη του Πειραιά";
 }
Mike Cheel
  • 12,626
  • 10
  • 72
  • 101
0

There are a couple of reasons why your approach doesn't work.

public void kryptonButton2_Click(object sender, EventArgs e)
{             
 Form2 form2 = new Form2(); //<-- this is a new instance for Form2
 form2.Text = "Η πόλη του Πειραιά"; //<-- and this is not your propery 
                                    //(as pointed out by @MikeCheel)
}

If you have no instance of Form2 then you could try and get it from Application.OpenForms but that is a HACK.

var frm2 = Application.OpenForms.Cast<Form>()
                      .FirstOrDefault(c => c.Name == "Form2"); 
if(frm2 != null)
 form2.formtext= "Η πόλη του Πειραιά"; 
Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
0

When you click the other button to open the form you need to hold onto the reference to it, so that your other button click event can use it:

private Form2 child;
public void openOtherForm_Click(object sender, EventArgs e)
{             
    child = new Form2();
    child.Show();
}

Now you can use that field to manipulate it:

public void kryptonButton2_Click(object sender, EventArgs e)
{             
    form2.formtext = "Η πόλη του Πειραιά";
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • So first I have to open Form2 and then to press the button on Form1? – Shevliaskovic Dec 02 '13 at 15:32
  • @Shevliaskovic That's what *you* said *you* wanted to happen. If you want something else to happen you'll need to describe what it should be. – Servy Dec 02 '13 at 15:33
  • Maybe I wasn't clear. I want it to change before I open it. I'll add this to my post – Shevliaskovic Dec 02 '13 at 15:34
  • @Shevliaskovic If you haven't created it then it doesn't exist. Just store the string as an instance field so you can set the text when you *do* create the form. – Servy Dec 02 '13 at 15:35
  • No, I have created it. When I run my program, Form1 shows up. The user clicks the button on this form that changes the text of Form2, then clicks another button and opens Form2 – Shevliaskovic Dec 02 '13 at 15:37
  • @Shevliaskovic Which means that the second form *doesn't* exist yet. Just do what I described in my comment and store the string for when the form does exist. – Servy Dec 02 '13 at 15:39