0

Admin is that form that have a label an i want to access it during the runtime from another form or another user control

public partial class Admin : Form
{
    public Admin()
    {
        InitializeComponent();


    }
    public Label lbl
    {
        get { return label8; }
        set { label8.Text = value.ToString(); }

    }
}

User control:

Admin.lbl.text="something";  //could i do this and how ??

but if i made a new instance , i should close the old one to show changes but i don't want to make that during all time of the Run time

  • Possible duplicate of [How to access a form control for another form?](https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) also https://stackoverflow.com/q/14000090/1070452. There are a great many here – Ňɏssa Pøngjǣrdenlarp Dec 04 '18 at 21:55
  • what if the second form is a user control ? – Abanoub Moris Dec 04 '18 at 22:05
  • A form is not and cannot be a user control. They are 2 different things. You cant create (make) an instance of a user control that is usable without adding it to a form, so there is another form involved somewhere. – Ňɏssa Pøngjǣrdenlarp Dec 04 '18 at 22:10
  • the first form include the user conrol so i want when i click that user control making some changes in that form could i do this – Abanoub Moris Dec 04 '18 at 22:14
  • Any help plz.. ? – Abanoub Moris Dec 04 '18 at 22:28
  • If you want to make changes in "that form" - the same one that contains the user control? - then you dont need any new instance of anything. If it is a different form, the fact that things started on/with a UserControl is beside the point and this is a duplicate of many many posts here. – Ňɏssa Pøngjǣrdenlarp Dec 04 '18 at 22:32
  • i have many of this user control in my form and i want when i click in on of them making changes in the form – Abanoub Moris Dec 04 '18 at 22:48

1 Answers1

0

Abanoub - If I understand what you are trying to do - you want to set the label of an already displayed form without creating a new instance of the form. At least one way to do that would be with a singleton class that holds the form instance. So there will only be one instance of the form. Please try the following:

First, we create a singleton class that keeps the form instance:

public class Singleton
{
    // Modified from: http://csharpindepth.com/articles/general/singleton.aspx

    // This will keep ONE instance of the Admin Form
    private Admin _adminForm; 
    public Admin AdminForm
    {
        get
        {
            if (_adminForm == null)
            {
                _adminForm = new Admin(); 
            }
            return _adminForm; 
        }
    }
    private static Singleton instance = null;

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

Now you instantiate the from from this instance - for example:

Button 1 will display the form:

    private void button1_Click(object sender, EventArgs e)
    {
        var singleton = Singleton.Instance;
        var f = singleton.AdminForm; 
        f.Show(); 
    }

Button 2 will set the already displayed form's label (BTW - I think you want the property to set the text of the label not the label right?)

    private void button2_Click(object sender, EventArgs e)
    {
        // Assuming you clicked button 1 first, 
        // this will not cause a new instance but use the existing one
        var singleton = Singleton.Instance;
        var f = singleton.AdminForm;
        f.LabelText = "Hello world!";
    }

Assuming you want to set the text of the label - here is the modified property in Admin:

    public string LabelText
    {
        get { return label8.Text; }
        set { label8.Text = value; }
    }

I hope this will be helpful to you - good luck!!

Jon Vote
  • 604
  • 5
  • 17