Lambda expression error in vb.net - vb.net

I'm a newbie to this linq stuff. I never used any linq before. So when I had a scenario to move selected items from left list to the right list, I've got a nice solution from search which is in C#, but I converted it into VB. here is the code I've got
Dim leftItems = lb_left.Items.Cast(Of ListItem)().ToList()
Dim rightItems = lb_right.Items.Cast(Of ListItem)().ToList()
'Get all selected items from left box
Dim LeftSelectedItems = leftItems.Where(Function(a) a.Selected).ToList()
'Add all selected items to right box
'Clear lb_right Items and add sorted list
lb_right.Items.Clear()
LeftSelectedItems.Union(rightItems).OrderBy(Function(a) a.Text).ToList().ForEach(Function(b) lb_right.Items.Add(b))
'Remove all selected items from left box
LeftSelectedItems.ForEach(Function(a) lb_left.Items.Remove(a))
The above is the code I got from internet to move left to right list box. But on that function in the ForEach it is giving me a kinda error "Expression does not produce a value"
I really got stucked with this error. Requesting your fast reply..

LeftSelectedItems.ForEach(Sub(a) lb_left.Items.Remove(a))

From the documentation for VB lambda expressions:
The body of a single-line function must be an expression that returns a value, not a statement. There is no Return statement for single-line functions. The value returned by the single-line function is the value of the expression in the body of the function.
As the compiler says, Add doesn't return a value.
I believe you could use Sub instead of Function, and use the multiline version - but I don't think that's the best way of working here.
It looks like you should be creating a query and then using a sort of "add all these items" call. Unfortunately you haven't told us the type of lb_right, or even whether you're using WPF, WinForms, ASP.NET etc.

Related

Pass parameter value to Revit family using .net api

I want to pass the parameter values to Revit family.I have spend many hours on google. In result i got few links which tells Read and Write Parameter Values with VB.NET
Read and Write Parameter Values with VB.NET
in this example we are fetching parameters and writing a value in text file called ParametersValue.txt. But i am confused, how should i pass this file to Revit?
I'm hoping someone can steer me in the right direction. I would really appreciate it!
One of the first things I would do after downloading the SDK mentioned in the previous post, is install the included revit lookup addin. This has been incredibly valuable in figuring what elements are called in the API, as well as determining which storage type the parameter is using. If all of the parameters you want to update are strings, those will be fairly straightforward to set from your text file. However, if,for example, the parameter value that you think is a string is actually set by an elementid, then there will be some coding involved to get the proper info to set the parameter value.
You can easily pass parameters to component families using "FamilyManager" class. The FamilyManager class provides access to family types and parameters. Just get the parameter and set its value. As we are working in family editor for component families, we have to load the parameter values in the project. I have tried this on Revit 2019. You have to
Open family in family editor
Activate the plugin
Click on Load into Project button on addins ribbon.
Then check the parameter value in the properties of that family.
Public Sub SetParamtersForComponentFamilies(ByVal doc As Document, ByVal parameterValue As String)
Dim f As Family = doc.OwnerFamily
Using trans As Transaction = New Transaction(doc, "Creating transaction for parameters")
trans.Start()
Dim familyMgr As FamilyManager = doc.FamilyManager
Dim n As Integer = familyMgr.Parameters.Size
Dim comment As FamilyParameter = familyMgr.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS)
familyMgr.Set(comment, parameterValue)
TaskDialog.Show("Paramters", "TypeComments : Updated")
trans.Commit()
End Using
End Sub
I use C# when writing Revit API code because that's what all the samples are written in, but I might be able to get you pointed in the right direction with some additional details. Are you looking to assign a value to a specific parameter? For example: Height=30"?
If so you first have to "get" the parameter. In the example in spidernet he goes through every parameter of a selected element:
Dim element As Autodesk.Revit.DB.Element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element 'Prompts you to select an element
For Each p As Parameter In element.Parameters 'Goes through every parameter in "element" and assigns the parameter to "p"
If p.Definition.Name = "Height" Then 'Check if "p" is the name you want, "Height"
p.Set(2.5) 'Because Revit knows FEET, so in order to type in 30in you use 2.5
End If
Next 'Loop through parameters
If you're looking for it to do something else, please post again.
Also, not sure you're aware, but a blog that is FULL is great Revit API info is Jeremy Tammik's: http://thebuildingcoder.typepad.com. A lot of his examples are C#, which is why I started learning C# instead of VB.NET.
If you don't have it already, make sure you get the SDK for Revit 2014 here: http://images.autodesk.com/adsk/files/Revit2014SDK_RTM0.exe
It has a TON of samples that might help too. Good Luck!

Get Values from Listbox for if functions

Hey guys very new here.
Have a listbox that gets account names from a specific game server using this command line
Dim apikeyinfo As APIKeyInfo = api.getApiKeyInfo()
lstbxCharacters.DataSource = apikeyinfo.Characters
this code gets all the characters in a single account by displaying it in a listbox.
Now i would like to reference a character from the lisbox but not sure how
Any method such as Listbox.Get to get the value and compare it with something else?
Thanks
you can try something like
lstbxCharacters.SelectedItem
Update
To read the data from the listbox I think there are multiple ways (Assuming that it is readable).
-> Usually listbox display strings, so it should work to read to a string variable
Dim a_string as Strin = lstbxCharacters.SelectedItem
also you may like to add a small check before, assuring that an Item is currently selected:
If lstbxCharacters.SelectedIndex < 0 then return
This jumps out of current sub if no item is selected
And finally, to read the first entry, you can also do it this way:
a_string = lstbxCharacters.Items(0)
if it returns objects, then instead of accessing the object directly, it may work to do
a_string = lstbxCharacters.Items(0).ToString
(most objects allow a .ToString() Function )
Here two ideas for different solutions:
As a user commented, you could access the DataSource directly + the information which listIndex was selected. But if you do so, then maybe it is more easy (if you need to access it anyways, to go with solution 2)
Create a variable of type list(Of some_object) and fill it with the data from the datasource. It will take some time to do this, but if you define for the class some_object a function ToString, then you can fill all objects directly to the lstbxCharacters, and access them without any worries, by doing CType(lstbxCharacters.SelectedItem, some_object)
Update 2
If with reference you mean to access further information from the datasource, then you need to build some kind of query, or set the content of the listbox in relation to another control that shows the database content (in that way the listbox lstbxCharacters would act like a filter)

Cannot access property value on listbox item

I have a listbox with multiple selection activated and i am trying to read the different selected values
I have tried many snippets, here are the latest two :
For i = 0 To ListBox1.SelectedIndices.Count
MsgBox(ListBox1.Items((ListBox1.SelectedIndices(i))).value)
Next
For i = 0 To ListBox1.SelectedItems.Count
MsgBox(ListBox1.SelectedItems(i).value)
Next
For some reason with any approach i choose i can't read any item's value
My listbox is data bound, so i found out on a forum that making it public might fix the issue but it did not
I am hesitating as even Intellisense doesn't show much info, all i get is :
Equals
GetHashCode
GeType
ReferenceEquals
ToString
Any ideas where i went wrong?
Thanks in advance
Edit:
I'm not a big fan of using the Data GUI tools in VB. That said, I think what you are looking for is this:
For Each dvRow As DataRowView In Me.Listbox1.SelectedItems
MessageBox.Show(dvRow("Id").ToString)
Next
If you were interested in the Last Name field, change "Id" to "Last Name".
Also, I used MessageBox.Show instead of MsgBox. MsgBox is a leftover from VB6.
When you use a databound ListBox, the items aren't ListBoxItem objects (which you would expect to have Text and Value properties). Rather they are the type from the data source. The Items collection (and its variations, such as SelectedItems) is defined as a collection of objects, and the runtime type is obtained from the data source. Have you tried something like this?
For i = 0 To ListBox1.SelectedItems.Count
MsgBox(ListBox1.SelectedItems(i).ToString())
Next
In the comments, you indicated that the data source is a DB object containing String objects. If you have a collection of classes, such as Person, you can get it this way:
For i = 0 To ListBox1.SelectedItems.Count
MsgBox(DirectCast(ListBox1.SelectedItems(i), Person).FirstName)
Next
You should also get the same results with a For Each, as long as the selected items collection isn't changed while you are looping.
For Each p As Person in ListBox1.SelectedItems
MsgBox(p.FirstName)
Next
NOTE: This is untested code, as I'm not in front of Visual Studio at the moment.
EDIT: I see from the screenshot that the Value Member is set to a property named Id. If that is a uniqueidentifier column from the database, then the runtime type in the ListBox should be Guid.

ActiveX textbox value

How do I get the value of a textbox in Word?
I know in excel this is the right syntax: ActiveSheet.Shapes(x).Name.
I thought in word this would be the right syntax
ActiveDocument.Shapes(x).Name,
but this doesn't seems to work.
With this piece of code I also couldn't find a textbox:
For i = 1 To ActiveDocument.Shapes.Count
MsgBox ActiveDocument.Shapes(i).Name
Next i
To get the value of a standard textbox, use this:
ActiveDocument.Shapes(1).TextFrame.TextRange.Text
To get the value of ActiveX controls (OLEobjects), use this syntax where TextBox1 is the control name, use
ActiveDocument.TextBox1.Value
To get the name of ActiveX controls, use this:
ActiveDocument.InlineShapes(1).OLEFormat.Object.Name
I used tags (object properties -> assign a tag name) to edit the object's value. Use this syntax to change the value of a content control:
ActiveDocument.SelectContentControlsByTag("Your-Content-Control-Tag").Item(1).Range.Text = "your-preferred-value"
Anyway, thanks Rachel Hettinger for your patience ;).

How would one detect a property of type "InvalidOperationException" within a collection?

Consider the following vb.net code for an office add-in (for access):
td = db.TableDefs(objectName)
For Each fld In td.Fields
For Each fldprp In fld.Properties
Debug.Print(fldprp.Value.ToString())
Next
Next
the variable "db" is a .net representation of the access vba return result from "Application.CurrentDB()". "td" is of type "DAO.TableDefClass".
This code throws an exception of type "InvalidOperationException" when the value of the fldprp.value property cannot be determined (in Visual Studio, it shows the value as {"Invalid Operation."} in the watch window). fldprp.name, however, is available.
There are only a few properties which this occurs on. I'd like to be able to loop through all the fld.properties and output the values, but ONLY if it is not an exception.
I am pretty sure why it is happening (certain properties are not available in this context). What I need to know is how to detect this at run-time so i can skip the property.
I can't seem to find a solution that will work. Help would be greatly appreciated.
Inside the body of the inner loop, put this at the top:
If TypeOf fldprp Is InvalidOperationException Then Continue
http://msdn.microsoft.com/en-us/library/0ec5kw18%28VS.80%29.aspx
I don't know your specific situation, but what I would suggest is using an explicit filter for the object types you want to include instead of filtering out the ones you don't want to include.