A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/how-to-set-the-visibility-of-the-button-in-c-sharp/ below:

How to set the Visibility of the Button in C#?

How to set the Visibility of the Button in C#?

Last Updated : 17 Apr, 2023

A Button is an essential part of an application, or software, or webpage. It allows the user to interact with the application or software. In Button, you are allowed to set a value which represents the button and its child buttons are displayed by using the Visible Property. It is provided by Button class. If you want to display the given button and its child controls, then set the value of Visible property to true, otherwise set false. The default value of this property is true. You can use this property in two different methods: 1. Design-Time: It is the easiest method to set the visibility of the button. Using the following steps:

2. Run-Time: It is a little bit trickier than the above method. In this method, you can set the Visible property of the Button programmatically with the help of given syntax:

public bool Visible { get; set; }

Here, the return type of this property is System.Boolean. Following steps are used to set the Visible property of the Button:

// Creating Button using Button class
Button MyButton = new Button();
// Set the visibility of the button
MyButton.Visible = true;
// Add this Button to form
this.Controls.Add(Mybutton);
CSharp
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 WindowsFormsApp8 {

public partial class Form1 : Form {

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        // Creating and setting the properties of label
        Label l = new Label();
        l.AutoSize = true;
        l.Text = "Do you want to submit this form?";
        l.Location = new Point(222, 145);

        // Adding this label to form
        this.Controls.Add(l);

        // Creating and setting the properties of Button
        Button Mybutton = new Button();
        Mybutton.Location = new Point(225, 198);
        Mybutton.Text = "Submit";
        Mybutton.AutoSize = true;
        Mybutton.BackColor = Color.LightBlue;
        Mybutton.Visible = true;

        // Adding this button to form
        this.Controls.Add(Mybutton);

        // Creating and setting the properties of Button
        // This button in not visible in the output because the
        // visibility of this button is set to be false
        Button Mybutton1 = new Button();
        Mybutton1.Location = new Point(438, 198);
        Mybutton1.Text = "Cancel";
        Mybutton1.AutoSize = true;
        Mybutton1.BackColor = Color.LightPink;
        Mybutton1.Visible = false;

        // Adding this button to form
        this.Controls.Add(Mybutton1);
    }
}
}


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4