我使用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
{
}
}
错误是“委托给实例方法不能为'''。”
答案 0 :(得分:0)
试试这个:
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的任何属性。
答案 1 :(得分:0)
好吧我从来没有发现如何将字符串从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!
}
}
form formDwg(Form3)内部是公共ProcessPrinterName方法:
public void ProcessPrinterName(string message)
{
comboPrinter.Text = message;
}
如果未打开Form3(formViewDwg),则只要通过其构造函数参数列表调用实例,就会将更新的打印机名称传递给它。打印机名称将在构造函数中作为“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();