1

I am a C# newbie and have encountered the following problem.

I have a class called Form1 which contains a number of controls in design view.

I have another class called Staff which inherits from Form1 and which, amongst others, contains a method called PlayAll that plays all the music notes played by the user on a music keyboard one after the other.

In the method PlayAll, I have implemented a condition which determines whether a user pressed any notes or not.

If the user did not press any notes, an error message should be displayed in ErrorTextBox (contained in Form1.cs).

This is the relevant code of PlayAll() (in Staff.cs)

public void PlayAll()
{
    ErrorTextBox.Text = "";
    if (Pressed_Notes.Count == 0) //check if the user pressed a key
    {
        ErrorTextBox.Text = "There are no music notes to play!";
    }  
    else
    {
        //Play the music notes
    }
}

My problem is that nothing appears on the ErrorTextBox (found in Form1.cs). How can I solve this problem please? Thanks.

Adam
  • 15,537
  • 2
  • 42
  • 63
Joe
  • 21
  • 7
  • you don't get any error message (e.g. an `Exception`)? – Adam Dec 31 '11 at 08:53
  • No. The ErrorTextBox just stays blank – Joe Dec 31 '11 at 08:54
  • Are you sure that this function is being called, or that the part of the `if` block you think is going to be executed is being executed? – Robert Allan Hennigan Leahy Dec 31 '11 at 08:54
  • Does the `if` condition pass? – Shiplu Mokaddim Dec 31 '11 at 08:54
  • I am not certain, however I tried to change the line before the condition, that is, ErrorTextBox.Text = "" to ErrorTextBox.Text = "abc" and still nothing comes up – Joe Dec 31 '11 at 08:55
  • The problem is not with the condition I think. It lies in the fact that I am calling ErrorTextBox from another class. – Joe Dec 31 '11 at 08:56
  • place a breakpoint on the line `ErrorTextBox.Text=""`. Then press `F5` to start debugging. If your method is getting called, the program will pause at the breakpoint and you can see what happens next by pressing `F10` multiple times. – Adam Dec 31 '11 at 08:57
  • I did as you told me. The ErrorTextBox.Text is in fact altered when debugging. The problem is that the changes are not reflected in the actual window. – Joe Dec 31 '11 at 09:01
  • When you access `ErrorTextBox` from within `Staff`, the method will get called on the `ErrorTextBox` that `Staff` **inherits** from `Form1` (but which you haven't added to the UI of `Staff`). How do you expect the runtime to know *which instance* of `Form1` to access the property on? – Adam Dec 31 '11 at 09:17
  • take a look at http://stackoverflow.com/questions/8566/best-way-to-access-a-control-on-another-form-in-winforms – Adam Dec 31 '11 at 09:21
  • To follow @codesparkle comment, how do you instanciate and display your Staff class? – FrenchData Dec 31 '11 at 09:32
  • 1
    Add `this.Show();` at the end of the method to see it. – Hans Passant Dec 31 '11 at 10:29

4 Answers4

1

Inheritance does not created a link between instances (objects) of the involved classes.

public class Form1 : Form
{
    public TextBox ErrorTextBox;
}

public class Staff : Form1
{ 
    public void PlayAll() { }
}

Let's create two instances

Form1 form1 = new Forms1();
form1.Show();

Staff staff = new Staff();
staff.Show();

This open two forms. Now two different ErrorTextBoxes exist: One on form form1 and one on form staff.

I suggest two different solutions to this problem:

  1. Instead of opening Form1 open Staff.

  2. Pass a reference of form1 or of form1.ErrorTextBox to staff.

You can do #2 through constructor injection. Change the constructor of Staff to:

private TextBox _form1ErrorTextBox;

public Staff (Form1 form1)
{
   InitializeComponent();
    _form1ErrorTextBox = form1.ErrorTextBox;
}

public void PlayAll()
{
    _form1ErrorTextBox.Text = "";
    if (Pressed_Notes.Count == 0) {
        _form1ErrorTextBox.Text = "There are no music notes to play!";
    }  else {
        //Play the music notes
    }
}

Now you can pass an instance of Form1 to Staff like this:

Form1 form1 = new Forms1();
form1.Show();

Staff staff = new Staff(form1);
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

You can't access any form control from another class. The simple way to access them is unsafe way, here it is.. let suppose u have a class form1 which have a control Textbox1 and u have another class myClass. just pass the desired control as argument with ref. e.g.

public Class myClass
{
 TextBox tb;
 public myClass(ref TextBox mtb)
    {
     tb = mtb;
    }
 //...Now you can use tb as your textbox and the value in it will be 
 //...displayed on form1 control
}

public Class form1
{
 myClass mc = new myClass(ref textBox1);
 // ...
}

But remember, its an unsafe operation to do so. This code will throw error in debugging mode. So run it without debugging..

Raza Ahmed
  • 2,661
  • 2
  • 35
  • 46
0

Here main problem is when you are inheriting form1 class in staff class. It is not possible to access form members(label) of form1 through staff.

That is why you are facing this problem.

Contact if you have any doubts.

Zlatan
  • 698
  • 7
  • 16
0

This link will show you 3 ways of getting passed that problem. I suggest you concentrate on Kevin's answer for the solution.

1- Creating a handle on form1, making the label modifier public and acessing it on form2 (not advisable, explained in the question of the link)

2- Passing the variable parameter to form2 (first option in Kevin's answer)

3- Creating an event that will update the value (second option in Kevin's answer)

Leave a comment if you need more information.

Community
  • 1
  • 1
Fernando Silva
  • 353
  • 4
  • 20