首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在c#中遇到了上下文问题

我在c#中遇到了上下文问题
EN

Stack Overflow用户
提问于 2010-02-01 10:40:59
回答 1查看 124关注 0票数 0

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

" file“类编写文件,并在”菜单“类中调用方法(直接)。

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

谢谢你提前提供帮助。

约翰

代码语言:javascript
复制
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();
        }
EN

回答 1

Stack Overflow用户

发布于 2010-02-01 10:56:08

可以使用属性将对menu类的引用存储在send类中。

代码语言:javascript
复制
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表单的引用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2176027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档