Home > Mobile >  How would I "get" a button in a Windows Forms app in Visual Studio Code?
How would I "get" a button in a Windows Forms app in Visual Studio Code?

Time:04-09

Alright, so what I mean exactly by "get" is getting a button and manipulating the button via the C# script. I'm used to js (javascript) more than C# so I'm not entirely sure of what the C# equivalent of

document.getElementById("button_id");

is.

CodePudding user response:

Note - I am assuming this is a regular WinForms app. You refer to 'Visual Code Studio'. There is no such thing as that, there is 'Visual Studio Code' and 'Visual Studio'. I would never develop a winforms app in Visual Studio Code (but maybe it works). So I hope you are using Visual Studio

There will be a variable in the Form code that represents the button

Here is my form with one button (this is the code generated by the designer). If you built it by hand (becuase you use VS code) you will still have the same plumbing

partial class Form1 {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(446, 124);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;

You see there is a variable called button1. THats how you manipulate the button. For example you can go

        button1.Hide(); 

and the button will disappear

        button1.Text = "fertang";

changes the text etc.

CodePudding user response:

If you're in the form, and already know it's name...

Then you just use it as already shown by pm100:

button1.PerformClick(); // make button1 fire all its click handlers

If you have the name of a control in a String, and you need to SEARCH for that control, then do something more like:

String btnName = "button1";
Button btn = this.Controls.Find(btnName, true).FirstOrDefault() as Button;
if (btn != null) {
    // ... do something with "btn" ...
    btn.PerformClick();
}

The above will find the control no matter how deeply it is nested; it might be within a GroupBox, within a Panel, within the Form.

If you only want to look for a control "by name" within a specific container, then you can use syntax like this:

String btnName = "button1";
Button btn = this.Controls[btnName]; // button1 must be DIRECTLY contained by the Form
// ... < another example > ...
Button btn2 = panel1.Controls[btnName]; // button1 must be DIRECTLY containded by panel1
  • Related