【问题标题】:Button Object Access按钮对象访问
【发布时间】:2016-01-09 00:04:42
【问题描述】:

我对我的 C# 技能非常生疏,但基本上我需要关闭我以编程方式创建的按钮以“返回”到主菜单。我无法访问我的按钮,我忘记了什么?这是导致问题的行:ikr1.visible = false;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Locker_Rental
{    
public partial class Form1 : Form
{
    public object ikr1 { get; private set; }

    public Form1()
    {
        InitializeComponent();
        button4.Visible = false;
        Button lkr1 = new Button();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        hide_buttons();
        build_LL1_Key_Lockers();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        hide_buttons();
        build_LL1_Combo_Lockers();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        hide_buttons();
        build_L2_Combo_Lockers();
    }

    private void hide_buttons()
    {
        button1.Visible = false;
        button2.Visible = false;
        button3.Visible = false;
    }

    private void show_buttons()
    {
        button1.Visible = true;
        button2.Visible = true;
        button3.Visible = true;
    }

    private void build_LL1_Key_Lockers()
    {
        button4.Visible = true;
        Button lkr1 = new Button();
        lkr1.Location = new Point(25, 25);
        lkr1.Text = "1";
        lkr1.Size = new Size(50, 50);
        lkr1.BackColor = System.Drawing.SystemColors.ButtonFace;
        Controls.Add(lkr1);

    }

    private void build_LL1_Combo_Lockers()
    {
        button4.Visible = true;
    }

    private void build_L2_Combo_Lockers()
    {
        button4.Visible = true;
    }

    private void button4_Click(object sender, EventArgs e)
    {
        button4.Visible = false;
        //turn off ikr1
        **ikr1.visible = false;**

        show_buttons();
    }
}

}

【问题讨论】:

  • mmm 将变量声明为按钮
  • 选择比 ikr1 和 lkr1 更好的名字!

标签: c# visual-studio object get set


【解决方案1】:

这是因为您引用的是object ikr1,而不是Button(因为它根本不存在!)。该按钮在内部方法中被实例化,因此您无法从 private void button4_Click 方法访问。您需要在此方法之外声明按钮,并在您的事件方法中创建 instance

试试这个:

namespace Locker_Rental
{    
public partial class Form1 : Form
    {
        public object ikr1 { get; private set; }
        public Button myButton;

        public Form1()
        {
            InitializeComponent();
            button4.Visible = false;
            myButton = new Button();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            hide_buttons();
            build_LL1_Key_Lockers();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            hide_buttons();
            build_LL1_Combo_Lockers();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            hide_buttons();
            build_L2_Combo_Lockers();
        }

        private void hide_buttons()
        {
            button1.Visible = false;
            button2.Visible = false;
            button3.Visible = false;
        }

        private void show_buttons()
        {
            button1.Visible = true;
            button2.Visible = true;
            button3.Visible = true;
        }

        private void build_LL1_Key_Lockers()
        {
            button4.Visible = true;
            myButton = new Button();
            myButton.Location = new Point(25, 25);
            myButton.Text = "1";
            myButton.Size = new Size(50, 50);
            myButton.BackColor = System.Drawing.SystemColors.ButtonFace;
            Controls.Add(myButton);

        }

        private void build_LL1_Combo_Lockers()
        {
            button4.Visible = true;
        }

        private void build_L2_Combo_Lockers()
        {
            button4.Visible = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button4.Visible = false;
            //turn off ikr1
            myButton.Visible = false;

            show_buttons();
        }
    }
}

【讨论】:

  • 没关系,以后可以做。我认为您可以将其标记为可接受的答案,以防它解决了问题@Trying2Code
  • 没有看到可见作为按钮的扩展:严重性代码描述项目文件行错误 CS1061“按钮”不包含“可见”的定义,并且没有扩展方法“可见”接受第一个参数可以找到“按钮”类型的(您是否缺少 using 指令或程序集引用?)
  • 必须 可见可见 @Trying2Code
  • 现在它看到了,但我有 NullReferenceException 未处理。
  • 您已将 myButton = new Button(); 添加到公共 Form1() 构造函数中? @Trying2Code
猜你喜欢
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2017-04-23
  • 1970-01-01
相关资源
最近更新 更多