Results 1 to 12 of 12

Thread: [RESOLVED] Going nuts about changing a label text...

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Resolved [RESOLVED] Going nuts about changing a label text...

    I have tried for like 3 hours now to change the text of label, but I haven't succeeded.

    I have tried numerous ways, Invoking, Defining, Subs etc. etc.

    I have 2 forms. Form1 and Form2. Form2 is opened by Form1 via ShowDialog. When click a button on Form2 I to change the text of the label on Form1. Form2 also gets closed by that button.

    And what I've found out, the text gets changed, but It doesn't appear. The control don't want to display it too me

    Pleeease anyone, I seriously need help

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Going nuts about changing a label text...

    Can you show us the code ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Going nuts about changing a label text...

    I only have code from the latest method I tried, "Subbing":

    Form1 is MKVC, Form2 is Settings

    I call this in the buttonclick on Form2:

    Code:
    MKVC form1 = new MKVC();
                form1.CalcChapNumber(MKVC.duration);
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
    Here is the CalcChapNumber sub:

    Code:
            public void CalcChapNumber(int runtime)
            {
    
                decimal count = runtime / trackBar1.Value;
    
                if (count < 0)
                {
    
                    MessageBox.Show("Too high interval!");
                    trackBar1.Value = Properties.Settings.Default.defChapInterval;
    
                }
    
                else
                {
    
                    decimal roundoff = Math.Floor(count);
                    
                    if (Properties.Settings.Default.firstChap00)
                    {
                        roundoff += 1;
                    }
    
                    lblChapterCount.Text = roundoff.ToString();
    
                }
    I have solved it temporarily now by setting a oublic variable on form1 from form2 and then using the DialogResult thingy to check if it should use that variable, and it works. The label changes. But I feel I would like to learn the real way of doing this, and I also need to call a sub on form1 from form2.

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Going nuts about changing a label text...

    the reason you don't see the label get updated is because you create new instance of form1...

    new instance meaning another instance of form1.. you need to pass the original instance of from1 and changes it's.
    Last edited by motil; Feb 6th, 2011 at 01:39 PM.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Going nuts about changing a label text...

    ok I made small example for you:

    Form1:
    Code:
     public partial class Form1 : Form
        {
    
          
            public Form1()
            {
                InitializeComponent();
            }
            // public method for changing the label
            public void ChangeLabel(string message)
            {
                this.label1.Text = message;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // passing the original instance of Form1 to Form2 Constructor 
                Form2 f = new Form2(this);
                f.Show();
            }
        }
    and now form 2:

    Code:
     public partial class Form2 : Form
        {
            //declare private member of form1 instance
            private Form1 _MainForm { set; get; }
    
            public Form2()
            {
                InitializeComponent();
            }
    
           //  constructor that gets Form1 as parameter and set it to From2 private member which we //delcared above
            public Form2(Form1 MainForm) 
            {
                this._MainForm = MainForm;
            }
    
    
    
            private void Form2_Load(object sender, EventArgs e)
            {
                // if _MainForm is not null change Form1 label 
                if (_MainForm != null)
                {
                    _MainForm.ChangeLabel("Hello World");
                }
            }
        }
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  6. #6

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Going nuts about changing a label text...

    thank you I will take a look and try to learn!

  7. #7

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Going nuts about changing a label text...

    I looked through the code and used it, but there is one problem.
    Are you really passing the original instance when you declare:

    Form2 f = new Form2(this);

    Feels like the "new" thingy makes it a new instance, and in fact, the form that shows up if I after that call f.ShowDialog(); is just an empty new form :S

  8. #8
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Going nuts about changing a label text...

    the new is creating new instance of Form2, the "this" is the instance of Form1.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  9. #9

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Going nuts about changing a label text...

    Okay now I see, but why won't the controls show up then?

  10. #10
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Going nuts about changing a label text...

    my mistake, the controls are not showing up because in the constructor which get the From1 parameter not calling InitializeComponent() which initialize the form controls. so you have two options:

    1. chin the constructors like so:
    Code:
     public Form2(Form1 MainForm) : this()
            {
                this._MainForm = MainForm;
            }
    note the "this()" it make sure that the default constructor will be called, or you can simply add the metohd into both constructors:

    Code:
     public Form2(Form1 MainForm) 
            {
                 InitializeComponent();
                this._MainForm = MainForm;
            }
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  11. #11

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Going nuts about changing a label text...

    I went with number one and it works flawlessly now, thank you!

  12. #12
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Going nuts about changing a label text...

    No problem, glad I could help.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width