Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使手动创建的按钮在Windows窗体中单击后不可见?_C#_Button_Invisible - Fatal编程技术网

C# 如何使手动创建的按钮在Windows窗体中单击后不可见?

C# 如何使手动创建的按钮在Windows窗体中单击后不可见?,c#,button,invisible,C#,Button,Invisible,我使用下面的代码手动创建了一个按钮。通常,对于按钮,我可以将它们的Visible=false设置为不可见,这是我在单击按钮时调用的setInvisible方法中执行的操作。但是,我似乎无法用手动创建的按钮来实现这一点 myButtonObject start = new myButtonObject(); public MainForm() { InitializeComponent(); EventHandler myHandler = new EventHandler(

我使用下面的代码手动创建了一个按钮。通常,对于按钮,我可以将它们的
Visible=false
设置为不可见,这是我在单击按钮时调用的
setInvisible
方法中执行的操作。但是,我似乎无法用手动创建的按钮来实现这一点

myButtonObject start = new myButtonObject();

public MainForm()
{
    InitializeComponent();


    EventHandler myHandler = new EventHandler(start_Click);
    start.Click += myHandler;
    start.Location = new System.Drawing.Point(200, 500);
    start.Size = new System.Drawing.Size(101, 101);
    //start.Text="Start";
    this.Controls.Add(start);
}

void start_Click(Object sender, System.EventArgs e)
{
    start.Visible=false;
    setInvisible(); // sets a group of buttons invisible
    setVisible();   // sets another group visible 
}


public class myButtonObject : UserControl
{
    // Draw the new button. 
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Pen myPen = new Pen(Color.Black);
        // Draw the button in the form of a circle
        graphics.FillEllipse(Brushes.Goldenrod, 0, 0, 100, 100);        
        graphics.DrawEllipse(myPen, 0, 0, 100, 100);
        TextRenderer.DrawText(graphics, "Start", new Font("Arial Black", 12.25F, System.Drawing.FontStyle.Bold), new Point(23,37), SystemColors.ControlText);
        myPen.Dispose();
    }
}

请查找以下代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void myButton1_Click(object sender, EventArgs e)
    {
        this.myButton1.Hide();
    }
}
初始化组件方法:

        this.myButton1 = new WindowsFormsApplication4.MyButton();
        this.myButton1.Click += new System.EventHandler(this.myButton1_Click);

您应该将手动创建的按钮声明为构造函数外部的字段:

诸如此类

public class MainForm()
{
    // Declare the button as a field in order to have access to it
    // in any property/method/constructor within the class
    private myButtonObject start;

    ...
}

public MainForm() 
{
    InitializeComponent();

    start = new myButtonObject();
    EventHandler myHandler = new EventHandler(start_Click);
    start.Click += myHandler;
    start.Location = new System.Drawing.Point(200, 500);
    start.Size = new System.Drawing.Size(101, 101);
    this.Controls.Add(start);

    ...
} 

private void setInvisible() 
{
    ...
    // You can access the button within setInvisible() method
    start.Visible = false;
}

您必须在setInvisible()中向我们显示代码。它所做的只是将类中的其他按钮设置为不可见,如button1.Visible=false;尝试调用:button1.Hide();我似乎无法访问“开始”按钮对象?我应该在构造函数之外创建它吗?是的,我就是这么做的:)我修改了问题中的代码以反映这一点。thnx 4您的帮助:)@user3541263请不要修改您的问题。让我们看看你问了什么,以及如何解决:)好的……我会记住……提示是thnx:)
public class MainForm()
{
    // Declare the button as a field in order to have access to it
    // in any property/method/constructor within the class
    private myButtonObject start;

    ...
}

public MainForm() 
{
    InitializeComponent();

    start = new myButtonObject();
    EventHandler myHandler = new EventHandler(start_Click);
    start.Click += myHandler;
    start.Location = new System.Drawing.Point(200, 500);
    start.Size = new System.Drawing.Size(101, 101);
    this.Controls.Add(start);

    ...
} 

private void setInvisible() 
{
    ...
    // You can access the button within setInvisible() method
    start.Visible = false;
}