Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Disabling a button on one form from another.

Disabling a button on one form from another.

Scheduled Pinned Locked Moved C#
question
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    False Chicken
    wrote on last edited by
    #1

    I have this button that I want disabled from a check box on a separate form. Its an options window and one option is to disable a certain button but the button is on the previous from. How can i disable the button from the form? I cant just start typing because it does not know the button exists. due to it being on another form.

    Y*Live Long And Prosper*Y

    D L 3 Replies Last reply
    0
    • F False Chicken

      I have this button that I want disabled from a check box on a separate form. Its an options window and one option is to disable a certain button but the button is on the previous from. How can i disable the button from the form? I cant just start typing because it does not know the button exists. due to it being on another form.

      Y*Live Long And Prosper*Y

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      Raise an event in the form with the checkbox that the form with the button subscribes to.

      Dave
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      1 Reply Last reply
      0
      • F False Chicken

        I have this button that I want disabled from a check box on a separate form. Its an options window and one option is to disable a certain button but the button is on the previous from. How can i disable the button from the form? I cant just start typing because it does not know the button exists. due to it being on another form.

        Y*Live Long And Prosper*Y

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        You might want to delete the repost you made above this one before you get flamed for it! I think the CP servers had a bit of a fit a few minutes ago, so probably when you thought it hadn't submitted - it actually had.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        F 1 Reply Last reply
        0
        • D DaveyM69

          You might want to delete the repost you made above this one before you get flamed for it! I think the CP servers had a bit of a fit a few minutes ago, so probably when you thought it hadn't submitted - it actually had.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          F Offline
          F Offline
          False Chicken
          wrote on last edited by
          #4

          yeah sorry lol I thought it didn't go through

          Y*Live Long And Prosper*Y

          1 Reply Last reply
          0
          • F False Chicken

            I have this button that I want disabled from a check box on a separate form. Its an options window and one option is to disable a certain button but the button is on the previous from. How can i disable the button from the form? I cant just start typing because it does not know the button exists. due to it being on another form.

            Y*Live Long And Prosper*Y

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You could do it using events or delegates or custom WM messages :)

            F L 2 Replies Last reply
            0
            • L Lost User

              You could do it using events or delegates or custom WM messages :)

              F Offline
              F Offline
              False Chicken
              wrote on last edited by
              #6

              Can someone please be more specific? Im new to programming.

              Y*Live Long And Prosper*Y

              D 1 Reply Last reply
              0
              • F False Chicken

                Can someone please be more specific? Im new to programming.

                Y*Live Long And Prosper*Y

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #7

                Working Example:

                // FormButton.cs
                using System;
                using System.Windows.Forms;

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

                private void FormButton\_Load(object sender, EventArgs e)
                {
                    FormCheckBox formCheckBox = new FormCheckBox();
                    formCheckBox.CheckStateChanged += formCheckBox\_CheckStateChanged;
                    formCheckBox.Show();
                }
                
                void formCheckBox\_CheckStateChanged(object sender, CheckStateEventArgs e)
                {
                    button1.Enabled = e.IsChecked;
                }
                

                }

                // FormCheckBox.cs
                using System;
                using System.Windows.Forms;

                public partial class FormCheckBox : Form
                {
                public event EventHandler<CheckStateEventArgs> CheckStateChanged;
                public FormCheckBox()
                {
                InitializeComponent();
                }

                private void checkBox1\_CheckStateChanged(object sender, EventArgs e)
                {
                    OnCheckStateChanged(new CheckStateEventArgs(checkBox1.Checked));
                }
                
                protected virtual void OnCheckStateChanged(CheckStateEventArgs e)
                {
                    EventHandler<CheckStateEventArgs> eh = CheckStateChanged;
                    if (eh != null)
                        eh(this, e);
                }
                

                }

                // CheckStateEventArgs.cs
                public class CheckStateEventArgs : EventArgs
                {
                public CheckStateEventArgs(bool isChecked)
                {
                IsChecked = isChecked;
                }
                public bool IsChecked
                {
                get;
                private set;
                }
                }

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                1 Reply Last reply
                0
                • L Lost User

                  You could do it using events or delegates or custom WM messages :)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I've already read that DaveyM69 has explained to you how to use events/delegates for solving your problem.But sometimes you need to notify other instances of your app for something important.In this case events and delegates are not useful,because you can't get valid pointers of the forms belonging to another instance of the app.In this case you have to use custom WM messages.To register custom WM message call RegisterWindowMessage native function. And to send message call SendMessage native function passing HWND_BROADCAST value for hWnd parameter.But this function doesn't seem to work with child windows,but it works with hidden top windows for example. But be careful with that option-it could damage OS performance.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups