English 中文(简体)
我只能把静态功能从形式上说到另一种形式?
原标题:Can I only call static functions from a form to another?
  • 时间:2011-11-20 16:19:15
  •  标签:
  • c#

I have 2 forms connected to a database, LoadDocument form and a Fom1 that is the primary form. In LoadDocument I get document names out of my database, and when I close LoadDocument I send the document id to Form1 so I can retrieve its content there.

唯一的问题是,如果在<代码>Form1上行使职能,则称作public void showContent(本人的<代码>LoadDocument。 不能说它是因为它不是静态的,如果我把它固定下来的话,我会遇到创建<条码>的麻烦。

public partial class Form1 : Form
{
    public void showTasks()
    {
        radioButtons = new RadioButton[numberOfTasks];
        for (int i = 0; i < numberOfTasks; ++i)
        {
            radioButtons[i] = new RadioButton();
            radioButtons[i].Text = "Task " + (i+1);
            radioButtons[i].Location = new System.Drawing.Point(
                10, 10 + i * 20);
            groupBox1.Controls.Add(radioButtons[i]);
            radioButtons[i].Click += new EventHandler(this.radioButtons_Click);
        }
    }
}

Is there any way I can call this function from LoadDocument without making it static? Do I have to make LoadDocument dynamic, and in that case how?

EDIT:我猜测这一法典将非常相关:

    private LoadDocument m_form1;
    private bool m_underConstruction = false;
    private void ShowLoadDocument()
    {
        if (m_underConstruction)
        {
            // We re about to show it anyway
            return;
        }
        m_underConstruction = true;
        try
        {
            if (m_form1 == null)
            {
                m_form1 = new LoadDocument();
                // m_form1.FormClosed += new FormClosedEventHandler(m_form1_FormClosed);
                m_form1.Show();
            }
        }
        finally
        {
            m_underConstruction = false;
        }
        m_form1.BringToFront();
        m_form1.Activate();
    }
最佳回答

I m not sure about the control flow and the co-existence of the two forms, but you could pass the instance of Form1 to LoadDocument and call the method directly on that object. Like:

public class LoadDocument : Form {

    private Form1 form1;
    public LoadDocument(Form1 form1) {
        this.form1 = form1;
    }

    // later

    public void Method() {
        form1.showTasks();
    }
}


public class Form1 : Form {

     public void SomeMethod() {              
        LoadDocument doc = new LoadDocument(this);
        doc.Show();
     }
}
问题回答

您无需作静态改动,但需要参考<>Form1。 您可将此提及<代码>LoadDocument。 当你创建时:

public class Form1 : Form
{
    ...

        LoadDocument loadDocument = new LoadDocument(this);
        loadDocument.ShowDialog();

    ...
}

public class LoadDocument : Form
{
    private readonly Form1 _form1;

    public LoadDocument(Form1 form1)
    {
        _form1 = form1;
        InitializeComponent();
    }

    ...


         _form1.showTasks();

    ...
}

You can call the showContent method on an instance of Form1.

我的理解是,<代码>LoadDocument 表格从上创建。 表格1:。 通过<代码>Form1至/LoadDocument的构件。 这样,你就能够做以下工作:form1WhoCreatedMe.ShowContent(,有些地方载于

http://msdn.microsoft.com/en-us/library/d264741.aspx” rel=“nofollow”>dynamic 关键词给你们带来了帮助。 static





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签