How to Programmatically add or remove items in ListBox FORM control - openoffice.org

I am having a problem with OpenOffice.org ListBox Form control.
I have built a small form (not dialog) that contains a textbox and ListBox and 2 buttons.
Sub AddToList_ButtonClicked()
Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
oThisDoc = thisComponent.getDrawPage()
oForms = oThisDoc.getForms()
oForm = oForms.getByName("SimpleForm")
Dim oTextBox As Object
Dim oListBox As Object
oListBox = oForm.getByName("simpleListBox")
oTextBox = oForm.getByName("simpleTextBox").Text
oListBox.stringitemlist() = Array("One", "Two", "Three") '<--- Only possible way to add items to the ListBox Form Control :(
End Sub
Sub RemoveFromList_ButtonClicked()
Dim oThisDoc As Object
Dim oForms as Object
Dim oForm as Object
oThisDoc = thisComponent.getDrawPage()
oForms = oThisDoc.getForms()
oForm = oForms.getByName("SimpleForm")
Dim oListBox As Object
oListBox = oForm.getByName("simpleListBox")
oListBox.stringitemlist() '<--- contains array of items
oListBox.SelectedItems '<--- contains items selected for removal
End Sub
I would totally appreciate any solution for this problem!.

Is this what you are looking for?
' Add items.
oListBox.StringItemList() = Array("One", "Two", "Three")
oListBox.insertItemText(oListBox.ItemCount, "Four") ' This works even if oListBox starts out empty.
oListBox.insertItemText(oListBox.ItemCount, "Five")
' Remove the last item in the list.
oListBox.removeItem(oListBox.ItemCount - 1)
XrayTool shows that oListBox implements XItemList.
The form I used to test this code was in Writer, without any connection to Base.

Related

Updated ComplexObject values AfterSaveEntities: missing from server to client

I am calculating some values in the AfterSaveEntities method on the server using Breeze EFContextProvider. I am doing this for several different entity types.
After I calculate the new values, I save them to the datastore (using a new datacontext as specified in other SO questions).
I also update the entities in the saveMap with these new scalar values. These values are just integer properties that make up a ComplexObject that is a property of the entity being saved.
Everything works great. I can see just before my SaveChanges API function returns to client, that the properties are my ComplexObject are all correct.
Now, for most of my entity types, when the saveResult hits the client, everything is as expected. EXCEPT...for one of my entity types.
For this problem entity, the properties of the ComplexObject are not getting updated. If I look at the client-side complexAspect.originalValues property of the ComplexObject, the originalValues are the values I'm expecting to be the actual values the object...the same values that it had just before it left the server in the SaveChanges result.
I'm wondering if the answer is with adjusting entityInfo.OriginalValuesMap in the AfterSaveEntities function. But it didn't seem to make a different. I wonder if working with Complex Objects requires a different syntax when adjusting the OriginalValuesMap.
EDIT: I discovered that this unexpected behavior happens only when I create a new EntityInfo and add it to the SaveMap Dictionary in AfterSaveEntities.
The new entities that I create via CreateEntityInfo(entity, Breeze.ContexProvider.EntityState.Unchanged) are added to the saveMap with proper Type as the key.
Ironically, these extra entities all have the properties on their complex objects show up fine on client side. But as soon as I add more entities to the saveMap is when my original entity (that is already in the saveMap) starts messing up.
I hope I made my problem clear enough.
Here's my solution for anyone else with this problem...
The problem goes back to having 2 datacontexts...I needed a 2nd one to load related entities to do calculations for business logic.
I was adding entities from my 2nd context back into the SaveMap...I fixed this by instead loading those entities from the original breeze context and saving THOSE into the SaveMap.
Protected Overrides Sub AfterSaveEntities(saveMap As Dictionary(Of Type, List(Of EntityInfo)), keyMappings As List(Of KeyMapping))
Dim context2 = New Data.Db(EntityConnection)
context2.Configuration.ProxyCreationEnabled = False
context2.Configuration.LazyLoadingEnabled = False
Dim newEntitiesMap As New Dictionary(Of Type, EntityInfo)
For Each entType In saveMap
Select Case entType.Key.Name
Case "Registration"
For Each ent In entType.Value
Dim registration = DirectCast(ent.Entity, Registration)
registration.Stats = GenerateRegistrationStats(context2, registration.Id)
Next
Case "ConferenceRoom"
For Each ent In entType.Value
Dim conferenceRoom = DirectCast(ent.Entity, ConferenceRoom)
conferenceRoom.Stats = GenerateConferenceRoomStats(context2, conferenceRoom.Id)
Next
Case "Reservation"
For Each ent In entType.Value
Dim reservation = DirectCast(ent.Entity, Reservation)
reservation.Stats = GenerateReservationStats(context2, reservation.Id)
''Update ConferenceRoom stats
Dim confRoom = Me.Context.ConferenceRooms.Find(reservation.ConferenceRoomId)
confRoom.Stats = GenerateConferenceRoomStats(context2, confRoom.Id)
AddToSaveMap(newEntitiesMap, confRoom, GetType(ConferenceRoom))
''Update Registration stats
Dim registration = Me.Context.Registrations.Find(reservation.RegistrationId)
registration.Stats = GenerateRegistrationStats(context2, registration.Id)
AddToSaveMap(newEntitiesMap, registration, GetType(Registration))
Next
Case "Registree" 'update
For Each ent In entType.Value
Dim registree = DirectCast(ent.Entity, Registree)
Dim registration = Me.Context.Registrations.Find(registree.RegistrationId)
registration.Stats = GenerateRegistrationStats(context2, registration.Id)
AddToSaveMap(newEntitiesMap, registration, GetType(Registration))
Next
End Select
Next
context2.SaveChanges()
For Each ent In newEntitiesMap
If saveMap.ContainsKey(ent.Key) Then
'add to existing list
saveMap(ent.Key).Add(ent.Value)
Else
Dim list As New List(Of EntityInfo) From {ent.Value}
saveMap.Add(ent.Key, list)
End If
Next
End Sub
Private Sub AddToSaveMap(AddToSaveMap As Dictionary(Of Type, EntityInfo), entity As Object, classType As Type)
Dim entInfo = Me.CreateEntityInfo(entity, Breeze.ContextProvider.EntityState.Unchanged)
AddToSaveMap.Add(classType, entInfo)
End Sub
Private Function GenerateConferenceRoomStats(context As Data.Db, conferenceRoomId As Integer) As ConfRoomStats
Dim confRoom = context.ConferenceRooms.Where(Function(x) x.Id = conferenceRoomId) _
.Include(Function(x) x.Reservations.Select(Function(r) r.Occupants)).FirstOrDefault
confRoom.Stats = confRoom.GenerateStats
Return confRoom.Stats
End Function
Private Function GenerateReservationStats(context As Data.Db, reservationId As Integer) As ReservationStats
Dim reservation = context.Reservations.Where(Function(x) x.Id = reservationId) _
.Include(Function(x) x.Occupants).FirstOrDefault
reservation.Stats = reservation.GenerateStats
Return reservation.Stats
End Function
Private Function GenerateRegistrationStats(context As Data.Db, registrationId As Integer) As RegStats
Dim registration = context.Registrations.Where(Function(x) x.Id = registrationId) _
.Include(Function(x) x.Registrees.Select(Function(r) r.Person)) _
.Include(Function(x) x.Estimates) _
.Include(Function(x) x.Reservations.Select(Function(r) r.ConferenceRoom)) _
.Include(Function(x) x.Reservations.Select(Function(r) r.Occupants)).FirstOrDefault
registration.Stats = registration.GenerateStats
Return registration.Stats
End Function

How can I create multiple new instances of a model and save them to the database?

I'm using the following code to attempt to import a CSV file. However it is just saving the last object of Fact rather than saving each of the objects that were built.
Do While Not sr.EndOfStream
Dim aFact as Fact
Dim mArray = sr.ReadLine().Split(",")
aFact.Name = mArray(0)
aFact.Value = mArray(1)
db.Facts.Add(aFact)
End
db.SaveChanges()
Just use a list where you save the object
Dim factList As New List(Of Fact) ' add the list
Do While Not sr.EndOfStream
Dim aFact as Fact
Dim mArray = sr.ReadLine().Split(",")
aFact.Name = mArray(0)
aFact.Value = mArray(1)
factList.Add(aFact) ' put fact object in list
End

Drop-down list not populating other controls

I am a complete newb to webapps but here goes.
I have web form with a drop-down list that populates a list of holiday resorts from a database procedure. That bit works fine. When I select an item from the list I need to populate a listbox with hotels specific to that resort.
This bit I am having trouble with, the list does populate if I click off the drop-down list onto a calendar control on the form.
Question: how do I get it to populate the list after I clcik on the value from the drop-down list?
Thanks
Here is the code by the way:
Private Sub Page_Init(sender As Object, e As System.EventArgs) Handles Me.Init
Me.Calendar1.SelectedDate = Now()
Me.DropDownList1.Items.Clear()
Dim connStr As String = Web.Configuration.WebConfigurationManager.ConnectionStrings("ITC").ConnectionString
Dim conn As New SqlClient.SqlConnection(connStr)
conn.Open()
Dim sqlProducts As String = "<<sql_string>>"
Dim da As New SqlDataAdapter(sqlProducts, conn)
Dim ds As New DataSet()
da.Fill(ds, "Products")
DropDownList1.DataTextField = "Rcode"
DropDownList1.DataValueField = "Rcode"
DropDownList1.DataSource = ds.Tables("Products")
DropDownList1.DataBind()
ds.Dispose()
da.Dispose()
conn.Close()
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
Me.ListBox1.Items.Clear()
Me.ListBox2.Items.Clear()
Dim connStr As String = WebConfigurationManager.ConnectionStrings("ITC").ConnectionString
Dim conn As New SqlConnection(connStr)
conn.Open()
Dim sqlProducts As String = "sql_string '" & Me.DropDownList1.Text & "'"
Dim da As New SqlDataAdapter(sqlProducts, conn)
Dim ds As New DataSet()
da.Fill(ds, "Products")
ListBox1.DataTextField = "accommDescription"
ListBox1.DataValueField = "accommCode"
ListBox1.DataSource = ds.Tables("Products")
ListBox1.DataBind()
ds.Dispose()
da.Dispose()
conn.Close()
ListBox1.Focus()
End Sub

Creating a List (of String) giving error" Object reference not set to an instance of an object. "

MVC 3 razor VB.NET project. I have resorted to manual building a list for a drop down box so I can insure certain values are available in the select list and also to control what the first item is in the list. The below is my code snippet for the part that is giving me problems..
Dim _courses1 As Integer = db.courses.ToList.Where(Function(r) r.course_day = "Tuesday").Count
Dim _classes1 As List(Of cours) = db.courses.ToList
Dim classRef1 As List(Of String)
If Not _selectedClass0 = "--" Then
classRef1.Add("--")
Else
classRef1.Add(_selectedClass0)
End If
For i As Integer = 0 To _courses1 - 1
For Each item In _classes1.Where(Function(f) f.course_day = "Tuesday")
Dim _item As cours = item
classRef1.Add(_item.course_ref)
Next
Next
ViewBag.tue1 = classRef1
The _selectedClass0 is just a string that gets set earlier... The error mentioned happens when it gets to the ClassRef1.Add(_selectedClass0) part of the else statement. Which _selectedClass0 string value at the time of error is "--". I have a feeling it is in how the list is being created but I am not certain... Any ideas???
You're not initializing classRef1.
Dim classRef1 As new List(Of String)
Another thing I see is in the first line - I've made the changes I see:
Dim _courses1 As Integer = db.courses.Where(Function(r) r.course_day = "Tuesday").Count()
You don't need ToList at the beginning if all your getting is the count.
You are declaring classRef1 to be a list of strings here:
Dim classRef1 As List(Of String)
But you're never actually creating an instance using New. I'm not sure about the VB syntax, as I'm a C# developer, but I'd guess you should add the following line right after the declaration:
classRef1 = New List(Of String)

Sorting or Ordering values returned in a EF query MVC 3 vb.net app

I am using a where comparer in the below snippet from my function.. I need to order or sort the returned items by one of the columns... I tried using .OrderBy(function(f) f.regDate) but that dont work at all...
The part of the function in question looks like this:
Function ClassFiller() As ActionResult
Dim _courses As List(Of cours) = db.courses.ToList
Dim _registrants As List(Of reg_info) = db.reg_info.ToList
Dim _classSpec As List(Of classrm) = db.classrms.ToList
Dim _CurrRegistrants As List(Of reg_classes) = db.reg_classes.ToList
For Each Course In _courses.Where(Function(a) a.course_day = "Tuesday")
Dim _CurrCourse As cours = Course
Dim _classRoom As classrm = db.classrms.Where(Function(b) b.Course_ID = _CurrCourse.course_ref).FirstOrDefault()
Dim _classmax As Integer = _classRoom.ClassMax - 1
For Each reg In _registrants.Where(Function(d) d.tues_class1 = _CurrCourse.course_ref).OrderBy(Function(f) f.reg_date)
Dim _ClassCount As Integer = db.reg_classes.Where(Function(c) c.tues_class = _CurrCourse.course_ref).Count
I need to have the _registrants ordered or sorted by a value that is in the db.reg_info under the reg_date column... Any ideas??
Where _registrant is declared at I should have used the following instead:
Dim _registrants As List(Of reg_info) = db.reg_info.OrderBy(Function(t) t.reg_date).ToList
then it will already be ordered.. So the orderby is not needed in the For Each reg In _Registrants.

Resources