Excel 2010 - Disable button - button

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

Related

Raising User Control Event VB.NET and passing variable

I am delving into territory unfamiliar to me. I'm creating a User Control that I'd like to use across multiple forms. (thus reason for User Control). I am only familiar with VB.NET language. I've googled and I think I'm close to understanding, but I just can't figure this out. I've got multiple buttons on my User Control. When the "upload" button is pressed on my user control, I'd like it to run a Sub on my parent page called UploadFile(), which has one string parameter. I will have the UserControl on my page mulitple times like this:
<uc1:UploadFile ID="UploadFile1" runat="server" />
<uc1:UploadFile ID="UploadFile2" runat="server" />
In my code behind of the parent page, I'd like to execute this function on the Click event of the upload button within the user control. Where the unique parameters will then tell me what folders I will be uploading my file to. So that when user uploads a file within my first User Control on the page, it will run my Upload Sub on my parent page, take the parameter "a" and run my Sub that uploads my document and saves to database for files specific to "a" type. I will then tell the user control to change it's label to show that File A has been uploaded
Sub something somethingUploadFile1_Click
UploadFile("a")
End Sub
Sub something somethingUploadFile2_Click
UploadFile("b")
End Sub
Sub UploadFile(ByVal myString as string)
Select Case myString
Case "a"
If UploadFile1.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile1.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
Case "b"
If UploadFile2.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile2.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
End Select
End Sub
I know that in above code I'm supposed to be somehow referencing the button on my UserControl, but I'm not sure how to do it. I also know that Raising Events and Event Delegation are two major components of this, but again, I'm just not figuring it out. Can you please show me how to complete my code using the existing examples I provided?
Here is the complete code behind of my existing UserControl:
Public Class UploadFile
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
End Sub
Protected Sub btnDelete_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnDelete.Click
End Sub
Public Property lblViewFile() As Label
Get
Return _lblViewFile
End Get
Set(ByVal value As Label)
_lblViewFile = value
End Set
End Property
Public Property btnDelete() As ImageButton
Get
Return _btnDelete
End Get
Set(ByVal value As ImageButton)
_btnDelete = value
End Set
End Property
Public Property pnlUpload() As Panel
Get
Return _pnlUpload
End Get
Set(ByVal value As Panel)
_pnlUpload = value
End Set
End Property
Public Property FileUpload() As FileUpload
Get
Return _FileUpload
End Get
Set(ByVal value As FileUpload)
_FileUpload = value
End Set
End Property
Public Property btnUpload() As Button
Get
Return _btnUpload
End Get
Set(ByVal value As Button)
_btnUpload = value
End Set
End Property
Public Property lblReplaceMsg() As Label
Get
Return _lblReplaceMsg
End Get
Set(ByVal value As Label)
_lblReplaceMsg = value
End Set
End Property
Private _lblViewFile As Label
Private _btnDelete As ImageButton
Private _pnlUpload As Panel
Private _FileUpload As FileUpload
Private _btnUpload As Button
Private _lblReplaceMsg As Label
End Class
asp.net vb user control raising an event on the calling page
derive the arguments class you need from EventArgs according to your structure and data and use that.

Stop radio button change

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

VB.Net passing/calling a variable

How do I pass a value over to a module/function. Whenever I search here or Google I am getting code for C# rather than VB.net
I want to be able to click on a button and pass a value to a module and action it. Each button has a value 1,2,3,4 etc... and they pass back to make a panel visible=true/false.
i.e. I want to say the below but when I click btnShow1 I want to pass this as a value to a function and hide/show the panel I am talking about.
Current code
Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
If panel1.Visible = True Then
panel1.Visible = False
Else
panel1.Visible = True
End If
End Sub
Guess code
'Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
vpanel = btnShowQ1.value
Call fncPanel(vpanel as string) as string
End Sub
Then somewhere - I guess in App_code I create function.vb
Imports Microsoft.VisualBasic
Public Class ciFunctions
Public Function fncPanel(vPanel as string)as string
If (vPanel).visible = False then
(vPanel).visible = true
Else
(vPanel).visible = false
End IF
End Function
End Class
Set the button's CommandArgument property to the panel number to which it shows or hides:
<asp:Button ID="Button1" runat="server" Text="Show/Hide" CommandArgument="1" />
<asp:Button ID="Button2" runat="server" Text="Show/Hide" CommandArgument="2" />
On the button click event:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, ...
' get the button which triggers this event
Dim thisButton As Button = DirectCast(sender, Button)
' construct the panel name to toggle visibility from the button's CommandArgument property
Dim panelName as String = "panel" & thisButton.CommandArgument
' call the function, passing the panel name and the reference to current page
TogglePanel(Me, panelName)
End Sub
You can add all the buttons click event handler to this sub by adding them to the Handles... list as above. Therefore you don't have to create event handler for each buttons on the page. Just one to handle them all.
Create the function to toggle visibility:
Sub TogglePanel(pg As Page, panelName As String)
' get the panel control on the page
Dim panel As Panel = DirectCast(pg.FindControl(panelName), Panel)
' toggle its visibility
panel.Visible = Not panel.Visible
End Sub
The pg parameter is required if you put this function in a class other than the page it runs on or in a module. If the function sit in the same page class, then pg is not required.
You should be able to just add a parameter of type Panel to the sub, and in the btnShow event, pass the item.
Public Sub ChangePanelVis(panel as Panel)
If panel.Visible then
panel.Visible = True
Else
panel.Visible = True
End If
End Sub
Protected Sub btnShowQ1_Click(sender As Object, e As System.EventArgs) Handles btnShowQ1.Click
ChangePanelVis(panel1)
End Sub

session variable set by checkbox not being stored/shared from page to page

I have a listview that's showing a long list. Each item has the ability to be 'hidden' or not. I want a checkbox to either show all the list, or not to show the hidden ones. The idea is that users will hide the older items they don't want to see any more, but may want to see at some point. I want to store the value of this decision in a session variable so if the user navigates to another page, then comes back, the ShowAllCheckbox will pre-populate to what the user has previously decided. Everything is working good, except i can't get the session variable to keep. It keeps going back to False. This is what I have:
aspx page:
Show Hidden: <asp:Checkbox ID="ShowHiddenCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="ShowHiddenCheckboxChange" />
...
<asp:ListView ...>
<!-- this list works fine, and pulls the correct records -->
aspx.vb page:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
' toggle the values
Dim CheckBoxField As CheckBox = TryCast(sender, CheckBox)
If CheckBoxField.Checked Then
Session("ShowHiddenRotations") = False
Else
Session("ShowHiddenRotations") = True
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'if i navigate to another page, and come back to this one, this comes back as "False". I don't understand how it could be reset to False.
Response.Write( "Session: " & Session("ShowHiddenRotations") )
'when page loads check if session variable has been set to show/hide the hidden rotations
If Session("ShowHiddenRotations") Then
If Session("ShowHiddenRotations") = True Then
'update sql query on select statement to show the hidden rotations
'update checkbox to show it as checked
ShowHiddenCheckBox.Checked = True
Else
ShowHiddenCheckBox.Checked = False
End If
Else
'not checked by default (ie don't show hidden)
ShowHiddenCheckBox.Checked = False
End If
End Sub
The Session variable always reverts back to False when i navigate to another page and come back to this one. My understanding of session variables was that they would pass their values from one page to another until the user closes the browser. Maybe there's another way of doing this, or something simple I'm missing. Any help is much appreciated! Thanks!
Is session-state enabled on your site? It can be disabled in a couple of different way, on a page level or even in web.config.
You should also be aware the Page_Load event fires for every request, before the check-box auto-postback happens.
I'm also a little confused as to what you're trying to store: I assume every row has a check-box, but it seems you're trying to store the set/not-set value in a single session variable. How do you differentiate which have been selected, and which ones hasn't? :)
Update:
Okay, let's try a clean the code up a little bit. First create a property to access the session value:
Private Property ShowHiddenRotations As Boolean
Get
If Not Session("ShowHiddenRotations") Is Nothing Then
Return CType(Session("ShowHiddenRotations"), Boolean)
Else
Return False
End If
End Get
Set(value As Boolean)
Session("ShowHiddenRotations") = value
End Set
End Property
If you're using that value on other pages, I would recommend moving it to a seperate class.
Then we can reduce your other code to something closer to this:
Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
ShowHiddenRotations = ShowHiddenCheckbox.Checked
End Sub
And ...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If not Page.IsPostBack
' Load your data, better stick it in a seperate sub...
ShowHiddenCheckBox.Checked = ShowHiddenRotations
else
' This section is executed BEFORE any control methods are run, i.e. ShowHiddenCheckBoxChange
end if
End Sub
I'm guessing your problem is really just the order of how things are called in your page. What happens when you debug through it?

Grid View, ASP.NET

When pressed Once,i want to make the Grid view Button(Time In) invisible until the User press Time Out Button.
Once the user press the Time Out Button,Time in Button must be shown
Have you looked at NServiceBus? Here's an introduction to it. http://www.udidahan.com/2009/02/07/nservicebus-19/
Just use the .Visible property on the buttons. For example:
btnTimeOut.Visible = False 'This will hide it
btnTimeOut.Visible = True ' ... and this will show it
To implement your solution, I'm assuming you've got two separate event handlers for each of your buttons. What you'd want to do is something like this:
Sub btnTimeIn_Click(o as Object, e as EventArgs)
'Hide the button now it has been clicked
Me.btnTimeIn.Visible = False
'Do some other stuff, such as record the Time In here...
End Sub
And then:
Sub btnTimeOut_Click(o as Object, e as EventArgs)
'Show the TimeIn button again
Me.btnTimeIn.Visible = True
'Record the time out etc...
End Sub
You can use the Load or Init events of the form to initialise the state of the buttons, i.e:
If Not Page.IsPostBack
InitialiseButtons()
End If
Private Sub InitialiseButtons()
Me.btnTimeIn.Visible = True
Me.btnTimeOut.Visible = False
End Sub
The "If Not Page.IsPostBack" property will stop your form being accidentally reset when the user clicks on one of your buttons after the initial load.
Hope this helps, feel free to pop any questions back up here.
Cheers

Resources