【问题标题】:How to Inherit button with code from form1 to form2如何使用从form1到form2的代码继承按钮
【发布时间】: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


【解决方案1】:
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。

【讨论】:

  • 错误3 只有assignment、call、increment、decrement、await、new object表达式可以作为语句使用
  • 是的,我知道。因为我发现你只是拿到那个按钮,没有做任何动作。
  • 我只是按照你的代码来编辑。您应该对该按钮执行其他操作,例如 _frmMainform.newButton.Text = "My New Button";
  • 名称应该是来自 form1 或来自 form2 的第一个按钮?
  • 来自_frmMainform 表格2。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
相关资源
最近更新 更多