0

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

Bob T
  • 413
  • 2
  • 8
  • 17
  • 3
    possible duplicate of [How to copy a textbox value from Form1 to Form2?](http://stackoverflow.com/questions/3384961/how-to-copy-a-textbox-value-from-form1-to-form2) – H H Apr 17 '12 at 21:50
  • I don't understand how it is possible that formViewDwg will be the owner of formUserSettings. – Steve Apr 17 '12 at 21:59
  • I am trying to get formUserSettings to send the new printer name string to formViewDwg. The code you see above is around the fourth attempt at doing that. I have also used: string newEntry = comboPrinters.Text; ValueUpdatedEventArgs valueArgs = new ValueUpdatedEventArgs(newEntry); ValueUpdated(this, valueArgs); ValueUpdatedEventArgs valueArgs2 = new ValueUpdatedEventArgs(newEntry); ValueUpdated2(this, valueArgs2); – Bob T Apr 18 '12 at 03:17
  • But I get the "Object Reference not set to an instance of an object". – Bob T Apr 18 '12 at 03:21
  • I also tried: ValueUpdatedEventArgs valueArgs2 = new ValueUpdatedEventArgs(newEntry); ValueUpdated2(frmVD, valueArgs2); But get "Object Reference not set to an instance of an object". – Bob T Apr 18 '12 at 03:25

2 Answers2

0

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.

Tanuj Wadhwa
  • 2,025
  • 9
  • 35
  • 57
  • Thank you, I will check to see if this works. I can't right now because I'm having to go out of town on business. I will check as soon as possible. – Bob T Apr 18 '12 at 20:02
0

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();
Bob T
  • 413
  • 2
  • 8
  • 17