如何使用从form1到form2的代码继承按钮

时间:2015-04-07 12:37:09

标签: c# winforms inheritance button

我有一个名为form的主表单和一个名为form2的表单。 Form包含一个内部带编码功能的按钮。现在我的form2我有相同的按钮,它在form上执行相同的功能,或者我希望它执行与form1相同的功能。

form2我创建了一个按钮,我希望它使用form1中的相同功能。现在我希望能够从form2单击按钮,它会调用form1中的按钮功能。

我已经这样做了,但我不知道如何让它发挥作用

Form1(mainform)

    public Button newButton
    {
        get
        {
            return btnNewfile;
        }
    }
    public void SetLanguage(string cbolang)
    {
        cboLanguage.SelectedValue = cbolang;
    }

窗体2

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

1 个答案:

答案 0 :(得分:0)

private void btnCreatFile_Click(object sender, EventArgs e)
{
    // because newButton returns btnNewfile
    // You can access btnNewLife by : _frmMainform.newButton
    _frmMainform.newButton.Text = "My Button";
}

此外,您的newButton会返回一个Button对象。 Button对象没有子Button。 所以你只需要访问newButton来获取btnNewLife。