问题描述
有人可以请让我知道一些code我怎么能调用位于Form类从另一个类的函数?
Can someone please let me know by some code how I can call a function located in the Form class from another class?
有些code将有很大的帮助!
Some code will be of great help!
感谢
编辑:这是我目前的code
This is my current code
public partial class frmMain : Form
{
//*******Class Instances*******
ImageProcessing IP = new ImageProcessing();
//********************
public void StatusUpdate(string text)
{
tlsStatusLabel.Text = text;
}//
public frmMain()
{
InitializeComponent();
}//
}
class ImageProcessing
{
private void UpdateStatusLabel(frmMain form, string text)
{
form.StatusUpdate(text);
}//
private UpdateLabel()
{
UpdateStatusLabel(frmMain, "Converting to GreyScale");
}
}
这个问题我有是frmMain。
the problem i am having is with frmMain.
推荐答案
一个快速和肮脏的方法是在你的Program.cs文件创建MainForm中的参考上面列出。
A quick and dirty way is to create a reference of the MainForm in your Program.cs file as listed above.
另外,您可以创建一个静态类来处理回电话给你的主要形式有:
Alternatively you can create a static class to handle calls back to your main form:
public delegate void AddStatusMessageDelegate (string strMessage);
public static class UpdateStatusBarMessage
{
public static Form mainwin;
public static event AddStatusMessageDelegate OnNewStatusMessage;
public static void ShowStatusMessage (string strMessage)
{
ThreadSafeStatusMessage (strMessage);
}
private static void ThreadSafeStatusMessage (string strMessage)
{
if (mainwin != null && mainwin.InvokeRequired) // we are in a different thread to the main window
mainwin.Invoke (new AddStatusMessageDelegate (ThreadSafeStatusMessage), new object [] { strMessage }); // call self from main thread
else
OnNewStatusMessage (strMessage);
}
}
把上面到您的MainForm.cs的命名空间,但独立于MainForm类里面的文件。
接下来把这个事件中调用到MainForm.cs主类。
Put the above into your MainForm.cs file inside the namespace but separate from your MainForm Class.
Next put this event call into your MainForm.cs main class.
void UpdateStatusBarMessage_OnNewStatusMessage (string strMessage)
{
m_txtMessage.Caption = strMessage;
}
然后,当你初始化MainForm.cs添加此事件处理到表单中。
Then when you initialise the MainForm.cs add this event handle to your form.
UpdateStatusBarMessage.OnNewStatusMessage += UpdateStatusBarMessage_OnNewStatusMessage;
在与窗体(MDI),你想调用相关的任何用户控件或形式,只是我们以下...
In any UserControl or form associated with the form (MDI) that you want to call, just us the following...
UpdateStatusBarMessage.ShowStatusMessage ("Hello World!");
由于它是静态的,可以称之为从程序中的任何地方。
Because it is static it can be called from anywhere in your program.
这篇关于从另一个类,C#.NET中调用Form类的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!