簡體   English   中英

我在C#中遇到上下文問題

[英]I have a context problem with c#

我在c#中創建的2個Windows窗體有問題。 第一個窗體(菜單)具有一個PrintPreviewDialog對象。 然后,“菜單”窗體實例化發送類“文件”的副本,並調用ShopDialog方法。

“文件”類寫入文件,並在“菜單”類中調用方法(直接)。

我的問題是“菜單”類不了解“直接”方法。 我認為答案是在“文件”中定義“菜單”類的副本,但我看不出必須這樣做。

感謝您的任何幫助。

約翰


namespace CSharp
{
    public partial class MainMenu : Form
    {
        // Fields for printing.
        public PrintDocument printDocument1 = new PrintDocument();
        PageSettings printPageSettings = new PageSettings();
        static RichTextBox printRichTextBox = new RichTextBox();
        static string stringToPrint = "";
        Font printFont = new Font("Arial", 10);


        /****************************************************
         *  Select the Data->File-IO menu item.             *
         ****************************************************/

        private void fileIoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            File_IO fileIoDialog = new File_IO();

            fileIoDialog.ShowDialog();
        }

  /****************************************************
         *  Initiate the printing process. The data to be   *
         *  printed will be read from a file and stored in a*
         *  rich text box.  The print menu buttons are      *
         *  enabled.                                        *
         ****************************************************/

        public static void PrintInitialise(String printSource)
        {
            try
            {
                // Read text file and load into printRichTextBox.
                FileStream printStream = new FileStream(printSource, FileMode.Open, FileAccess.Read);
                printRichTextBox.LoadFile(printSource, RichTextBoxStreamType.PlainText);
                printStream.Close();

                // Initialise string to print.
                stringToPrint = printRichTextBox.Text;
            }
            catch (Exception ex)
            {
                // Display error message if they appear.
                MessageBox.Show(ex.Message);
            }  
        }

        /****************************************************
         *  Select the Data->File-IO menu item.             *
         ****************************************************/

        public void PrintDirect()
        {
          printDocument1.Print();
        }


        /****************************************************
         *  Select the Data->File-IO menu item.             *
         ****************************************************/

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int numChars;
            int numLines;
            string stringForPage;
            StringFormat strFormat = new StringFormat();

            // Based on page setup, define drawable rectangle on page
         }
    }
}




namespace `enter code here`CSharp
{
    public partial class File_IO : Form
    {
        MainMenu mainBransom;

      public File_IO()
        {
            InitializeComponent();
        }


private void btnPrint_Click(object sender, EventArgs e)
        {
            MainMenu.PrintInitialise(printSource);
mainBransom.PrintDirect();
        }

您可以使用屬性將對menu類的引用存儲在send類中。

public class send: Form
{
    public send(Form menuForm)
    {
        menu = menuForm;
    }

    public Form menu { get; private set; }

    //... some other methods and properties

    public void SomeButton_Click(object sender, EventArgs e) 
    {
       // you have access to the direct method (provided it is public)
       menu.direct();
    }
}

為了實例化send類,您需要傳遞對menu形式的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM