开发者

I have a context problem with c#

开发者 https://www.devze.com 2022-12-19 06:16 出处:网络
I have a problem with 2 Windows Forms which I created in c#. The first form (menu) has a PrintPreviewDialog object. The \"menu\" form then instantiates a copy of the send class \"file\" and call a Sho

I have a problem with 2 Windows Forms which I created in c#. The first form (menu) has a PrintPreviewDialog object. The "menu" form then instantiates a copy of the send class "file" and call a ShopDialog method.

The "file" class write a file and calls a method (direct) in the "menu" class.

The problem I have is that the "direct" method is not开发者_运维问答 known to the "menu" class. I think the answer is to define a copy of "menu" class in the "file", but I can't see have to do that.

Thanks for any help in advance.

John


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


You could use a property to store a reference to the menu class in the send class.

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

In order to instantiate the send class you need to pass a reference to the menu form.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号