Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Just want to check you all that whether is it possible to call a method from a button in form 2 as we are having a "cross-thread call to windows forms controls error" after fired the button in form 2.

In my program, It has two form, form 1 is the main form, it will contains all the main methods here, example, open com port() and close com port() method. the reason why all the main methods are located here is because form 1 will be the last form to be closed.

If those methods are located in form 2, there will be an issue after closing the form 2, all the application or method will be stopped and closed,

Therefore, we need the main form "form 1" having all the methods, and call the form 2 from form 1. In form 2, perhaps we can configure com port settings and call the com port methods, like Open com port methods to start the application in form 1 then we close the form 2.

The the return massage and status of the com port will be display on the form 1.

Please advise whether possible to call that method from 2, or please suggest any alternative solution for this.

Thanks in advance.
hcchin
Posted

By the sounds of things, you are confusing yourself as well as me!

Why put all the com port stuff in a form? Create a class and put it in there - that way you don't need to start playing with other forms - which is a poor idea at best.

I would have the second form create, configure, and so forth the comport class instance, as pass it back to the first form when it is ready to go via a property in the second form, or via a property and an event, depending on how your app is supposed to work.

But getting Form2 to call Form1 methods and update it's controls is a very nasty way to do things for a whole bunch of reasons!
 
Share this answer
 
Comments
hc_chin 20-Jul-14 5:28am    
Thanks for your prompt reply, I am still very new to C#,

There is a com port class in this program. It created a instance of com port class in form 1.

Please elaborate more on the third paragraph. how should we pass the instance back to the first form if the com port instance created in form 2.
OriginalGriff 20-Jul-14 5:54am    
"It created a instance of com port class in form 1"
No it didn't - it's possible that you created an instance in form1, but that's likely to be an instance of the SerialPort class - which you can;t change as it's part of .NET. I'm talking about writing a class which contains (or encapsulates) the SerialPort.

But... your question: You know what a property is, yes?
hc_chin 20-Jul-14 10:56am    
yes, set and get property of a class which is use to set data into a instance and get data from from instance.

please have a look, another example code below here which has been simplified, I am trying to call a Add() method from "form2" with static variable "start". In from 1, there is a timer to check the whether this static variable "start" is set to true. if it set to true, it should call the Add() method. However the Visual studio prompted error "cross-thread call to windows forms controls error" while calling this method.



please advice, is that possible to have a delegate for this method? so that it can be called from form2. thanks.

Form 1 code..

public partial class Form1 : Form
{
System.Timers.Timer timer1 = new System.Timers.Timer();


public Form1()
{
InitializeComponent();
timer1.Elapsed += timer1_Elapsed;
timer1.Interval = 1000;
timer1.Start();


}

void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//throw new NotImplementedException();
if (Global.Start && !Global.Stop) // when this condition come to true, it will call Add();
{
Add();
Global.Start = false;
}

if (!Global.Start && Global.Stop)
{
//
Global.Stop = false;
}

if (Global.Start && Global.Stop)
{
Global.Start = false;
Global.Stop = false;
}

}


static public int Add()
{
return 3 + 3;
}

private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}

}
************************************************************
From 2 code ..

public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Global.Start = true; //this is possible to set the "Start" to true.
}
}

****************************************************************************

the global static class here

public class Global
{

public static bool Start = false;
public static bool Stop = false;

}
OriginalGriff 20-Jul-14 11:27am    
Why, oh why are you doing it like that?
If I'm frank, that's horrible!
What you want to do is create an event in Form2 which Form1 handles. Form2 signals the event ("StartRequest" perhaps) and Form1 handles it and does the "Add" method directly, or uses a Form1 class level variable to start it going.
Do you know how to create and signal events? And how to add event handles in code rather than via the designer?
hc_chin 21-Jul-14 11:00am    
Yes. thanks for the good advise, I never think of a form class can have event handler.
I will try it out. wish me luck..
Why not just use a class and just reference it in the form that will use all those methods?
 
Share this answer
 
Comments
hc_chin 21-Jul-14 10:15am    
Thanks for you reply. Do you mind to share one example of reference a method from a class into a form? I am really keen to learn this. thanks
Hi,

Use the best practice of coding. do not include everything on form/pages in your application. create classes and use those wherever it is required.

Create a class which have methods to Open and closing of ports and call these methods by referencing the object of this class in multiple forms.
 
Share this answer
 
Comments
hc_chin 21-Jul-14 10:17am    
Thanks for you reply. Do you mind to share one example of reference methods from a class into a multiple forms? I am really keen to learn this also. thanks
Zexceed 23-Jul-14 0:42am    
Solution is already given by @sharmanuj. :) There other ways but you can start with his solution and try to come up with your own.
Port details class
C#
public static class PortDetails
    {
        private static bool portOpen;
        private static bool portClose;
        public static void OpenPort()
        {
            if (portClose)
            {
                portOpen = true;
                // do your code to open the port details
                portClose = false;
            }
        }
        public static void ClosePort()
        {
            if (portOpen)
            {
                portClose = true;
                // do your code to close the port
                portOpen = false;
            }
        }
    }



Form1 code behind

C#
public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                PortDetails.OpenPort();
            }
            catch (Exception ex)
            { }
        }
}


Form2 code behind

C#
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PortDetails.ClosePort();
        }
    }
 
Share this answer
 
Comments
hc_chin 22-Jul-14 6:08am    
thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900