Count Number of Selected Shapes in the ShapeSheet

Started by RhesusMinus, December 13, 2010, 12:52:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RhesusMinus

Hi.

Is it possible, in the shape sheet, to count the number of selected shapes?
I have an action that should be visible only if just 1 shape is selected.

THL

Jumpy

#1
Don't think so. You would have to use VBA and events to push the number of selected shapes to the shapesheet of that shape. Much work for that.

It would be more easy, to ignore the action in the VBA-Code, if there's more than one shape selected, although it would be nicer and more professional, if the action wouldn't be visible in that case, but difficult to accomplish, I think.

Don't even know, if there is a "Shape selected" event on page or doc level?

aledlund


Jumpy

#3
OK. It's good to know your object model, like Rhesus said in the other thread.
In that case, this will work:


Dim WithEvents win As Visio.Window

Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
  Set win = Application.ActiveWindow
End Sub

Sub StartManually()
  Set win = Application.ActiveWindow
End Sub

Private Sub win_SelectionChanged(ByVal Window As IVWindow)
  If win.Selection.Count <> 1 Then
    MsgBox "not 1 selected"
    'Or Yourshapes.Action.Visible = false
  Else
    MsgBox "only 1 selected"
    'Or Yourshapes.Action.Visible = true
  End If
End Sub



Less work than I thought.

Visio Guy

#4
You can't get selection info in the ShapeSheet because the ShapeSheet is behind a shape, and hence behind the "model", where a window is a "view" into the model.

In Visio, you can have several windows for the same page. A shape might be selected in one of them, but not in the others. If your shape could react to being selected, it would end up turning on visibility in the other windows anyway...or maybe the universe might just implode instead.

Anyway, maybe this helps to understand why Visio doesn't offer the capability.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

aledlund

#5
mobius strip application model
;D
al

Visio Guy

#6
...running on the Palindrome OS.
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Jumpy

#7
Thats could be a feat for quantum computing, so it ever comes.
In quantum land a shape could be selected and not selected, could be visible and not visible all at the same time.
It could be in different windows with different probabilities.
Or it is in all the windows at once, as long as you don't look at them.
Think of the possibilities: You could draw a shape, while in another window it's already drawn. Funtastic.

--------------

Back to my code above. It only works, when you use only one drawing window and the code monitors that.
I don't know, if it is possible to detect, if the active window changes and then to test if it is an drawing window and to than reference that window to the withevents variable.

Visio Guy

#8
Hey, it's bad enough that when I'm on the ICE, all my shapes shrink and turn a little bit blue!
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Visio Guy

Below is a modified version of Jumpys code that tracks when the active window changes. The code now detects changes to the selection in any active window. So you can have multiple windows open and it still works.


Dim WithEvents m_win As Visio.Window
Dim WithEvents m_app As Visio.Application

Sub StartManually()
  m_setEventObjects
End Sub
Private Sub Document_RunModeEntered(ByVal Doc As IVDocument)
  '// Toggle the run-mode triangle off, then on to set up events.
  m_setEventObjects
End Sub
Private Sub Document_DocumentOpened(ByVal Doc As IVDocument)
  m_setEventObjects
End Sub

Private Sub m_setEventObjects()
  '// Set the application:
  Set m_app = Visio.Application
  '// Set the event-tracking window object:
  Set m_win = Visio.Application.ActiveWindow
End Sub

Private Sub m_app_WindowActivated(ByVal Window As IVWindow)

  '// When a new window is activated, we reset m_win to
  '// that window...if it is a drawing window that can
  '// have selected shapes. Otherwise, we set it to nothing.
 
  If Window.Type = Visio.VisWinTypes.visDrawing Then
    Set m_win = Window
  Else
    '// ShapeSheet or stencil window or other non-drawing window:
    Set m_win = Nothing
  End If
 
End Sub

Private Sub m_win_SelectionChanged(ByVal win As IVWindow)

  MsgBox win.Selection.Count & " selected shapes!"

End Sub

For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

Jumpy

#10
Nice to see, that I had the right idea and that I'm not such a big "Diletant" as I thought.

Where "Diletant" means sth. like nonproffesional looser and has nothing to do with the effects of nearly the same name, that Chris obviously expirienced in our german lightspeed train that is so fast, that you normaly spend more time at the station waiting for it, than actually travelling with it.
It's generally a marvelous multifunctional train, that in winter can be used as refrigerator and in summer is used as sauna by poor Finns, who can't afford one of their own.