简体   繁体   English

如何使用从Form1到Form2的代码继承按钮

[英]How to Inherit button with code from form1 to form2

I have a main form which is called form and a second form called form2 . 我有一个称为form的主窗体和一个称为form2的第二个窗体。 Form contains a button with a coded function inside. Form包含一个内部带有编码功能的按钮。 Now on my form2 I have the same button which performs the same function on form or which I want it to perform the same function as form1. 现在,在我的form2我有执行上的相同功能的相同按钮form或者我希望它执行相同的功能为Form1。

On form2 I have created a button of which I want it to use the same function from the form1 . form2我创建了一个按钮,希望它使用与form1相同的功能。 Now I want to be able to click the button from form2 and it calls the button funtion from form1. 现在,我希望能够单击来自form2的按钮,它调用来自form1的按钮功能。

I have done this but I don't know how I can make it work 我已经做到了,但是我不知道如何使它起作用

Form1 (mainform) Form1(主要形式)

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

Form2 窗体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?)


    }

this is the button with coded function. 这是带有编码功能的按钮。 am trying to take it from this button 我试图从这个按钮

    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();
    }
private void btnCreatFile_Click(object sender, EventArgs e)
{
    // because newButton returns btnNewfile
    // You can access btnNewLife by : _frmMainform.newButton
    _frmMainform.newButton.Text = "My Button";
}

Also your newButton returns a Button object. 同样,您的newButton返回一个Button对象。 A Button object dose not have a child Button. Button对象没有子Button。 So you just need to access newButton to get btnNewLife. 因此,您只需要访问newButton即可获取btnNewLife。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM