简体   繁体   English

将字符串从Form2发送到Form3

[英]Send string to Form3 from Form2

I am using Form2 to update the default printer and send the string to Form3. 我正在使用Form2更新默认打印机,并将字符串发送到Form3。 I typically have no problem operating from Form1 and passing data to Form2 or Form3. 我通常没有问题,可以从Form1操作并将数据传递到Form2或Form3。 But having trouble using Form2 to update Form3! 但是在使用Form2更新Form3时遇到了麻烦!

The real names are: Form1 = Form1, Form2 = formUserSettings, Form3 = formViewDwg 真实名称为:Form1 = Form1,Form2 = formUserSettings,Form3 = formViewDwg

Here is the code in Form1, calling Form2 (formUserSettings): 这是Form1中的代码,调用Form2(formUserSettings):

private void configureStartupSettingsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        formUserSettings frmUsr = new formUserSettings(prnNameString, prnDriverString, prnPortString,
            Settings.Default.DefaultPrinter.ToString(), Settings.Default.ViewStyle, Settings.Default.ReCenterEVafterDwgClose, 
            Settings.Default.SyncListDwgNum, listMain);
        frmUsr.ValueUpdated += new ValueUpdatedEventHandler(frmUsr_ValueUpdated); //---added 3-22-12
        //frmUsr.ValueUpdated2 += new ValueUpdatedEventHandler(newPrn_ValueUpdated); //---added 4-12-12

        frmUsr.ShowDialog();
        frmUsr.Close();
    }

Here's the code inside Form2 (formUserSettings) that tries to send the printer name to Form3 (formViewDwg). 这是Form2(formUserSettings)中的代码,试图将打印机名称发送到Form3(formViewDwg)。

if (Application.OpenForms.OfType<formViewDwg>().Count() > 0)
            {
                newEntry = comboPrinters.Items[index].ToString();
                formViewDwg frmVd = this.Owner as formViewDwg;
                delPassData del = new delPassData(frmVD.passedNewVal);
                del(newEntry);
            }
            else
            {
                frmVD = new formViewDwg(EViewMethods.currentPartPath, EViewMethods.currentPartNum, EViewMethods.currentDwgNum,
                    Settings.Default.DefaultPrinter, Settings.Default.DefaultPrinterDriver, Settings.Default.DefaultPrinterPort,
                    EViewMethods.defaultPrn[0], EViewMethods.defaultPrn[1], EViewMethods.defaultPrn[2], lBox, false, false);

                newEntry = comboPrinters.Items[index].ToString();
                delPassData del = new delPassData(frmVD.passedNewVal);
                del(newEntry);
            }

Inside Form3 (formViewDwg) is: 在Form3(formViewDwg)里面是:

public void passedNewVal(string newPrn) // using the delegate "delPassData" declared in formUserSettings
    {
        try
        {
            comboPrinter.Text = newPrn;
        }
        catch
        {

        }
    }

The error is "Delegate to an instance method cannot have null 'this'". 错误是“委托给实例方法不能具有空的'this'”。

Try this: 尝试这个:

In Form1

Form2 vForm2=new Form2();
vForm2.vForm1=this;      //initialize the vForm1 variable of Form2 to this form
vForm2.Show();

and in Form2 define a global public variable of type Form1. 在Form2中定义类型为Form1的全局公共变量。

public Form1 vForm1;

you may now be able to play around with any of the property of Form1. 您现在可以使用Form1的任何属性了。

Well I never found out how to send the string from Form2 to Form3, but I found a good solution: When Form2 closes and sends its string to Form1 from "frmUsr_ValueUpdated", it checks to see if Form3 is open. 好吧,我从来没有发现如何将字符串从Form2发送到Form3,但是我找到了一个很好的解决方案:当Form2关闭并从“ frmUsr_ValueUpdated”将其字符串发送到Form1时,它将检查Form3是否已打开。 If it is then a public method within Form3 is used to update its comboBox.text as follows. 如果是,则Form3中的公共方法用于更新其comboBox.text,如下所示。 (Form1 = Form1, Form2 = formUserSettings, Form3 = formViewDwg {instance = frmVD}) (Form1 = Form1,Form2 = formUserSettings,Form3 = formViewDwg {instance = frmVD})

private void frmUsr_ValueUpdated(object sender, ValueUpdatedEventArgs e) //---added 3-22-12
    {
        // Update the printer name on Form1 with the new value from formUserSettings
        string prnStr = e.NewValue;
        string[] parts = prnStr.Split('^'); //the printer name, driver and port were passed by e.NewValue, being separated by a "^"

        //---added 5-7-12
        EViewMethods.defaultPrn[0] = parts[0]; //printer name
        EViewMethods.defaultPrn[1] = parts[1]; //printer driver
        EViewMethods.defaultPrn[2] = parts[2]; //printer port

        toolStripStatusLabel3.Text = parts[0];

        //---added 5-7-12
        if (frmVD != null && !frmVD.IsDisposed) //want to send the new printer name now if formViewDwg is already open. If it is not open, then when it is called to open, the formViewDwg constructor will pass the new printer to it.
        {
            frmVD.ProcessPrinterName(parts[0]); //ProcessPrinterName is a public method inside formViewDwg.  Can call here because formViewDwg is already open!
        }
    }

Inside formViewDwg (Form3) is the public ProcessPrinterName method: 在formViewDwg(Form3)内部是公共的ProcessPrinterName方法:

public void ProcessPrinterName(string message)
    {
        comboPrinter.Text = message;
    }

If Form3 (formViewDwg) is not open then the updated printer name will be passed to it whenever an instance is invoked through its constructor parameter list. 如果未打开Form3(formViewDwg),则每当通过其构造函数参数列表调用实例时,更新的打印机名称都将传递给它。 Printer name will be passed as "string prnName" in the constructor: 打印机名称将在构造函数中作为“ string prnName”传递:

public formViewDwg(string currentPath, string currentPartNum, string currentDwgNum,
            string prnNameList, string prnDriverList, string prnPortList,
            string prnName, string prnDriver, string prnPort, ListBox lstBox, bool usingEngCode, bool engCodeIsEnabled) //---added 3-12-12
    {
        InitializeComponent();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM