user2307236
user2307236

Reputation: 755

adding controls to a Windows Forms Form

I'm struggling to correctly add controls (actually these are objects which I have created) to the panel in the Form1.cs. Example, MusicNote class is a picture box object which when it is instantiated FROM FORM1_LOAD METHOD and added to MusicStaff object (another class) which is then added to panel it works fine. See below...

MusicStaff ms;

private void Form1_Load(object sender, EventArgs e)
{
    ms = new MusicStaff();
    panel2.Controls.Add(ms);

    //Test of Music Note
    MusicNote musNote = new MusicNote(1, "");
    ms.Controls.Add(musNote);

The problem is that I want the MusicNote to be created and Added to ms and panel2 (which are different classes) when a button is pressed. I have a button click event on another class which is working fine. The problem is that ms and panel2 are not accessible from that class and I don't know how to get access to them. I know that if I create new instances of MusicStaff and Form1 they will be new object and not the actual ones.

        protected void MusKey_Click(object sender, EventArgs e)
        {
            this.BackColor = Color.Green;
            txt1.Text = Convert.ToString(musicNote); //To test if musicNote refers to   the correct pitch integer.
            MusicNote musNote = new MusicNote(this.musicNote, " ");
            //HERE I NEED THE CODE TO ADD MUSNOTE TO MUSICSTAFF WHICH IS ALREADY ADDED TO THE PANEL IN FORM1.CS }

I hope I explained what I need clearly enough...

Upvotes: 1

Views: 211

Answers (3)

groverboy
groverboy

Reputation: 1135

The problem is that ms and panel2 are not accessible from that class and I don't know how to get access to them. This is because a Form's controls have private visibility by default. Generally this is a good thing, to prevent excessive coupling between a Form and other objects, including other Forms. You can change a control's Visibility property using the VS form designer.

A better approach in my opinion is to make each object as self-sufficient as possible, to minimise dependency between objects (as discussed in the link above). So for example, the Form that displays/edits the music staff should be fully responsible for creating/moving/deleting the staff and any notes or other notation on the staff. Then other objects can ask the staff Form to make changes on the staff instead of making the changes directly. You could do this by adding a public/internal method to Form1, AddMusicNote(int value, string description), then call it from the other class/Form :

protected void MusKey_Click(object sender, EventArgs e)
{
    this.BackColor = Color.Green;
    txt1.Text = Convert.ToString(musicNote); //To test if musicNote refers to   the correct pitch integer.
    form1.AddMusicNote(musicNote, " ");
}

Upvotes: 0

Terry Lewis
Terry Lewis

Reputation: 407

You can do this in one of two ways. Easy one first:

Just declare your MusicStaff control outside of the Form1_Load method (make it a field on the form).

MusicStaff ms = null;

private void Form1_Load(object sender, EventArgs e)
{
   ms = new MusicStaff();
   //etc...
}

Secondly, you could retrieve the control from the Controls collection using Linq. This line assumes there's only one control of type MusicStaff in the collection:

var ms = Controls.First(c => c is MusicStaff) as MusicStaff;

Upvotes: 2

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13794

just create MusicNote and ms as private static class field and make them as public static property

     static MusicStaff ms;
     statuc MusicNote musNote 

private void Form1_Load(object sender, EventArgs e)
{
    ms = new MusicStaff();
    panel2.Controls.Add(ms);

    //Test of Music Note
    musNote = new MusicNote(1, "");
    ms.Controls.Add(musNote);

public static MusicStaff Ms
{ 
   get {return ms; }
}


public static MusicNote Mn
{ 
   get {return ms; }
}

to access controls just use

  Form1.Ms or Form1.Mn

Upvotes: 1

Related Questions