Trying to execute a method in another Winform class c#

Carlo Goretti 121 Reputation points
2021-05-03T19:24:02.113+00:00

Hey,

Im trying to reach a method in my other Form class but it seeems like its not want to be executed...
When im starting in debug mode it reaches the method and everything but nothing happens...
Any ideas what im doing wrong here?
Here is my code:
Here is the call.

            Form1 form1 = new Form1();
            form1.TaskSchedulerLogg();

And here is the method:

        public void TaskSchedulerLogg()
        {
            config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
            var taskName = config.AppSettings.Settings["SchemaLäggareNamnFavorit"].Value;
            var task = ts.GetTask(taskName);
            if (!(task == null))
            {
                BeteckningHämtadLbl.Text = task.Name;
                counter = (int)(Convert.ToDateTime(task.NextRunTime.ToString()) - DateTime.Now).TotalSeconds;
                timeSpan = TimeSpan.FromSeconds((int)(Convert.ToDateTime(task.NextRunTime.ToString()) - DateTime.Now).TotalSeconds);

                Tid_FöregåendeLbl.Text = task.LastRunTime.ToString();

                if (NedräkningsTimer.Enabled == false)
                {
                    NedräkningsTimer.Start();

                }
                Tid_Nästa.Text = timeSpan.ToString(@"hh\:mm\:ss");


                if (task.IsActive == true)
                {
                    Status.Text = "Aktiverad";
                    pictureBox1.Image = Resources.GreenPicture;
                }
                else
                {
                    Status.Text = "Inaktiverad";
                    pictureBox1.Image = Resources.Gul;
                }


                SchemaläggareInfoBtn.Visible = false;
                KonfigurationBtn.Location = SchemaläggareInfoBtn.Location;
            }
            else
            {
                BeteckningHämtadLbl.Text = "";
                Tid_FöregåendeLbl.Text = "ERROR";
                Tid_Nästa.Text = "ERROR";
                Status.Text = "ERROR";
                SchemaläggareInfoBtn.Visible = true;
                KonfigurationBtn.Location = OldPosKonfigurationBtn;
                NedräkningsTimer.Stop();
                pictureBox1.Image = Resources.RedPicture;
            }
        }

Best Regards

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,814 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,164 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 111.5K Reputation points
    2021-05-03T19:55:33.627+00:00

    Try a workaround that is not often recommended:

    Form1 form1 = (Form1)Application.OpenForms["Form1"];
    . . .
    

    And consider other approaches too.


  2. Viorel 111.5K Reputation points
    2021-05-04T08:29:17.37+00:00

    Try a method that is usually acceptable:

    // Form1 that displays Form2:
    
    class Form1
    {
       . . .
       void SomeFunction( )
       {
          Form2 f2 = new Form2();
          f2.form1 = this;
          f2.ShowDialog();
       }
    }
    
    // Form2 that is displayed by Form1:
    
    class Form2
    {
       public Form1 form1;
       . . .
       void SomeFunction()
       {
          form1.TaskSchedulerLogg();
          . . .
       }
    }
    
    0 comments No comments