Catia VBA-How to subtract selected item - vba

I wan to write a code to select parent item in catia.
I select a object then run this code.
Set Sel = CATIA.ActiveDocument.Selection
Sel.Add Sel.Item(1).LeafProduct.Parent.Parent
then 2 item selected: item(1),item(2).
How to subtract item(1)? Thanks

Related

Is there really no way to count the number of Visible Slicer Items in a Slicer connected to an OLAP data source?

I'm trying to loop through all slicer items from three slicers and then call another sub within the inner most loop. The slicers are connected to an OLAP data source. For each slicer I'd only like it to loop through visible slicer items. I hide the items with no data after selecting an item from the outer slicer. If I use the following code, then it will loop through all slicer items, not just the visible ones:
'Begin Loop through Fund Name Slicer
i = 1
Do Until i = ActiveWorkbook.SlicerCaches(SC1).SlicerCacheLevels.Item.count + 1
'select ith item in Fund Name Slicer
ActiveWorkbook.SlicerCaches(SC1).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC1).SlicerCacheLevels.Item.SlicerItems(i).Name)
'hide items with no data in Scenario Name Slicer
With ActiveWorkbook.SlicerCaches(SC2).Slicers(S2)
.SlicerCacheLevel.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
.SlicerCacheLevel.SortItems = xlSlicerSortDataSourceOrder
End With
'Begin Loop through Scenario Name Slicer
j = 1
Do Until j = ActiveWorkbook.SlicerCaches(SC2).SlicerCacheLevels.Item.count + 1
'select jth item in Scenario Name Slicer
ActiveWorkbook.SlicerCaches(SC2).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC2).SlicerCacheLevels.Item.SlicerItems(j).Name)
'hide items with no data in Override Set Name Slicer
With ActiveWorkbook.SlicerCaches(SC3).Slicers(S3)
.SlicerCacheLevel.CrossFilterType = xlSlicerCrossFilterHideButtonsWithNoData
.SlicerCacheLevel.SortItems = xlSlicerSortDataSourceOrder
End With
'Begin Loop through Override Set Name Slicer
k = 1
Do Until k = ActiveWorkbook.SlicerCaches(SC3).SlicerCacheLevels.Item.count + 1
'Select kth item in Override Set Name Slicer
ActiveWorkbook.SlicerCaches(SC3).VisibleSlicerItemsList = Array( _
ActiveWorkbook.SlicerCaches(SC3).SlicerCacheLevels.Item.SlicerItems(k).Name)
'Call sub that copies and pastes summary values
Call SelectedComboOnly
k = k + 1
Loop
j = j + 1
Loop
i = i + 1
Loop
I tried to replace the Do Until statements with something like:
Do Until i = ActiveWorkbook.SlicerCaches(SC1).VisibleSlicerItems.count + 1
but this throws a run time error. I found here that "Attempting to access the VisibleSlicerItems property for slicers that are connected to an OLAP data source (SlicerCache.OLAP = True) generates a run-time error." Is there really no way to access the number of visible slicer items here? Or should I try something completely different? My macro does what it's supposed to do, but it takes about 25 minutes. I'm just trying to run through only visible slicer items to optimize the code. This would take the combinations that it has to run through from 1,458 to 84. I already have screen updating off.
Sorry, for the late answer, but I just want to share below solution that worked for me:
UBound(ThisWorkbook.SlicerCaches(SC1).SlicerCacheLevels(1).VisibleSlicerItemsList)
EDIT:
The .VisibleSlicerItemsList property returns an array so You can't use the .count property on It, like You would do on collections. In order to get the length of an array, use the function UBound (see here).
Try using this logic:
Sub Test()
Dim SC As SlicerCache
Dim SI As SlicerItem
For Each SC In ThisWorkbook.SlicerCaches 'this will loop through all your slicer caches in the whole workbook
'if you want to skip an slicer do it here and skip to the next
For Each SI In SC.VisibleSlicerItems 'this will loop through all your visible slicer items
'some code
Next SI
Next SC
End Sub

adding multiple listbox items into one listbox

I'm using vb.net form.
I have 8 listboxes (listbox1 , listbox2,listbox3..) each one contains many items and another empty listbox (listbox10). I'm trying to add first listboxes items into the listbox10
ex: ) listbox1 first items is 'A' , listbox2 first items is 'b' , listbox3 first items is 'c'...etc . Now listbox10 first item must be "Abc..."
The problem is i'm only able to add 3 listboxes into the listbox10
But i want to all 8 listboxes to include in the loop.
here is my code
Dim n As Integer = _
Math.Min(ListBox1.Items.Count, Math.Min(ListBox2.Items.Count, ListBox3.Items.Count))
For i As Integer = 0 To n - 1
ListBox10.Items.Add( _
ListBox1.Items(i).ToString + _
ListBox2.Items(i).ToString + _
ListBox3.Items(i).ToString)
Next
Create a List<Listbox> of all the listboxes that you want to work on.
Then, using Linq, is pretty easy to reach your objective
Dim controls = new List(Of Listbox)() From _
{ listbox1, listbox2, listbox3, ....etc.... }
Dim minCount = controls.Min(Function(x) x.Items.Count)
for x = 0 to minCount-1
listbox10.Items.Add(string.Join(" ", controls.Select(Function(lb) lb.Items(x).ToString)))
Next
Of course, if all the Listbox have the same amount of items then the calculation to find the listbox with the lower amount of items is useless, you could replace it using the Items.Count of any Listbox (However, in defensive programming, I would leave it)

Add Text to start of ListBox

I want to know how I can add TextBox text to the start of some of items in my Listbox. For example when I type 'Game 1' in the TextBox it shows that at each timing like Game 1: 00:00:00 and if I type 'Game 2' It shows it like Game 2: 00:00:00 .
I am using VB.Net, any help would be appreicated
If you are using VB.net then let's just say you want to change the last Item of your Listbox then you could have the code below:
Dim itm As String = ListBox1.Items(ListBox1.Items.Count - 1)
ListBox1.Items(ListBox1.Items.Count - 1) = TextBox1.Text + itm
If you want the selected item from the Listbox is where the value of the Textbox will be added then you could use SelectedIndex property instead like:
Dim itm As String = ListBox1.Items(ListBox1.SelectedIndex)
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text + itm

Select Line from listbox and then convert to string

I want to search for a small bit of text in my listbox, if it's in the listbox I want to select it, and then covert it to a string.
How do I do this?
Because I cannot find the good command to select something on a specific line!
Thanks
To select a ListBox item, set the ListBox's SelectedIndex property. So, for example:
Dim stringToFind As String = "someString"
For i As Integer = 0 To Me.MyListBox.Items.Count - 1
Dim itemAsString As String = Me.MyListBox.Items(i).ToString()
If itemAsString.Contains(stringToFind) Then
Me.MyLabel.Text = itemAsString
Me.MyListBox.SelectedIndex = i
Exit For 'If you're using a MultiSelect ListBox, you can add to Me.MyListBox.SelectedIndices and remove this line.
End If
Next

Multi Select List Box

I have a list box on a form and it works fine for what I want to do.
I am wanting to edit items on the form, this means populating the listbox and then selecting the relevant items.
My listbox contains a list of item sizes, i want to select the sizes which belong to the item being edited.
PLease can someone give me some pointers.
I tried me.lstItemSizes.SetSelected(i,true) but this only works for a single item.
Any help wil be much appreciated.
My Code:
Private Sub SelectItemSizes(ByVal itemID As Integer)
Dim itemSizes As IList(Of ItemSize) = _sizeLogic.GetItemSizes(itemID)
Me.lstItemSizes.SelectionMode = SelectionMode.MultiExtended
If (itemSizes.Count > 0) Then
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
End If
End Sub
Did you set the selectionmode to multi?
You need to specify that in order to allow multiple selections.
Then you can do:
Dim i as Integer=0
For i=0 To Me.listBox.SelectedItems.Count -1
'display the listbox value
next i
Here is a screen shot:
After you set the property on the listbox then call setselected based on the values you want selected.
me.lstItemSizes.SetSelected(3,true)
me.lstItemSizes.SetSelected(4,true)
me.lstItemSizes.SetSelected(9,true)
Here you can add 20 numbers and only select the even.
Dim i As Integer
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
'now use setselected
'assume only even are selected
For i = 0 To 20
If i Mod 2 = 0 Then
Me.ListBox1.SetSelected(i, True)
End If
Next
3rd edit
Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:
Dim i As Integer
Dim l As New List(Of Integer)
l.Add(2)
l.Add(6)
l.Add(20)
'load the list with 20 numbers
For i = 0 To 20
Me.ListBox1.Items.Add(i)
Next
Dim lCount As Integer = 0
For lCount = 0 To l.Count - 1
For i = 0 To 20
If i = l.Item(lCount) Then
Me.ListBox1.SetSelected(i, True)
Exit For
End If
Next
Next
In the code my l is a list of just 3 items: 2, 6, and 20.
I add these items to l which is just a list object.
So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.
Notice in my for loop that once the item in my list is found I no longer need to loop so I exit for. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.
After running my code here is the result
You have to change the ListBox.SelectionMode property in order to enable multiple-selection.
The possible values are given by the SelectionMode enum, as follows:
None: No items can be selected
One: Only one item can be selected
MultiSimple: Multiple items can be selected
MultiExtended: Multiple items can be selected, and the user can use the Shift, Ctrl, and arrow keys to make selections
So, you simply need to add the following line to the code you already have:
' Change the selection mode (you could also use MultiExtended here)
lstItemSizes.SelectionMode = SelectionMode.MultiSimple;
' Select any items of your choice
lstItemSizes.SetSelected(1, True)
lstItemSizes.SetSelected(3, True)
lstItemSizes.SetSelected(8, True)
Alternatively, you can set the SelectionMode property at design time, instead of doing it with code.
According to MSDN, SetSelected() can be used to select multiple items. Simply repeat the call for each item that needs to be selected. This is the example they use:
' Select three items from the ListBox.
listBox1.SetSelected(1, True)
listBox1.SetSelected(3, True)
listBox1.SetSelected(5, True)
For reference, this is the MSDN article.
Because my code had the following loops:
For i As Integer = 0 To Me.lstItemSizes.Items.Count - 1
For x As Integer = 0 To itemSizes.Count - 1
If (CType(Me.lstItemSizes.Items(i), PosSize).SizeID = itemSizes(x).SizeID) Then
Me.lstItemSizes.SetSelected(i, True)
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Next
Next
The first loop loops through the available sizes and the second loop is used to compare the item sizes.
Having the following code:
Else
Me.lstItemSizes.SetSelected(i, False)
End If
Meant that even if item i became selected, it could also be deselected.
SOLUTION:
Remove Me.lstItemSizes.SetSelected(i, False) OR Include Exit For