我有c#的上下文问题

时间:2010-02-01 10:40:59

标签: c#

我在c#中创建的2个Windows窗体有问题。第一个表单(菜单)有一个PrintPreviewDialog对象。然后“菜单”表单实例化发送类“文件”的副本并调用ShopDialog方法。

“file”类编写一个文件并在“menu”类中调用一个方法(直接)。

我遇到的问题是“菜单”类不知道“直接”方法。我认为答案是在“文件”中定义“菜单”类的副本,但我看不出必须这样做。

感谢您提前提供任何帮助。

约翰


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();
        }

1 个答案:

答案 0 :(得分:0)

您可以使用属性存储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表单的引用。