将字符串从 Form2 发送到 Form3
本文关键字:Form3 Form2 字符串 | 更新日期: 2023-09-27 18:31:23
我正在使用 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
{
}
}
错误是"委托给实例方法不能有空的'this'"。
试试这个:
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 的任何属性。
好吧,我从来没有发现如何将字符串从 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();