简体   繁体   中英

Send string to Form3 from Form2

I am using Form2 to update the default printer and send the string to Form3. I typically have no problem operating from Form1 and passing data to Form2 or Form3. But having trouble using Form2 to update Form3!

The real names are: Form1 = Form1, Form2 = formUserSettings, Form3 = formViewDwg

Here is the code in Form1, calling 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).

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:

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'".

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.

public Form1 vForm1;

you may now be able to play around with any of the property of 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. If it is then a public method within Form3 is used to update its comboBox.text as follows. (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:

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. Printer name will be passed as "string prnName" in the constructor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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