1

C#で2つのwinformを一緒に「リンク」できるかどうか疑問に思っていました。ApplicationProperties.ApplicationPort.BaudRate呼び出したいすべてのインスタンスを作成する代わりに、次のように変数を呼び出すことができますか? VB.net に似ています。誰かが私を正しい方向に向けることができれば、それは非常に高く評価されます.

(MainBox) と (ApplicationProperties) の 2 つのフォームがあります。両方のフォームに互いにアクセスできるようにしたいと思います。VB.NET と同様の方法で。

元:

フォーム 1 にはシリアル ポート (ApplicationPort) があり、値を .xml ファイルに書き込んでいます。

    public void SaveApplicationProperties()
    {
        try
        {
            //CreateNode(everything being referenced. Put text boxes, and drop down boxes here.
            XmlTextWriter writer = new XmlTextWriter(@"C:\ForteSenderv2.0\Properties.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);

            //Making the code indeted by 2 characters.
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;

            //Making the start element "Table".
            writer.WriteStartElement("Forte_Data_Gatherer_Application");
            //Calling the rst of the .xml file to write.
            CreateNode(ApplicationPort.PortName, ApplicationPort.BaudRate.ToString(), ApplicationPort.DataBits.ToString(), ApplicationPort.Parity.ToString(), ApplicationPort.StopBits.ToString(), ApplicationPort.Handshake.ToString(), writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MessageBox.Show(ApplicationPort.PortName);
            MessageBox.Show(ApplicationPort.BaudRate.ToString());
        }
        catch (Exception ex)
        {
            MessageBox.Show("Writing to .xml file failure: " + ex.Message);
        }
    }

ここで、インスタンスを作成することなく、フォーム 2 からそのシリアル ポートを呼び出せるようにしたいと考えています。インスタンスで動作していますが、form2 で使用するシリアル ポート、テキスト ボックス、またはツールごとにインスタンスを作成するのは面倒です。VB.NET のように呼び出す方法はありますか?

4

4 に答える 4

1

共通のプロパティを配置する共通の基本フォーム クラスからフォームを継承させることができます。

于 2013-07-31T17:25:18.090 に答える
1

呼び出したいすべてのインスタンスを作成する代わりに

インスタンスメソッドの場合は、フォームのインスタンスを作成する必要があります。メソッドを静的にすることができると思う場合は、好きな方法で呼び出すことができます。例えば

public partial class Form1 : Form
{
     new Form2().InstanceMethod(); //Call with instance
     Form2.StaticMethod();   // call without creating instane
}


public partial class Form2 : Form
{
    public void InstanceMethod()
    {}
    public static void StaticMethod()
    {}
}
于 2013-07-31T17:28:39.167 に答える