按钮对象访问
Button Object Access
我对我的 C# 技能很生疏,但本质上我需要关闭我以编程方式创建的按钮 "return" 到主菜单。我无法访问我的按钮,我忘记了什么?这是导致问题的行: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();
}
}
}
这是因为您正在引用 对象 ikr1,而不是按钮 (因为它根本不存在!)。该按钮是在内部方法中实例化的,因此您无法从 private void button4_Click 方法访问。您需要在此方法之外声明 按钮,并在您的事件方法中创建实例。
试试这个:
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();
}
}
}
我对我的 C# 技能很生疏,但本质上我需要关闭我以编程方式创建的按钮 "return" 到主菜单。我无法访问我的按钮,我忘记了什么?这是导致问题的行: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();
}
}
}
这是因为您正在引用 对象 ikr1,而不是按钮 (因为它根本不存在!)。该按钮是在内部方法中实例化的,因此您无法从 private void button4_Click 方法访问。您需要在此方法之外声明 按钮,并在您的事件方法中创建实例。
试试这个:
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();
}
}
}