-3

My question is about dealing with multiple forms in C# Windows form application. I am developing a code for playing a movie and moving it frame by frame with buttons. I already have the code for moving the movie frame by frame with ctlcontrols in Windows Media Player.

The thing that I'm having an issue with is that I want to have a main form and a movie form, and when I click the button in the main form, I want to send a number to the other form and if the number was 2, I want the movie to go frame by frame in the movie form. And I want to do it without opening a new form every time I click the button. I have made a function in my second form and I called it in the button in the main form. It is expected to work but it doesn't.

The code for the button in the main form is:

private void button1_Click(object sender, EventArgs e)
{
        value = txtSendNum.Text; // get the value from the textox and
                                 // assign it to string variable
        MovieForm movieform = new MovieForm(); //create an object for MovieForm
        movieform.ConnectForms(value);            
}

The code for the function(ConnectForms function) in the second form is:

public void ConnectForms(string value)
{
    val = Convert.ToInt32(value);
    if (val == 2)
    {
        axWindowsMediaPlayer1.Ctlcontrols.play();
        axWindowsMediaPlayer1.Ctlcontrols.currentPosition += 0.5;
        axWindowsMediaPlayer1.Ctlcontrols.stop();
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Prmcdvri
  • 1
  • 2

3 Answers3

1

You are creating a new MovieForm every time the user clicks the button, this is wrong. You need a reference to the MovieForm that was previously open. This is the difference between the meaning of Object and Class. You need a reference to the object not a new object from the same class. A simple way to make it work is like the following code:

    MovieForm movieform = null; 
    private void button1_Click(object sender, EventArgs e)
    {
            value = txtSendNum.Text; 
            if(movieform == null || movieform.IsDisposed)
            {
                   movieform = new MovieForm(); //create an object for MovieForm
                   movieform.Show(); 
                   movieform.ConnectForms(value);            
            }
            else 
            {
                   movieform.ConnectForms(value); 
                   movieform.Focus();  
             }
    }
Navid Rsh
  • 308
  • 1
  • 6
  • 14
  • 1
    You need also to consider that the user could close the movieform instance and, if this happens, your code will fail with an object disposed exception. You need to subscribe to the FormClosing event and handle your local variable – Steve Mar 30 '19 at 16:53
  • 1
    IsDisposed property handles that problem as well. – Navid Rsh Mar 30 '19 at 17:08
  • @NavidRsh Thank you for your help. That really solved my problem. – Prmcdvri Mar 31 '19 at 05:52
0

You must have a reference the the other form. Instead of declaring movieform as a local variable, declare it as a class level variable (i.e., a field)

private MovieForm _movieform = new MovieForm();

private void button1_Click(object sender, EventArgs e)
{
    value = txtSendNum.Text; //get the value from the textox and assign it to string variable
    _movieform.ConnectForms(value);
    _movieform.Show();
}

A local variable, i.e., a variable declared in a method has a lifetime limited to one method call (I'm not talking about special cases like iterators and closures).

A class field has the same lifetime as the object (here the Form).

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Instead of creating a method on each form that receives a value or passing the value as parameter on the constructor of each form, or creating a new property to set the value to search for it later you should use the Tag property of the Control that is already created for that. Here you is how it is used

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.tag?view=netframework-4.7.2

private void buttonNewCustomer_Click(object sender, EventArgs e)
{
   /* Create a new customer form and assign a new 
    * Customer object to the Tag property. */
   CustomerForm customerForm = new CustomerForm();
   customerForm.Tag = new Customer();
   customerForm.Show();
}
Zinov
  • 3,817
  • 5
  • 36
  • 70