Enable or disable buttons in VB.NET

Enable or disable buttons in VB.NET

martin_mmj
Enthusiast Enthusiast
2,928 Views
5 Replies
Message 1 of 6

Enable or disable buttons in VB.NET

martin_mmj
Enthusiast
Enthusiast
My addin was written in VB.NET/VisualStudio 2008. I created my own CommandBar with a few buttons. Works fine, but I don't know how to enable/disable my buttons depending on type of documents by VB.NET.

For example: How to enable my buttons on my panel only in IDW drawings and disable it in other type of documents?

Can you help me? Exists any sample?

Thanks Martin
0 Likes
2,929 Views
5 Replies
Replies (5)
Message 2 of 6

rthelkap
Enthusiast
Enthusiast
This can be done by handling events fired by Inventor.

For example to disable certain buttons when a drawing file is opened handle the event "OnOpenDocument" which will be fired whenever a document is opened.
The first argument is a DocumentObject from which you can know the type of document, and then you get ButtonDefinition object of a button you want to disable and then can use Enabled() to enable/disable the button.
0 Likes
Message 3 of 6

Anonymous
Not applicable

The previous response is correct in that you do
need to listen to events and enable or disable your buttons appropriately. 
However, the OnOpenDocument is probably not the best choice.  In the simple
case of opening a drawing and not doing anything else with another document it
will work but in other cases you'll run into problems.  For example, if you
open the drawing you'll get the OnOpenDocument event and enable your
command.  Still leaving the drawing open you open a part.  You'll get
the OnOpenDocument for the part and disable the commands.  When you switch
back to the drawing you won't get the OnOpenDocument event because it's
already open and your commands will remain disabled.

 

I think the best choice is the OnNewEditObject
event.  This is fired whenever the object being currently edited
changes.  For example, even within a drawing this event will fire when you
edit sketches.  This is good because your command should probably only be
enabled in the general drawing environment and not when sketching within a
drawing.
--
Brian Ekins
Autodesk Inventor API

href="http://blogs.autodesk.com/modthemachine">http://blogs.autodesk.com/modthemachine
0 Likes
Message 4 of 6

Anonymous
Not applicable

Is not OnEnvironmentChange the preferred event to
hande this? Seems to be easier to manage there.

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


The previous response is correct in that you do
need to listen to events and enable or disable your buttons
appropriately.  However, the OnOpenDocument is probably not the best
choice.  In the simple case of opening a drawing and not doing anything
else with another document it will work but in other cases you'll run into
problems.  For example, if you open the drawing you'll get the
OnOpenDocument event and enable your command.  Still leaving the drawing
open you open a part.  You'll get the OnOpenDocument for the part and
disable the commands.  When you switch back to the drawing you won't get
the OnOpenDocument event because it's already open and your commands will
remain disabled.

 

I think the best choice is the OnNewEditObject
event.  This is fired whenever the object being currently edited
changes.  For example, even within a drawing this event will fire when
you edit sketches.  This is good because your command should probably
only be enabled in the general drawing environment and not when sketching
within a drawing.
--
Brian Ekins
Autodesk Inventor API

href="http://blogs.autodesk.com/modthemachine">http://blogs.autodesk.com/modthemachine
0 Likes
Message 5 of 6

Anonymous
Not applicable

OnEnvironmentChange will work too.  I should
have listed that in my response.  Both are valid and which one to
use really depends on the desired scope of your command.  One thing
with environments is that applications can create their own environments and if
you didn't know about them when you wrote your program you won't know at runtime
whether the environment is valid for your command or not.

 

In any case you need to be tolerant in your code of
getting some type of object you weren't expecting.  You should at least
handle it gracefully and not crash.  It seems there's always some obscure
environment or edit object that you didn't know about and missed in your
testing.
--
Brian Ekins
Autodesk Inventor API

href="http://blogs.autodesk.com/modthemachine">http://blogs.autodesk.com/modthemachine


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
"Neil Munro" <nmunro-replace with ATsymbol-shaw-replace with DOT
symbol-ca> wrote in message
href="news:6050563@discussion.autodesk.com">news:6050563@discussion.autodesk.com
...


Is not OnEnvironmentChange the preferred event to
hande this? Seems to be easier to manage there.

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


The previous response is correct in that you do
need to listen to events and enable or disable your buttons
appropriately.  However, the OnOpenDocument is probably not the best
choice.  In the simple case of opening a drawing and not doing anything
else with another document it will work but in other cases you'll run into
problems.  For example, if you open the drawing you'll get the
OnOpenDocument event and enable your command.  Still leaving the
drawing open you open a part.  You'll get the OnOpenDocument for the
part and disable the commands.  When you switch back to the drawing you
won't get the OnOpenDocument event because it's already open and your
commands will remain disabled.

 

I think the best choice is the OnNewEditObject
event.  This is fired whenever the object being currently edited
changes.  For example, even within a drawing this event will fire when
you edit sketches.  This is good because your command should probably
only be enabled in the general drawing environment and not when sketching
within a drawing.
--
Brian Ekins
Autodesk Inventor API

href="http://blogs.autodesk.com/modthemachine">http://blogs.autodesk.com/modthemachine
0 Likes
Message 6 of 6

martin_mmj
Enthusiast
Enthusiast
Hi guys, thanks for responses. I did it along your recommendations.
Martin

Private Sub appEvents_OnNewEditObject(ByVal EditObject As Object, _
ByVal BeforeOrAfter As Inventor.EventTimingEnum, _
ByVal Context As Inventor.NameValueMap, _
ByRef HandlingCode As Inventor.HandlingCodeEnum) _
Handles appEvents.OnNewEditObject
'
If (BeforeOrAfter = EventTimingEnum.kAfter) Then
Select Case m_inventorApplication.ActiveDocumentType
Case DocumentTypeEnum.kDrawingDocumentObject
m_featureFA_ButtonDef.Enabled = True
m_featureIB_ButtonDef.Enabled = True
Case Else
m_featureFA_ButtonDef.Enabled = False
m_featureIB_ButtonDef.Enabled = False
End Select
End If
HandlingCode = HandlingCodeEnum.kEventNotHandled
''
End Sub
0 Likes