Try a workaround that is not often recommended:
Form1 form1 = (Form1)Application.OpenForms["Form1"];
. . .
And consider other approaches too.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
Try a workaround that is not often recommended:
Form1 form1 = (Form1)Application.OpenForms["Form1"];
. . .
And consider other approaches too.
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();
. . .
}
}