-
[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
-
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?
-
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.
-
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:
Public Class frmLoginScreen
Private Sub frmLoginScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
'The purpose is to allow the user to login either as Guest or Admin
If txtUsername.Text = "Admin" And txtPassword.Text = "4321" Then
'Show main menu
frmMainMenu.Show()
'Hide this form
Me.Hide()
ElseIf txtUsername.Text = "Guest" And txtPassword.Text = "" Then
'Show main menu
frmMainMenu.Show()
'Hide this form
Me.Hide()
Else : MessageBox.Show("Username or password incorrect. Please try again")
End If
Dim G As New frmUserDetails
If txtUsername.Text = "Guest" Then
G.btnDelete.Enabled = False
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
'Quit the program
Application.Exit()
End Sub
End Class
and here is the main menu code:
vb Code:
Public Class frmMainMenu
Private Sub btnLinkToUserDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLinkToUserDetails.Click
'Show User Details
frmUserDetails.Show()
'Hide the form
Me.Hide()
End Sub
Private Sub btnLogoff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogoff.Click
MessageBox.Show("Are you sure you want to logoff", "Logoff now", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
'Show Login Screen
frmLoginScreen.Show()
'Hide this form
Me.Hide()
End Sub
Private Sub btnExitMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExitMainMenu.Click
'Quit the program
Application.Exit()
End Sub
Private Sub frmMainMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
If you need any more code please ask, thanks for replying.
-
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?
-
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).
-
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?
-
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.
-
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.
-
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
-
Re: Problem with disabling buttons in other forms
It still doesn't work this is the code i typed in:
vb Code:
If txtUsername.Text = "Guest" Then
frmUserDetails.btnDelete.Enabled = False
End If
i changed 'myUserDetails' to 'frmUserDetails' because an error was coming up. Im still stuck..
-
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.
-
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:
If txtUsername.Text = "Guest" Then
frmUserDetails.btnDelete.Enabled = False
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 :)
-
Re: Problem with disabling buttons in other forms
aaahh ive got it working, thanks to all who helped
-
Re: Problem with disabling buttons in other forms
mark thread resolved, rate posters that helped.
-
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
-
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.
-
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
-
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.
-
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
-
Re: Problem with disabling buttons in other forms
i just disabled it but that drop down menu still doesn't appear..
-
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?
-
Re: Problem with disabling buttons in other forms
okay just tried it again and it doesn't work.. it is currently enabled