Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

accessing form controls from another class c#

public partial class Form1 : Form
{
//https://stackoverflow.com/questions/12983427/accessing-forms-controls-from-another-class
    // Static form. Null if no form created yet.
    private static Form1 form = null;

    private delegate void EnableDelegate(bool enable);

    public Form1()
    {
        InitializeComponent();
        form = this;
    }

    // Static method, call the non-static version if the form exist.
    public static void EnableStaticTextBox(bool enable)
    {
        if (form != null)
            form.EnableTextBox(enable);
    }

    private void EnableTextBox(bool enable)
    {
        // If this returns true, it means it was called from an external thread.
        if (InvokeRequired)
        {
            // Create a delegate of this method and let the form run it.
            this.Invoke(new EnableDelegate(EnableTextBox), new object[] { enable });
            return; // Important
        }

        // Set textBox
        textBox1.Enabled = enable;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# compress string 
Csharp :: console.writeline c# 
Csharp :: javascript close page after 5 seconds 
Csharp :: c# integer to bit string 
Csharp :: c# multiply string 
Csharp :: keybyvalue c# 
Csharp :: how to get hours and minutes from second in c# 
Csharp :: c# get getter set setter method 
Csharp :: razor confirm password validation 
Csharp :: c# current dir 
Csharp :: dialog box with form flutter 
Csharp :: c# list remove item based on property duplicate 
Csharp :: unity show colliders 
Csharp :: string reverse c# 
Csharp :: c# bitmap to byte array 
Csharp :: get enum name 
Csharp :: c# static meaning 
Csharp :: hash table in c# 
Csharp :: Install Mono project on Ubuntu 20.04 
Csharp :: c# messagebox result 
Csharp :: prevent asp button from postback 
Csharp :: flip a character in unity 
Csharp :: Net.ServicePointManager.SecurityProtocol .net framework 4 
Csharp :: unity string lowercase 
Csharp :: upgrade asp.net core to 5.0 
Csharp :: c# oops concept 
Csharp :: sequelize count all 
Csharp :: scene switch unity 
Csharp :: unity create empty gameobject in code 
Csharp :: c# for statement 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =