CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2012
    Posts
    7

    Event Handling in different form

    I have a main windows form that runs everything and i have event handling that occurs when a user is logged in correctly it enables all other buttons. However, is their a way to enable other buttons from other active forms when the event handling catches true that the user logged in instead of just in the main form? i tried the same way i did the others however, the button doesnt seem to become enabled. Any help would be great.

  2. #2
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: Event Handling in different form

    I think you need to post a sample of your code. Saying that "you tried it the same way as you did in the other form", is meaningless, when we don't know what you did in the other form.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  3. #3
    Join Date
    Feb 2012
    Posts
    7

    Re: Event Handling in different form

    So in my login class i have an event handler
    public void enableButtons()
    {
    if (MakeUIEnabled != null)
    MakeUIEnabled(true);
    }
    and when the login is successful it goes over to the main function which checks to see if the event was successful or not, if it is it goes into

    public void EnableUIControls(bool shdEnable)
    {
    PatientInfoButton.Enabled = shdEnable;
    ViewGraph.Enabled = shdEnable;
    Logout.Enabled = shdEnable;
    ViewGraph.Enabled = shdEnable;
    searchPatient.Enabled = shdEnable;
    usernameTextBox.Text = person.getName();
    LoginButton.Enabled = false;
    patientBox.Visible = true;
    patientLabel.Visible = true;
    }

  4. #4
    Join Date
    Feb 2012
    Posts
    7

    Re: Event Handling in different form

    What i am trying to do is to access another windows form button when my login is successful and enable it the same way shown above. However, when i tried to enable the other forms button from main, the button never was enabled. I am wondering if i will have to enable the button within the form it belongs to or if i can enable it from a different form

  5. #5
    Join Date
    Jun 2011
    Location
    Buenos Aires, Argentina
    Posts
    130

    Re: Event Handling in different form

    I'm not sure I get how your forms work. Does the main form open the login form? Is there a login form at all? How does the login work?

    Assuming you have one main form and another "login" form, you could do this using the DialogResult, or generate an event that will be subscribed by the main form to capture a correct/incorrect login.

    Using DialogResult
    Code:
    // On the main form:
    
    DialogResult rslt = loginForm.ShowDialog();
    if (rslt == <ok>) // VS will complete this, I don't remember what the enum was called
     // login ok, validate buttons
    else
     // login error, retry or something.
    
    // On your Login form
    if (validationOk)
     this.DialogResult = <enumName>.OK;
    else
     this.DialogResult = <enumName>.(whatever you want) // Don't use Abort, it will close your form abruptly
    Using events
    Code:
    // On your main Form
    LoginFormClass loginForm = new LoginFormClass();
    
    loginForm.UserValidated += <eventHandler> // Again, VS should complete this
    // On the eventHandler you will create, do whatever you need
    
    // On your Login Form
    public delegate void LoginFormEventHandler(LoginForm sender, EventArgs e)
    public event LoginFormEventHandler UserValidated;
    
    // and when you need to trigger the event
    if (UserValidated != null) UserValidated(this, yourEventArgs);
    The plain EventArgs class will not be of much use here, so you can create your own derived class to include whatever parameters you need
    Code:
    public class UserValidationEventArgs : EventArgs
    {
     public string bla;
     public int bla;
     public bool bla;
    }
    Use 'UserValidationEventArgs' instead of EventArgs when declaring your event, and you can pass data between your forms when stuff happens.

    Not sure I'm understanding what you need though... but I hope this helps.

  6. #6
    Join Date
    Feb 2012
    Posts
    7

    Re: Event Handling in different form

    Thank You. Did help a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured