Set combobox item active

Set combobox item active

Anonymous
Not applicable
1,309 Views
4 Replies
Message 1 of 5

Set combobox item active

Anonymous
Not applicable

Hi, I have made a combobox in the ribbon with 5 sheet sizes. What I want is to set the combobox item active corresponding the opened drawing.

 

So, when I open an A3 sheet, I want set the active item in the combobox to the same value.

 

I tried:

 

combobox.ListIndex = 4

combobox.set_ListItem(4,"A3")

 

But the combobox stays at it's initial value 😞

 

HOw can I achieve this ?

 

0 Likes
Accepted solutions (1)
1,310 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

anyone ?

0 Likes
Message 3 of 5

frederic.vandenplas
Collaborator
Collaborator

Can you post the piece of code that actually retrieves the combobox you've created?

That would save me a lot of reproduction time

 

Met vriendelijke groeten

If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"
0 Likes
Message 4 of 5

Anonymous
Not applicable

public void SetSheetFormat(Application inventapp)
{
DrawingDocument doc = inventapp.ActiveDocument as DrawingDocument;
if (doc == null) return;
CommandManager cmdman = inventapp.CommandManager;
ControlDefinitions ctrldefs = cmdman.ControlDefinitions;
ComboBoxDefinition cmbdef = ctrldefs["SheetFormats"] as ComboBoxDefinition;
if (cmbdef == null) return;
Sheet sheet = doc.ActiveSheet;

 

 

// Okay, so here I get the size of the loaded document. Then I want my combobox to display this value
/*
DrawingSheetSizeEnum dform = sheet.Size;
switch (dform)
{
case DrawingSheetSizeEnum.kA0DrawingSheetSize:
cmbdef.ListIndex = 1;
break;
case DrawingSheetSizeEnum.kA1DrawingSheetSize:
cmbdef.ListIndex = 2;
break;
case DrawingSheetSizeEnum.kA2DrawingSheetSize:
cmbdef.ListIndex = 3; // tried this with no effect
break;
case DrawingSheetSizeEnum.kA3DrawingSheetSize:
cmbdef.set_ListItem(4, "A3"); // tried this with no effect
break;
case DrawingSheetSizeEnum.kA4DrawingSheetSize:
cmbdef.ListIndex = 5;
break;
}
*/

 

}

 

So I can't figure out how to change the selected index of a combobox in a ribbon.

 

0 Likes
Message 5 of 5

frederic.vandenplas
Collaborator
Collaborator
Accepted solution

Hi,

 

I've recreated this in vba (easier for debugging) and this worked for me

 

Public Sub setComboItem()

 

Dim cmdman As CommandManager
Set cmdman = ThisApplication.CommandManager
Dim ctrldefs As ControlDefinitions
Set ctrldefs = cmdman.ControlDefinitions
Dim cmbdef As ComboBoxDefinition
Set cmbdef = ctrldefs.Item("SheetSizeCombobox--> you need to change this to the internal name of the combobox")

If cmbdef Is Nothing Then
    Return
Else
'find the sheetsize and set the listindex according to the value
    cmbdef.ListIndex = 1
   
End If

End Sub

 

I've created a comobobox using this snippet below from another post for my convenience.

'***********************************************
' Copy below code to a module 
Public oChangeMaterial As clsChangeMaterial

Public Sub CreateComboBox()
    ' Set a reference to the active document.
    ' This assumes that a part document is active.
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oCommandMgr As CommandManager
    Set oCommandMgr = ThisApplication.CommandManager
    
    ' Set a reference to the collection of ControlDefinitions
    Dim oControlDefs As ControlDefinitions
    Set oControlDefs = oCommandMgr.ControlDefinitions
    
    On Error Resume Next
    ' Create a combobox definition
    Dim oComboBoxDef As ComboBoxDefinition
    Set oComboBoxDef = oControlDefs.Item("MaterialsComboBox")
    
    If oComboBoxDef Is Nothing Then
        Set oComboBoxDef = oControlDefs.AddComboBoxDefinition("Materials", "MaterialsComboBox", kNonShapeEditCmdType, 125, , "Materials Combo", "Materials")
        
        ' Set a reference to the materials collection.
        Dim oMaterials As Materials
        Set oMaterials = oDoc.Materials
        
        Dim oMaterial As Material
        
        For Each oMaterial In oMaterials
            ' Add material names to the combo box definition
            oComboBoxDef.AddItem (oMaterial.Name)
        Next
        
        ' Set a reference to the Part Standard toolbar
        Dim oPartFeatureToolbar As CommandBar
        Set oPartFeatureToolbar = ThisApplication.UserInterfaceManager.CommandBars.Item("PMxPartFeatureCmdBar")
        
        ' Add a combo box control to the toolbar
        Dim oComboBoxControl As CommandBarControl
        Set oComboBoxControl = oPartFeatureToolbar.Controls.AddComboBox(oComboBoxDef,1)
        
        oPartFeatureToolbar.Visible = True    
    End If
    
    ' Clear current selection
    oComboBoxDef.ListIndex = 0
  
    Set oChangeMaterial = New clsChangeMaterial
    oChangeMaterial.Initialize
End Sub


'*************************************************************
' The declarations and functions below need to be copied into
' a class module whose name is "clsChangeMaterial".  The name 
' can be changed but you'll need to change the declaration in
' the calling function "CreateComboBox" to use the new name.

Option Explicit
Private WithEvents oComboBoxDef As ComboBoxDefinition


Public Sub Initialize()

    Dim oCommandMgr As CommandManager
    Set oCommandMgr = ThisApplication.CommandManager
   
    ' Set a reference to the collection of ControlDefinitions
    Dim oControlDefs As ControlDefinitions
    Set oControlDefs = oCommandMgr.ControlDefinitions
    
    ' Set a reference to the "MaterialsComboBox" combo box definition
    Set oComboBoxDef = oControlDefs.Item("MaterialsComboBox")
        
End Sub

Private Sub oComboBoxDef_OnSelect(ByVal Context As NameValueMap)
    
    If oComboBoxDef.ListIndex = 0 Then
        Exit Sub
    End If
    
    ' Get the selected item
    Dim oMaterialName As String
    oMaterialName = oComboBoxDef.ListItem(oComboBoxDef.ListIndex)
    
    ' Set a reference to the active part document
    Dim oDoc As PartDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oPartCompDef As PartComponentDefinition
    Set oPartCompDef = oDoc.ComponentDefinition
    
    ' Get the selected material
    Dim oMaterial As Material
    Set oMaterial = oDoc.Materials.Item(oMaterialName)
    
    ' Change the part material
    oPartCompDef.Material = oMaterial
End Sub
If you think this answer fullfilled your needs, improved your knowledge or leads to a solution,
please feel free to "kudos"