Results 1 to 23 of 23

Thread: [RESOLVED] Problem with disabling buttons in other forms

  1. #1

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Resolved [RESOLVED] Problem with disabling buttons in other forms

    Hi, i have a problem, My situation is this, I have a login screen (frmLoginScreen) which links to a main menu(frmMainMenu) then to another form (frmUserDetails). Their are two possible usernames. The first one the user logs in as Admin and the password is 4321, then the second one is Guest and no password. This is the code i have so far:

    Dim G As New frmUserDetails
    If txtUsername.Text = "Guest" Then
    G.btnDelete.Enabled = False
    End If

    There are no errors and i don't know whats wrong can someone give me a code sample that works?

    Thanks
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Problem with disabling buttons in other forms

    When you do:
    Dim G As New frmUserDetails
    you create a new instance of form referred as "G".

    Are you sure you are using the same instance of the form everywhere in your application after that?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Problem with disabling buttons in other forms

    You define G as a new instance of frmUserDetails, then do nothing with it.

    If a textbox on G has "Guest" as the text, you disable a delete button?

    Need more information or post more of your code here.

  4. #4

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    To Pradeep:
    I don't use "G" anywhere else in the application because i don't need to. Why would i need to use it anywhere else?

    To bulldog:
    Here is the code for my login screen:
    vb Code:
    1. Public Class frmLoginScreen
    2.  
    3.     Private Sub frmLoginScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.     End Sub
    6.  
    7.     Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    8.         'The purpose is to allow the user to login either as Guest or Admin
    9.         If txtUsername.Text = "Admin" And txtPassword.Text = "4321" Then
    10.             'Show main menu
    11.             frmMainMenu.Show()
    12.             'Hide this form
    13.             Me.Hide()
    14.         ElseIf txtUsername.Text = "Guest" And txtPassword.Text = "" Then
    15.             'Show main menu
    16.             frmMainMenu.Show()
    17.             'Hide this form
    18.             Me.Hide()
    19.         Else : MessageBox.Show("Username or password incorrect. Please try again")
    20.         End If
    21.  
    22.         Dim G As New frmUserDetails
    23.         If txtUsername.Text = "Guest" Then
    24.             G.btnDelete.Enabled = False
    25.         End If
    26.     End Sub
    27.  
    28.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    29.         'Quit the program
    30.         Application.Exit()
    31.     End Sub
    32. End Class

    and here is the main menu code:
    vb Code:
    1. Public Class frmMainMenu
    2.  
    3.     Private Sub btnLinkToUserDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLinkToUserDetails.Click
    4.         'Show User Details
    5.         frmUserDetails.Show()
    6.         'Hide the form
    7.         Me.Hide()
    8.     End Sub
    9.  
    10.     Private Sub btnLogoff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogoff.Click
    11.         MessageBox.Show("Are you sure you want to logoff", "Logoff now", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    12.         'Show Login Screen
    13.         frmLoginScreen.Show()
    14.         'Hide this form
    15.         Me.Hide()
    16.     End Sub
    17.  
    18.     Private Sub btnExitMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExitMainMenu.Click
    19.         'Quit the program
    20.         Application.Exit()
    21.     End Sub
    22.  
    23.     Private Sub frmMainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    24.  
    25.     End Sub
    26. End Class

    If you need any more code please ask, thanks for replying.
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  5. #5
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: Problem with disabling buttons in other forms

    You define G as a new instance of frmUserDetails, but you never show that instance of the form, e.g. G.Show() does not appear in your code. What is the purpose of the form you have named G?

  6. #6
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Problem with disabling buttons in other forms

    As Pradeep and Bulldog have said, you create a new instance of the details form (G), you disable the button on this form, then do nothing with it - you don't show it or use it in any useful way.

    Perhaps you mean to access the frmUserDetails directly and disable the delete button on that? (this is the form you've referenced in the btnLinkToUserDetails_Click event).

  7. #7

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    To link it to the form user details.. what do you suggest i should do, to achieve my goal?
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Problem with disabling buttons in other forms

    Pradeep had the key answer, and this is one of my pet peeves (default instances). Beginning with .NET 2005 there was a default instance of every form. This made .NET more similar to VB6, but causes problems like this one. You create a form G of type frmUserDetails, change a property of an object on the form, then G gets destroyed when it goes out of scope at the end of the sub. You later show the default instance of frmUserDetails, which is called frmUserDetails, and are expecting what you did to G to show up in frmUserDetails, but they are two different objects, and changes to G have no effect at all on frmUserDetails. Unfortunately, because there is a default instance of the type frmUserDetails, and because the default instance happens to have the same name as the type, it is possible to not realize that the default instance and the type are two different concepts. One is a class type, while the other is a single instance of that class type.

    By the way, I'm amused to point out that there is a typo in your first post that cannot be correctly described in a post. One of the quirks of the English language (and perhaps any language) is that there are certain sentences that can be spoken, but cannot be correctly written down, and that particular typo happens to be one of them. An amusing oddity of the language.
    My usual boring signature: Nothing

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Problem with disabling buttons in other forms

    Lord was I late.

    I would suggest that you either use frmUserDetails (the default instance that you show later on) instead of G, or, better still, don't use the default instance here or anywhere else, but declare a new variable of type frmUserDetails outside of any sub (at form scope):

    Private myUserDetails as New frmUserDetails

    then change the stuff with G to this:
    Code:
    If txtUsername.Text = "Guest" Then   
      myUserDetails.btnDelete.Enabled = False 
    End If
    And change the place you show frmUserDetails to myUserDetails.Show. The reason for not using a default instance is that it can trip you up, as this one did. Otherwise, there's nothing particularly bad about them.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    Quote Originally Posted by SJWhiteley
    Perhaps you mean to access the frmUserDetails directly and disable the delete button on that? (this is the form you've referenced in the btnLinkToUserDetails_Click event).
    Yes that's what i mean, shaggy hiker seems to be write, i couldn't describe my situation properly, and i finally realise that frmUserDetails is a completely different concept to concept G, you see i was under the impression that they were the same thing. Having said this i still can't see a way to achieve my goal, can someone please give me a code same that i can input in the Login button (in frmLoginScreen) which disables the Delete button (btnDelete) in the User Details form (frmUserDetails)

    Thanks

    EDIT>>>>> i Just saw shaggy's new post, will attempt it now
    Last edited by Pantero; Feb 10th, 2009 at 05:07 PM.
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  11. #11

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    It still doesn't work this is the code i typed in:
    vb Code:
    1. If txtUsername.Text = "Guest" Then
    2.             frmUserDetails.btnDelete.Enabled = False
    3.         End If

    i changed 'myUserDetails' to 'frmUserDetails' because an error was coming up. Im still stuck..
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Problem with disabling buttons in other forms

    Oh, I had it backwards in my rush to get something out.

    I would say that the best thing to do would be to either return a value from the login form, or to add a property to the login form. In either case, the form that shows the login form will be responsible for enabling/disabling the button on the frmUserDetails instance. If the form that shows the login form won't be subsequently showing the frmUserDetails form, then it needs to get the button state from the login form and make it accessible to whatever code actually does show the frmUserDetails form.

    Of the two options, the first one would be best if you were closing the login form. However, you are using Me.Hide, so I assume that you actually are not closing the form for a reason, so returning a dialog result won't work (because nothing is being returned). Therefore, in the login form, you would need to add a property:
    Code:
    Private mDeleteState as boolean
    
    Public ReadOnly Property DeleteState as Boolean
     Get
       Return mDeleteState
     End Get
    End Property
    Then in the login Click event, you would add:

    mDeleteState = True

    (or mDeleteState = False). You would want to set the state of the boolean for each case, so there might be one such line in each If case.

    Next, in the form that shows the frmUserDetails form, you would want to set the state of the button right before you showed the form. Since you are using the default instance of both the frmUserDetails and the frmLoginScreen, then you can do this fairly easily by adding this line right before the frmUserDetails.Show:

    frmUserDetails.btnDetail.Enabled = frmLoginScreen.DeleteState

    There are a few caveats to this that ought to be noted. By following those steps, the buttons enabled state will be set to whatever the frmLoginScreen.DeleteState is. This means that if the user has not been to frmLoginScreen, or has not clicked the button on the form, then the delete state will be False, so the button will not be enabled. That may not be desirable, as this will make the button disabled by default. An alternative would be to set the mDeleteState variable to True in the constructor, which would make the button to default to visible. More complex options are also possible.
    My usual boring signature: Nothing

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Problem with disabling buttons in other forms

    Quote Originally Posted by Pantero
    It still doesn't work this is the code i typed in:
    vb Code:
    1. If txtUsername.Text = "Guest" Then
    2.             frmUserDetails.btnDelete.Enabled = False
    3.         End If

    i changed 'myUserDetails' to 'frmUserDetails' because an error was coming up. Im still stuck..
    Unless you are doing something extra-ordinary, this should have worked.

    Maybe your username is spelled incorrectly and it never enters the IF block. Or maybe something else.
    Put a breakpoint on this line and step thru the code line by line. Maybe that leads to some clues.

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    aaahh ive got it working, thanks to all who helped
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Problem with disabling buttons in other forms

    mark thread resolved, rate posters that helped.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #16

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    i cant mark it resolved yet because i am not a high enough member or so ive been told. I have already rated the useful posters
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Problem with disabling buttons in other forms

    You should be able to mark it resolved at any level, unless the rules have been changed again (it happens, and I don't keep up with it). There should be a Thread Tools menu option on the upper right of the page which has a Resolve Thread option on it. I have no idea why that wouldn't be available to everybody.
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    yes that menu doesn't come its more of a link label which links to other options and 'mark thread resolved' isn't one of them options
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Problem with disabling buttons in other forms

    Bizarre. Considering that I didn't know about that tool for a year or two, and marked threads [Resolved] by editing the first post to add that word to the subject line, and considering that ANYBODY can do that, I see no reason not to allow everybody access to that tool.

    Still, that's life, I guess.
    My usual boring signature: Nothing

  20. #20
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Problem with disabling buttons in other forms

    @pantero - the thinking is that you don't have javascript enabled. read this

    http://www.vbforums.com/showthread.php?t=557456
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  21. #21

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    i just disabled it but that drop down menu still doesn't appear..
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

  22. #22
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Problem with disabling buttons in other forms

    Hmmm,

    The suggestion was that you would enable Javascript if it wasn't already.
    But instead you disabled it, which would infer you had it enabled already?

    Perhaps you could try enabling it again, and report back if you then have access to the menu?


    BTW, the Mark thread as resolved links property is displayed to me as :

    http://www.vbforums.com/showthread.p...&is_resolved=1

    Perhaps you could clikc that and see if it works?

  23. #23

    Thread Starter
    Addicted Member Pantero's Avatar
    Join Date
    Jan 2009
    Posts
    169

    Re: Problem with disabling buttons in other forms

    okay just tried it again and it doesn't work.. it is currently enabled
    Im using visual basic 2008 express edition so any posts that include coding or stuff PLEASE refer to vb 2008.

    "A clever person solves problems, a wise person avoids them"

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