【问题标题】:Send string to Form3 from Form2将字符串从 Form2 发送到 Form3
【发布时间】:2012-04-17 21:39:51
【问题描述】:

我正在使用 Form2 更新默认打印机并将字符串发送到 Form3。我通常从 Form1 操作并将数据传递到 Form2 或 Form3 没有问题。但是使用Form2更新Form3有问题!

实名是:Form1 = Form1, Form2 = formUserSettings, Form3 = formViewDwg

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

这是 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);
            }

Form3(formViewDwg)里面是:

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

        }
    }

错误是“委托给实例方法不能有 null 'this'”。

【问题讨论】:

  • 我不明白 formViewDwg 怎么可能成为 formUserSettings 的所有者。
  • 我正在尝试让 formUserSettings 将新的打印机名称字符串发送到 formViewDwg。您在上面看到的代码大约是第四次尝试这样做。我也使用过:string newEntry = comboPrinters.Text; ValueUpdatedEventArgs valueArgs = new ValueUpdatedEventArgs(newEntry); ValueUpdated(this, valueArgs); ValueUpdatedEventArgs valueArgs2 = new ValueUpdatedEventArgs(newEntry); ValueUpdated2(this, valueArgs2);
  • 但我得到“对象引用未设置为对象的实例”。
  • 我也试过了: ValueUpdatedEventArgs valueArgs2 = new ValueUpdatedEventArgs(newEntry); ValueUpdated2(frmVD, valueArgs2);但是得到“对象引用未设置为对象的实例”。

标签: c# .net winforms visual-studio-2010


【解决方案1】:

试试这个:

In Form1

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

并在 Form2 中定义一个 Form1 类型的全局公共变量。

public Form1 vForm1;

您现在可以使用 Form1 的任何属性。

【讨论】:

  • 谢谢,我会检查一下是否有效。我现在不能,因为我要出城出差。我会尽快检查。
【解决方案2】:

好吧,我从来没有发现如何将字符串从 Form2 发送到 Form3,但我找到了一个很好的解决方案:当 Form2 关闭并将其字符串从“frmUsr_ValueUpdated”发送到 Form1 时,它会检查 Form3 是否打开。如果是,则使用 Form3 中的公共方法来更新其 comboBox.text,如下所示。 (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!
        }
    }

formViewDwg (Form3) 内部是公共的 ProcessPrinterName 方法:

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

如果 Form3 (formViewDwg) 未打开,则每当通过其构造函数参数列表调用实例时,将更新的打印机名称传递给它。打印机名称将在构造函数中作为“字符串 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多