I am trying to keep a button disabled until all required fields are completed. I have the button disabled at initialize and I can get it to enable with one field completed using the code below. How do I add the other fields that are required to be completed?
`Private Sub Findings_Change()
If Findings.Value <> "" Then
btnOk.Enabled = True
Else: btnOk.Enabled = False
End If
End Sub`
I tried the following, but it enables the button after one field is completed and not all three. Please HELP!
`Private Sub Findings_Change()
If Findings.Value <> "" Then
btnOk.Enabled = True
Else: btnOk.Enabled = False
End If
End Sub
Private Sub Failure_Change()
If Failure.Value <> "" Then
btnOk.Enabled = True
Else: btnOk.Enabled = False
End If
End Sub
Private Sub CompletionTime_Change()
If CompletionTime.Value <> "" Then
btnOk.Enabled = True
Else: btnOk.Enabled = False
End If
End Sub`
Related
I have radio button list that calls a function.
If the function returns true then I want to change the value.
However if the function returns false then I do not want to change the value and keep the original selection value.
Currently it changes the value even when the statement returns false.
Any advice?
ASP Page
<asp:RadioButtonList ID="rblType" runat="server" AutoPostBack="True"
DataSourceID="SqlData" DataTextField="Type"
DataValueField="TypeID">
</asp:RadioButtonList>
VB file
Private selectionvalue As Integer
Protected Sub rblType_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles rblType.SelectedIndexChanged
Dim CheckListValidation As Boolean = CheckListBox()
If CheckListValidation = True Then
selectionvalue = rblType.SelectedItem.Value
Else
rblType.SelectedItem.Value = selectionvalue
End If
End Sub
Function CheckListBox() As Boolean
If lstbox.Items.Count <> "0" Then
If MsgBox("Are you sure you want to change option?", MsgBoxStyle.YesNo, " Change Type") = MsgBoxResult.Yes Then
Return True
Else
Return False
End If
Else
Return True
End If
End Function
The problem is when rblType_SelectedIndexChanged is executed, the selected item is already changed and the RadioButtonList doesn't "remember" the previously selected value. You need to keep the previously selected value between postbacks in order to achieve this.
I would suggest using ViewState. Create a property in code behind to represent the ViewState value:
Private Property PreviousSelectedValue() As String
Get
If (ViewState("PreviousSelectedValue") Is Nothing) Then
Return String.Empty
Else
Return ViewState("PreviousSelectedValue").ToString()
End If
End Get
Set(ByVal value As String)
ViewState("PreviousSelectedValue") = value
End Set
End Property
and in rblType_SelectedIndexChanged:
Protected Sub rblType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblType.SelectedIndexChanged
Dim CheckListValidation As Boolean = CheckListBox()
If CheckListValidation = True Then
'save the currently selected value to ViewState
Me.PreviousSelectedValue = rblType.SelectedValue
Else
'get the previously selected value from ViewState
'and change the selected radio button back to the previously selected value
If (Me.PreviousSelectedValue = String.Empty) Then
rblType.ClearSelection()
Else
rblType.SelectedValue = Me.PreviousSelectedValue
End If
End If
End Sub
I am using the following to control user log in authentication. This redirects fine when the user is inactive or locked out, but when the user is deleted from the database, I get an error message...
"Object reference not set to an instance of an object" on this line...
If currentuser IsNot Nothing And currentuser.IsApproved = False Or currentuser.IsLockedOut = True Then
What could be causing this to happen?
Protected Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
If User.Identity.IsAuthenticated Then
Dim currentuser As MembershipUser = Membership.GetUser()
If currentuser IsNot Nothing And currentuser.IsApproved = False Or currentuser.IsLockedOut = True Then
FormsAuthentication.SignOut()
FormsAuthentication.RedirectToLoginPage()
End If
If currentuser IsNot Nothing Then
Response.Redirect("~/media")
End If
End If
End Sub
Change the If condition to:
If currentuser IsNot Nothing Then
If currentuser.IsApproved = False Or currentuser.IsLockedOut = True Then
FormsAuthentication.SignOut()
FormsAuthentication.RedirectToLoginPage()
End If
Response.Redirect("~/media")
End If
You need to parathesize the second part and use OrElse/AndAlso, otherwise currentuser IsNot Nothing will not be applied to all parts of your if
So this will fix it:
If currentuser IsNot Nothing AndAlso (Not currentuser.IsApproved OrElse currentuser.IsLockedOut) Then
Don't use Or (and AND) but OrElse (and AndAlso) which will short circuit. That means it will not evaluate the second part if the first part was already true. Or (and And) on the other side will evaluate both parts always.
Since 'And' receives precedence over 'Or' the expression will
be evaluated as follows:
If
{
currentuser IsNot Nothing And currentuser.IsApproved = False //condition 1
Or
currentuser.IsLockedOut = True //condition 2
}
Then..
If current user is in fact nothing:
condition 1- will not fail, but condition 2 will throw
an exception since the code is trying to evaluate (nothing).somthing.
To fix the problem you'll have to add parentheses as shown:
If
currentuser IsNot Nothing //condition 1
And
(currentuser.IsApproved = False Or currentuser.IsLockedOut = True) //condition 2
Then..
Now, the 2nd condition will be evaluated only if the first condition is true.
I have a treeview asp.net control. And I have the following SelectedNodeChanged Event ofr Treeview1.
Private Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
Try
Dim type As Boolean = False
Dim nodetext As String = TreeView1.SelectedNode.Value
Dim depth As Integer = TreeView1.SelectedNode.Depth
If nodetext = "Root" Then
btnCreateComp.Visible = True
btnCreateComp.Text = "Create Company"
btnCreateUser.Visible = False
Else
type = getNodeType(nodetext)
'if it is true its USER
'if false it is COMPANY
If type = True Then
btnCreateComp.Visible = False
btnCreateUser.Visible = True
btnCreateUser.Text = "Edit User"
btnCreateUser.PostBackUrl = "~/Account/User.aspx"
Else
btnCreateComp.Visible = True
btnCreateComp.Text = "Edit Company"
btnCreateUser.Visible = True
btnCreateUser.Text = "Create User"
btnCreateUser.PostBackUrl = "~/Account/User.aspx"
End If
End If
Catch ex As Exception
End Try
End Sub
When I click on a node, and if it is not "root", it makes 2 buttons visible, if I click the button btnCreateUser , it goes to a page "user.aspx", which is postbackurl for btnCreateUser.
This is fine.
But when the next time a click a node other than "Root", it directly goes to the postbackurl page "User.aspx", even though I have not clicked on the Button btnCreateUser.
And this happens only once after the first time I have clicked "btnCreateUser".
Can anyone please help?
I got 3 different form buttons on my spreadsheet. I want to disable 2 of them while one is pressed. Is that possible?
In C# the button can be true or false, but I can't find any examples of this in VBA Excel 2010?
Thanks in advance
You cannot disable Form Buttons. If you want to use that functionality then use the ActiveX button.
However there is an alternative. Create 2 Public Boolean Variables and then in the click event of Button 1 Set the variables to True or False. Depending on the Boolean variables, the other 2 buttons will run their code or not. For example
Option Explicit
Dim enableB2 As Boolean, enableB3 As Boolean
Sub Button1_Click()
If enableB2 = False Then
enableB2 = True: enableB3 = True
Else
enableB2 = False: enableB3 = False
End If
'
'~~> Rest of the code
'
End Sub
Sub Button2_Click()
If enableB2 = True Then
'
MsgBox "Hello You clicked Button 2"
'
End If
End Sub
Sub Button3_Click()
If enableB3 = True Then
'
MsgBox "Hello You clicked Button 3"
'
End If
End Sub
I just wanted to offer an alternative approach for anyone hitting this from Google (that prefers not to use ActiveX buttons): rather than using global Boolean variables, you can grey the text in the button when it is disabled, and check the text colour in the macro assigned to the button before running the macro.
For example, if you have the following sub:
Public Sub SetFormButtonEnabled(ByVal oWks As Object, ByVal sName As String, ByVal bValue As Boolean) As Boolean
If blnValue Then
' Enabled: black text
oWks.Shapes(sName).TextFrame.Characters.Font.ColorIndex = 1
Else
' Disabled: grey text
oWks.Shapes(sName).TextFrame.Characters.Font.ColorIndex = 16
End If
End Sub
and function:
Public Function GetFormButtonEnabled(ByVal oWks As Object, ByVal sName As String) As Boolean
' Enabled if text colour is black, otherwise it is disabled
GetFormButtonEnabled = (oWks.Shapes(sName).TextFrame.Characters.Font.ColorIndex = 1)
End Function
then you can use the SetFormButtonEnabled function in the same way as you'd use the ActiveX button's button.Enabled property, and use GetFormButtonEnabled to check the button is enabled before executing the click macro.
To use these to answer the initial question, with a buttons named btnTest1-3 (I name the buttons with this sort of convention after creating, but Button 1-3 would also work fine) and click macros btnTest1_Click (again mimicing the ActiveX convention, but can be named anything):
Public Sub btnTest1_Click()
If Not GetFormButtonEnabled(Me, "btnTest1") Then Exit Sub
SetFormButtonEnabled Me, "btnTest2", False
SetFormButtonEnabled Me, "btnTest3", False
' Do some work...
End Sub
Public Sub btnTest2_Click()
If Not GetFormButtonEnabled(Me, "btnTest2") Then Exit Sub
' Do some work...
End Sub
Public Sub btnTest3_Click()
If Not GetFormButtonEnabled(Me, "btnTest3") Then Exit Sub
' Do some work...
End Sub
I am using a WizardControl in .NET 2.0. On the first Step (which is set to StepType="Start") when the next button is clicked, I run server-side validation code. But, no matter what I do it keeps going to the next step. Here is my code:
Protected Sub Wizard1_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.NextButtonClick
Dim oUser As New BE.User
Select Case Wizard1.ActiveStepIndex
Case 0
If Membership.FindUsersByName(UserName.Text).Count = 0 Then
oUser.UserName = UserName.Text
oUser.Password = Password.Text
oUser.Email = Email.Text
Wizard1.ActiveStepIndex = 1
Else
Wizard1.ActiveStepIndex = 0
ErrorMessage.Text = "user name already in use"
End If
Case 1
Case 2
End Select
End Sub
You can write e.Cancel=true
if you are working in any wizard event. Here "e" is an alias for WizardNavigationEventArgs
The Wizard control's NextButtonClick event has a
"WizardNavigationEventArgs" parameter that contains a "Cancel" property
help to cancel the current next navigation operation.
courtesy of
Steven Cheng Microsoft Online Support
As others have mentioned, you can use the Cancel property of the WizardNavigationEventArgs. Here's your code updated to reflect that:
Protected Sub Wizard1_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.NextButtonClick
Dim oUser As New BE.User
Select Case Wizard1.ActiveStepIndex
Case 0
If Membership.FindUsersByName(UserName.Text).Count = 0 Then
oUser.UserName = UserName.Text
oUser.Password = Password.Text
oUser.Email = Email.Text
Wizard1.ActiveStepIndex = 1
Else
Wizard1.ActiveStepIndex = 0
ErrorMessage.Text = "user name already in use"
' Set the Cancel property to True here
e.Cancel = True
End If
Case 1
Case 2
End Select
End Sub