c# Windows窗体对象

本文关键字:对象 窗体 Windows | 更新日期: 2023-09-27 18:04:04

我目前在一个Windows窗体项目与几个窗体。每个表单有2个具有相同名称的对象(pictureBoxes)。一个叫"pBoxMale",另一个叫"pBoxFemale"。

我可以在不需要复制和粘贴的情况下在两种形式中使用此方法吗?

public void changeVisiblity(int column) // Change profile gender icon's visibility
{
    string tempGender = dRow.ItemArray.GetValue(column).ToString();
    if (tempGender == "M")
    {
        pBoxMale.Visible = true;
    }
    else
    {
        pBoxFemale.Visible = true;
    }
}

c# Windows窗体对象

创建父窗体:

public class ParentForm: Form
    {
        PictureBox pBoxMale { get; set; }
        PictureBox pBoxFemale { get; set; }
        public void changeVisiblity(int column, DataRow dRow) // Change profile gender icon's visibility
        {
            string tempGender = dRow.ItemArray.GetValue(column).ToString();
            if (tempGender == "M")
            {
                pBoxMale.Visible = true;
            }
            else
            {
                pBoxFemale.Visible = true;
            }
        }
    }

然后让每个窗体从父窗体继承:

 public partial class Form1 : ParentForm
    {
       ...