【发布时间】:2015-06-12 01:28:26
【问题描述】:
我有一个名为form 的主表单和一个名为form2 的第二个表单。 Form 包含一个内部带有编码功能的按钮。现在在我的form2 上,我有相同的按钮,它在form 上执行相同的功能,或者我希望它执行与 form1 相同的功能。
在form2 上,我创建了一个按钮,我希望它使用与form1 相同的功能。现在我希望能够从 form2 中单击按钮,并从 form1 中调用按钮功能。
我已经做到了,但我不知道如何让它工作
Form1(主窗体)
public Button newButton
{
get
{
return btnNewfile;
}
}
public void SetLanguage(string cbolang)
{
cboLanguage.SelectedValue = cbolang;
}
Form2
public frmMain_Page _frmMainform;
public FrmLanguage(frmMain_Page _frmMainform)
{
InitializeComponent();
this._frmMainform = _frmMainform;
}
public frmMain_Page _Main
{
get;
set;
}
//from this button I can't get the main button from the main form
private void btnCreatFile_Click(object sender, EventArgs e)
{
_frmMainform.newButton.btnNewfile;
//Error 19 'System.Windows.Forms.Button' does not contain a definition for 'btnNewfile' and no extension method 'btnNewfile' accepting a first argument of type 'System.Windows.Forms.Button' could be found (are you missing a using directive or an assembly reference?)
}
这是带有编码功能的按钮。我试图从这个按钮中取出它
private void btnNewfile_Click(object sender, EventArgs e)
{
_frmMainform.newButton;
XmlDocument _doc = new XmlDocument();
FileInfo _fileInfo = new FileInfo(txtInputfile.Text);
_InputFileName = _fileInfo.Name;
_InputFileSourceDirectory = _fileInfo.DirectoryName;
_InputFileExternsion = _fileInfo.Extension;
_OutFileName = cboLanguage.SelectedItem.ToString() + "-language.resx";
string outputFilePath = txtInputfile.Text.Replace(_InputFileName, _OutFileName);
File.Copy(txtInputfile.Text, outputFilePath);
string text = File.ReadAllText(outputFilePath);
XDocument doc = XDocument.Load(outputFilePath);
foreach (var valueNode in doc.Descendants("data").SelectMany(n => n.Elements("value")))
{
valueNode.Value = string.Empty;
}
foreach (var commentNode in doc.Descendants("data").SelectMany(n => n.Elements("comment")))
{
commentNode.Value = DeleteBetween(commentNode.Value, "Font");
commentNode.Value = DeleteBetween(commentNode.Value, "DateStamp");
commentNode.Value = DeleteBetween(commentNode.Value, "Comment");
}
doc.Save(outputFilePath);
txtOutputfile.Text = _InputFileSourceDirectory + "\\" + _OutFileName;
_doc.Load(outputFilePath);
string xmlcontents = _doc.InnerXml;
//lblversion.Text = updateversion.ToString();
}
【问题讨论】:
-
一方面,这与继承无关。第二,您试图访问
_frmMainform.newButton中的btnNewfile,这显然很愚蠢——您已经在_frmMainform.newButton上有了按钮引用。错误信息在这一点上非常清楚。三,不要。一般来说,这只是一个不好的做法,用 C# 编写 Delphi 并不是使用 C# 的好方法 :) 检查你的依赖关系,看看你是否可以将代码分离到一个可以调用的单独函数中从两个按钮点击事件... -
我试图从这里得到一个方向。我迷路了,我要求任何人告诉我我该怎么做
标签: c# winforms inheritance button