我有2个表格。表单1是mainForm,表单2是WordSearch。我想将newSystemRec的值从WrodSearch传递给mainForm。我的两个表单都可以访问datalayer.cs文件。从我在哪里获得WordSearch形式的newSystemRec值。 这是我的WordSearch代码:
public enum Category { Schematic, Component }
public partial class WordSearch : Form
{
private DataLayer dataLayer;
private MainForm mainForm;
public event EventHandler<JumpToEventArgs> JumpTo;
public event EventHandler ModeChanged;
public event EventHandler SystemChanged;
public event EventHandler<SimEventArgs> SimChanged;
private SystemMode systemMode;
private p2p p2pMode, originalMode;
private Int32 systemRec;
private string fileName;
public Int32 projectRec { get; set; }
public WordSearch(Category category, String title)
{
InitializeComponent();
this.dgSearch.Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
this.Icon = Properties.Resources.TDXm;
if (category == Category.Schematic)
SetDefaultCategory(true, false);
else
SetDefaultCategory(false, true);
dataLayer = new DataLayer(false);
rbRefDes.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
rbComp.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
rbSchematic.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
rdoTrace.Click += new EventHandler(this.rbTraceAndSim);
rdoSim.Click += new EventHandler(this.rbTraceAndSim);
lblModel.Text = title;
}
private void WordSearch_FormClosing(object sender, FormClosingEventArgs e)
{
if (fileName == null) return;
if (originalMode != p2pMode)
{
if (ModeChanged != null)
ModeChanged(this, EventArgs.Empty);
}
//Need to pass the value below
Int32 newSystemRec = dataLayer.GetSystemType(fileName, p2pMode);
if (systemRec != newSystemRec)
{
if (SystemChanged != null)
SystemChanged(null, EventArgs.Empty);
}
}
这是我的主要形式:
public Int32 newSystemRec { get; set; }
public MainForm()
{
InitializeComponent();
InitializeP2Trace();
dataLayer.InsertHandler(this.Handle);
Args = Environment.GetCommandLineArgs();
MethodInvoker mi = new MethodInvoker(EmsJump);
this.dgComp.Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
this.dgSchematic.Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
dgSchematic.Splits[0].ColumnCaptionHeight = 28;
dgComp.Splits[0].ColumnCaptionHeight = 28;
xmlPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "temp.xml");
tsMarkup.Location = new Point(355, 0);
if (Args != null)
mi.Invoke();
}
private void wordSearch_SystemChanged(object sender, EventArgs e)
{
//How to get the value of newSystemRec???
Int32 newSystemRec =
newSystemRec--;
Int32 index = newSystemRec;
switch (index)
{
case 1:
systemMode = SystemMode.Hydraulic;
cbSystem.SelectedIndex = 1;
break;
case 0:
default:
systemMode = SystemMode.Electrical;
cbSystem.SelectedIndex = 0;
break;
}
if (p2pMode == p2p.Trace)
tabP2Trace.TabVisible = true;
}
答案 0 :(得分:0)
您的代码并未显示如何实例化这两个表单,但基本上您可以做的是将值作为表单的属性公开并从其他表单访问它。当然,为了做到这一点,必须有一个上下文,其中两个表单实例都是已知的,以便一个人可以访问另一个:
public int NewSystemRec
{
get
{
// return value here
}
}
答案 1 :(得分:0)
您可以在WordSearch上创建一个属性,如:
private int newSystemRec;
使用Property:
访问它public int NewSystemRec
{
get
{
this.newSystemRec;
}
}
例如:
MainForm mainForm = new mainForm()
someControlOnForm1.Text = mainForm.NewSystemRec;
您也可以使用构造函数:
public MainForm(int newSystemRecFromWordSearch ) {
int newSystemRec = id;
}
并在WordSearch上使用它
var form = new MainForm(newSystemRec);
希望它有所帮助!