Enabling/disabling commandbar items at runtime

S

SomeNewDev

Hi,

I've got problems accessing some properties of buttons on my own toolbar in
Visio. Mainly I really need to change the 'Enabled' property of some buttons
(depending on specific states, which are changing at runtime).

I tried both.. creating/refering to them as "Office.CommandBars"-objects and
as "Visio.UIObject"-objects aswell. Both ways of using custom buttons in
Visio are just working fine. But they both start working weird, when it comes
to changing their properties at runtime.

For example the 'Enabled' property: Sometimes accessing it results in an
exception (via Office.CommandBars).. sometimes I can change it, but the state
doesn't update in Visio anyway (Visio.UIObject).

And yes.. I AM keeping them at a class level, that they aren't collected by
the GC at runtime. Again.. everything works fine with them, inspite of
changing their properties like 'Enabled' at runtime.

I'm doing the same in Excel, PowerPoint, MSProject and Word - it works
perfect !
Why not in Visio ?

Thanks in advance.
 
S

SomeNewDev

Thanks,

actually I'm developing an add-in (C#), but that shouldn't make a
difference, right ? The example you are refering to is not quite what I need.
I need to enable/disable the buttons MYSELF, not by just creating them as
custom ones for 1 document ! You see I need to enable/disable _some_ of mine
on specific events - NOT all of them when the doc is deactivated :\

Any idea ?
 
J

JuneTheSecond

Yes, the first part of the example is the problem.
This example sometimes multiple toolbars of same name.
I think this part must be modified for the needs.
I've modified the "'Check whether there are document custom toolbars."
part as "Set vsoUIObject = GetUIObject", where GetUIObject is a custom
function, because I wished to make toolbars in application mode but not for
document mode.

Function GetUIObject() As Visio.UIObject
If Application.CustomMenus Is Nothing Then
Set GetUIObject = Application.BuiltInMenus
Else
Set GetUIObject = Application.CustomMenus
End If
End Function

And last line "ThisDocument.SetCustomToolbars vsoUIObject"
is modified to "Application.SetCustomToolbars vsoUIObject"
--
Best Regards.

JuneTheSecond
Now, visual calculation is more visual.
http://www.geocities.jp/visualcalculation/english/index.html
 
S

SomeNewDev

@JuneTheSecond:
Thx for your replies, but you don't seem to understand my problem.

In my eyes this is a BUG in Visio, so here's an example add-in.
With it everybody should be able to reproduce the odd behaviour,
which is commented in the code:

using System;
using Extensibility;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Office = Microsoft.Office.Core;
using Visio = Microsoft.Office.Interop.Visio;

namespace VisioCheck1
{
[GuidAttribute("70A9397E-F726-4EF7-9948-A9C06BE1B22C"),
ProgId("VisioCheck1.Connect")]
public class Connect : Object, IDTExtensibility2
{
Visio.Application appVisio;
Office.CommandBar toolBar;
Office.CommandBarButton tbButton1;
Office.CommandBarButton tbButton2;

public Connect() { }
public void OnConnection(object application, ext_ConnectMode
connectMode, object addInInst, ref Array custom)
{
appVisio = (Visio.Application)application;
if (connectMode != ext_ConnectMode.ext_cm_Startup)
OnStartupComplete(ref custom);
}
public void OnAddInsUpdate(ref System.Array custom) { }
public void OnStartupComplete(ref System.Array custom)
{
Office.CommandBars commandBars = appVisio.CommandBars as
Office.CommandBars;
try { toolBar = commandBars["VisioCheck1"]; toolBar.Delete(); }
catch { }
toolBar = commandBars.Add("VisioCheck1", 1, Type.Missing, true);
toolBar.Visible = true;

tbButton1 = (Office.CommandBarButton)toolBar.Controls.Add(
Office.MsoControlType.msoControlButton, 1, Type.Missing,
Type.Missing, true);
tbButton1.Tag = tbButton1.GetHashCode().ToString();
tbButton1.Caption = "No active drawing";
tbButton1.OnAction = "!<VisioCheck1.Connect>";
tbButton1.Style = Office.MsoButtonStyle.msoButtonCaption;
tbButton1.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(tbButton_Click);

tbButton2 = (Office.CommandBarButton)toolBar.Controls.Add(
Office.MsoControlType.msoControlButton, 1, Type.Missing,
Type.Missing, true);
tbButton2.Tag = tbButton2.GetHashCode().ToString();
tbButton2.Caption = "Active drawing";
tbButton2.OnAction = "!<VisioCheck1.Connect>";
tbButton2.Style = Office.MsoButtonStyle.msoButtonCaption;
tbButton2.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(tbButton_Click);
tbButton2.Enabled = false;

appVisio.WindowActivated += new
Visio.EApplication_WindowActivatedEventHandler(appVisio_WindowActivated);
appVisio.BeforeWindowClosed += new
Visio.EApplication_BeforeWindowClosedEventHandler(appVisio_BeforeWindowClosed);
}
public void OnBeginShutdown(ref System.Array custom)
{
try { toolBar.Delete(); } catch { }
}
public void OnDisconnection(ext_DisconnectMode disconnectMode, ref
Array custom)
{
if (disconnectMode != ext_DisconnectMode.ext_dm_HostShutdown)
OnBeginShutdown(ref custom);
}

void tbButton_Click(Office.CommandBarButton Ctrl, ref bool
CancelDefault)
{
MessageBox.Show("\"" + Ctrl.Caption + "\" pressed!");
}
void appVisio_WindowActivated(Visio.Window Window)
{
if (Window.Type == (short)Visio.VisWinTypes.visDrawing)
{
// 1.Method: Always throws Exceptions:
//tbButton1.Enabled = false;
//tbButton2.Enabled = true;

// 2.Method: At least works "most" of the times:
toolBar.Controls["No active drawing"].Enabled = false;
toolBar.Controls["Active drawing"].Enabled = true;
}
}
void appVisio_BeforeWindowClosed(Visio.Window Window)
{
if (appVisio.Windows.Count == 1)
{
// 1.Method: Always throws Exceptions:
//tbButton1.Enabled = true;
//tbButton2.Enabled = false;

// 2.Method: At least works "most" of the times:
toolBar.Controls["No active drawing"].Enabled = true;
toolBar.Controls["Active drawing"].Enabled = false;
}
}
}
}


PS: The 2. method often doesn't work ('delayed'), when you open a template
from the 'first steps' screen !
 
S

SomeNewDev

Hi Nikolay.
Um.. prolly not the cleanest way, but at least it seems to work via
VisioIsIdle event. Thanks a lot.. really - I already gave up on it :)

regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top