473,587 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disable Button During Processing & Re-Enable After Processing?

Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_ Click(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCreateExcel. Click
Me.btnCreateExc el.Enabled = False
Dim pCreateExcel As New ClassCreateExce l
AddHandler pCreateExcel.Cr eateExcelComple ted, AddressOf
CreateExcelComp letedEventHandl er
Dim thCreateExcel As System.Threadin g.Thread
thCreateExcel = New System.Threadin g.Thread(Addres sOf
pCreateExcel.Pr ocess)
thCreateExcel.S tart()
End Sub
I've tried using the "CreateExcelCom pleteEventHandl er to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelComp letedEventHandl er(ByVal sender As
System.Object, ByVal e As System.EventArg s)
frmCreateExcelP rogress.Close()
Me.btnCreateExc el.Enabled = True
End Sub
The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don
Dec 14 '07 #1
3 3945
On 2007-12-14, Donald A. Fisher <ds****@bellsou th.netwrote:
Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_ Click(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCreateExcel. Click
Me.btnCreateExc el.Enabled = False
Dim pCreateExcel As New ClassCreateExce l
AddHandler pCreateExcel.Cr eateExcelComple ted, AddressOf
CreateExcelComp letedEventHandl er
Dim thCreateExcel As System.Threadin g.Thread
thCreateExcel = New System.Threadin g.Thread(Addres sOf
pCreateExcel.Pr ocess)
thCreateExcel.S tart()
End Sub
I've tried using the "CreateExcelCom pleteEventHandl er to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelComp letedEventHandl er(ByVal sender As
System.Object, ByVal e As System.EventArg s)
frmCreateExcelP rogress.Close()
Me.btnCreateExc el.Enabled = True
End Sub
>
The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don
This maybe a silly question - but is the event even beign fired? And if
it is, your most likely going to have issues because it's going to be
raised from your background thread...

Personally, if your going to do this, you might want to look into using
hte backgroundworke r component.

--
Tom Shelton
Dec 14 '07 #2
Tom Shelton wrote:
On 2007-12-14, Donald A. Fisher <ds****@bellsou th.netwrote:
>Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_ Click(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCreateExcel. Click
Me.btnCreateExc el.Enabled = False
Dim pCreateExcel As New ClassCreateExce l
AddHandler pCreateExcel.Cr eateExcelComple ted, AddressOf
CreateExcelCom pletedEventHand ler
Dim thCreateExcel As System.Threadin g.Thread
thCreateExcel = New System.Threadin g.Thread(Addres sOf
pCreateExcel.P rocess)
thCreateExcel.S tart()
End Sub
I've tried using the "CreateExcelCom pleteEventHandl er to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelComp letedEventHandl er(ByVal sender As
System.Objec t, ByVal e As System.EventArg s)
frmCreateExcelP rogress.Close()
Me.btnCreateExc el.Enabled = True
End Sub
>The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don

This maybe a silly question - but is the event even beign fired? And if
it is, your most likely going to have issues because it's going to be
raised from your background thread...

Personally, if your going to do this, you might want to look into using
hte backgroundworke r component.
Yes, the event is fired. I've even added a message box to confirm. I
do, however, get an debugging error that I'm not understanding. At the
beginning of my separate class that performs actions as follows:

Event Code:
Public Event CreateExcelComp leted(ByVal sender As Object, ByVal e As
System.EventArg s)
Then at the end of the class I have this line to call the handler:
RaiseEvent CreateExcelComp leted(Me, New System.EventArg s)
The handler runs the code above under "CreateExcelCom pletedEventHand ler"
after the class is finished (this is where I've added the message box
and I get it). I'm getting a debug error:

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'btnCreateExcel '
accessed from a thread other than the thread it was created on."
Source="System. Windows.Forms"
StackTrace:
at System.Windows. Forms.Control.g et_Handle()
at System.Windows. Forms.Control.O nEnabledChanged (EventArgs e)
at System.Windows. Forms.ButtonBas e.OnEnabledChan ged(EventArgs e)
at System.Windows. Forms.Control.s et_Enabled(Bool ean value)
at MT2.frmMain.Cre ateExcelComplet edEventHandler( Object sender,
EventArgs e) in K:\VBS_05_PROJ\ Munitions Task Management Tools
v1.0\Munitions Task Management Tools v1.0\frmMain.vb :line 159
at MT2.ClassCreate Excel.Process() in K:\VBS_05_PROJ\ Munitions
Task Management Tools v1.0\Munitions Task Management Tools
v1.0\Class_Crea teExcel.vb:line 23
at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
at System.Threadin g.ExecutionCont ext.runTryCode( Object userData)
at
System.Runtime. CompilerService s.RuntimeHelper s.ExecuteCodeWi thGuaranteedCle anup(TryCode
code, CleanupCode backoutCode, Object userData)
at
System.Threadin g.ExecutionCont ext.RunInternal (ExecutionConte xt
executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ThreadHelper. ThreadStart()

when it reaches the line to re-enable the button:

Line to re-enable button:
Me.btnCreateExc el.Enabled = True
This may be a little too detailed and I apologize. I am not familiar
with background workers. I did much reading and found a lot about
threads etc. Am I going about it all wrong? Thanks again.

Don
Dec 15 '07 #3
On 2007-12-14, Donald A. Fisher <ds****@bellsou th.netwrote:
Tom Shelton wrote:
>On 2007-12-14, Donald A. Fisher <ds****@bellsou th.netwrote:
>>Hello. I've been working a vb project and have a form with a button on
it that performs some actions after disabling the button when clicked:

Code disabling button and starting actions:
Private Sub btnCreateExcel_ Click(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles btnCreateExcel. Click
Me.btnCreateExc el.Enabled = False
Dim pCreateExcel As New ClassCreateExce l
AddHandler pCreateExcel.Cr eateExcelComple ted, AddressOf
CreateExcelCo mpletedEventHan dler
Dim thCreateExcel As System.Threadin g.Thread
thCreateExcel = New System.Threadin g.Thread(Addres sOf
pCreateExcel. Process)
thCreateExcel.S tart()
End Sub
I've tried using the "CreateExcelCom pleteEventHandl er to re-enable the
button to no avail:

Code performed after actions are completed:
Private Sub CreateExcelComp letedEventHandl er(ByVal sender As
System.Object , ByVal e As System.EventArg s)
frmCreateExcelP rogress.Close()
Me.btnCreateExc el.Enabled = True
End Sub
>>The actions completed really don't matter (I think). I just want the
button to be disabled so the user cannot start multiple events by
double-clicking or being impatient and clicking again and then re-enable
the button after processing has finished. Any help would be great!
Thanks in advance!

Don

This maybe a silly question - but is the event even beign fired? And if
it is, your most likely going to have issues because it's going to be
raised from your background thread...

Personally, if your going to do this, you might want to look into using
hte backgroundworke r component.
Yes, the event is fired. I've even added a message box to confirm. I
do, however, get an debugging error that I'm not understanding. At the
beginning of my separate class that performs actions as follows:

Event Code:
Public Event CreateExcelComp leted(ByVal sender As Object, ByVal e As
System.EventArg s)
Then at the end of the class I have this line to call the handler:
RaiseEvent CreateExcelComp leted(Me, New System.EventArg s)
The handler runs the code above under "CreateExcelCom pletedEventHand ler"
after the class is finished (this is where I've added the message box
and I get it). I'm getting a debug error:

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'btnCreateExcel '
accessed from a thread other than the thread it was created on."
Source="System. Windows.Forms"
StackTrace:
at System.Windows. Forms.Control.g et_Handle()
at System.Windows. Forms.Control.O nEnabledChanged (EventArgs e)
at System.Windows. Forms.ButtonBas e.OnEnabledChan ged(EventArgs e)
at System.Windows. Forms.Control.s et_Enabled(Bool ean value)
at MT2.frmMain.Cre ateExcelComplet edEventHandler( Object sender,
EventArgs e) in K:\VBS_05_PROJ\ Munitions Task Management Tools
v1.0\Munitions Task Management Tools v1.0\frmMain.vb :line 159
at MT2.ClassCreate Excel.Process() in K:\VBS_05_PROJ\ Munitions
Task Management Tools v1.0\Munitions Task Management Tools
v1.0\Class_Crea teExcel.vb:line 23
at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
at System.Threadin g.ExecutionCont ext.runTryCode( Object userData)
at
System.Runtime. CompilerService s.RuntimeHelper s.ExecuteCodeWi thGuaranteedCle anup(TryCode
code, CleanupCode backoutCode, Object userData)
at
System.Threadin g.ExecutionCont ext.RunInternal (ExecutionConte xt
executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at System.Threadin g.ThreadHelper. ThreadStart()

when it reaches the line to re-enable the button:

Line to re-enable button:
Me.btnCreateExc el.Enabled = True
This may be a little too detailed and I apologize. I am not familiar
with background workers. I did much reading and found a lot about
threads etc. Am I going about it all wrong? Thanks again.

Don
Don,

As I suspected - you are getting a cross thread access violation. You
can not directly access windows controls outside of the thread on which
they were created. You need to change the code of your method
(something like):
Private Sub CreateExcelComp letedEventHandl er(ByVal sender As System.Object, ByVal e As System.EventArg s)
If Me.InvokeRequir ed Then
Me.Invoke ( _
new EventHandler(Me .CreateExcelCom pletedEventHand ler), _
new Object() {sender, e))
Else
frmCreateExcelP rogress.Close()
Me.btnCreateExc el.Enabled = True
End If
End Sub

--
Tom Shelton
Dec 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
629
by: Ashvin Navare | last post by:
Hi, We want to disable BACK & FORWARD button of the browser so that user is forced to navigate through the menu options provided in the site. The browser used is Internet Explorer only. Please suggest some solution or any other alternate approach. Early response is appreciated. Regards,
5
1692
by: Vern | last post by:
I would like to disable a button on the form if the user is not authorized to use it. I've created a custom button and would like for each button to know if it should be enabled or disabled based on another class that would contain the users authority levels. I know I can use the on form load event, but I rather test to see if the user has access to the button during the buttons OnPaint event? If they don't have access, I'd like to...
6
9068
by: Al Cohen | last post by:
I'm using a LinkButton to call some code, then do a redirect. The code does some queries, sends some emails, and takes a few moments before the redirect is performed. I'd like to disable my LinkButton during this period to prevent multiple submissions. (Obviously) the disabling should be done at the client side to prevent any delay. I've tried adding some javascript to LinkButton.Attributes: LinkButton1.Attributes.Add("onclick",...
3
3152
by: Jeff | last post by:
I have a payment form with a submit button. A large percentage of users double-click the submit button thus submitting their payment information twice. I would like to use javascript to disable the submit button once it's been clicked, yet still have the form submit. I can do this in ASP 2.0, however, ASP.Net seems to be adversely affected if you disable the submit button. Here's how I have it set up... The submit button is a...
0
1796
by: Ahmad Jalil Qarshi | last post by:
Hi! I have a problem while developing some webpages.The Problem is that:- How We Can Disable The Controls Of One Web Form From Other Web Form In Asp.net? Explanation:- There Should Be Two Web Form,Web Form1.aspx and Web Form2.aspx,Now From Web Form1.aspx When I Click One Button (Disable), Then Web Form2.aspx Buttons
6
2315
by: Mike | last post by:
I have a form that contains 240 "products". Each Product has a TR. Each TR contains a Yes and No radio button and a Product-Qty text input. A situation exists where I have to go through all the products and check the No radio button for all the products. In javascript, that is easy enough. When the No radio button is clicked, I have to put '0' in the Product-Qty text input, and disable the Product-Qty text input. I create an array of...
8
3687
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I disable the right mouse button? ----------------------------------------------------------------------- The oncontextmenu intrinsic event is the only safe and reliable method. Of the other approaches often presented, most depend on an alert box interrupting the process and rarely work. Note that oncontextmenu is a non-standard event and is not...
21
62431
convexcube
by: convexcube | last post by:
Hi everyone, I am trying to create a single window interface for my database. As I am using Access 2007, I have my document layout options set to tabbed documents without displaying documents tabs in order to get my main form to appear as the only element in the access application window. I have also set anchoring in the form so that controls that should always be visible are and others stretch to adjust to the size of the window. When...
3
3236
by: Venturini | last post by:
I am trying to put together a web page where the customer makes choices of products and is then given a total. I am extremely new to Javascript and have managed to get as far as I have from web searches for scripts and html and adapted them. I need to know the principles of how to disable certain options based on other selections within the form. I have attached my script and html below to help. For example of a customer selects (BASIC...
1
3705
by: eeriehunk | last post by:
Hi All, I wrote a procedure to clear all my text fields and disable all buttons in my form. And I call this procedure in a button (insert button). when I presses the insert button, after all my button operations, all the text fields get cleared and all my buttons get disabled, except my insert button. I do have code to disable this button in my procedure as well. But for some reason, every other button gets disable except the button which...
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.