Update a data bound list box - wpf

I have a section of code that fires when a check box is checked/unchecked.
The check box is going to be used to narrow down a listbox that is data bound. One item in the data is a link to an png image which shows correctly when the application first initializes.
In the code below, I get a "Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception" exception every time.
Private Sub chkShowCurrent_Click(sender As Object, e As RoutedEventArgs) Handles chkShowCurrent.Click
Dim o As ObjectDataProvider
Dim xDoc = XDocument.Load(sFileName)
If chkShowCurrent.IsChecked = False Then
'MsgBox("false")
End If
If chkShowCurrent.IsChecked = True Then
'MsgBox("true")
Dim OnlyCurrent = From job In xDoc.Root.Descendants("Job")
Where job.Element("Status").Value = "a-current"
Order By job.Element("Status").Value, job.Element("Rush").Value, Convert.ToDateTime(job.Element("DateOut").Value)
Select job
lstJobs.ItemsSource = OnlyCurrent
End If
lstJobs.Items.Refresh()
o = FindResource("jobs")
o.Refresh()
End Sub
I had originally thought that the lstjobs.items.refresh was the issue, but the exception doesn't popup until after End Sub has been reached. I have tried adding a breakpoint to the code at the checkbox code and step through the code, but the answer still eludes me. Can anyone tell me where my error is?
**** Edit for more info below ****
Public Property Artwork() As String
Get
Return _Artwork
End Get
Set(ByVal value As String)
If value = String.Empty Then value = "images\Untitled.png"
If My.Computer.FileSystem.FileExists("\\ARTSTATION\Users\Public\XML Job Board\" & value) Then
value = "\\ARTSTATION\Users\Public\XML Job Board\" & value
Else
value = "\\ARTSTATION\Users\Public\XML Job Board\images\Untitled.png"
End If
_Artwork = value
End Set
The xml element is formated as image\GUID.png, thus necessitating the need to add the full path at runtime.
I had originally explored this as the issue and changed the XML to contain the full path and commented out the need for adding the full path in the properties, but this yielded the same results.

Related

Connecting and Updating to Database through VB.NET "Select command property has not been ... "

I am using this guide to update a value in a database through VB.NET and really struggling to get it to work.
I am trying to add 1 to a particular cell in the database. The grid that I am using updates with the value, but the database itself doesn't change. Using different tutorials (and loads of other StackOverflow questions) I have changed the code from the original but nothing seems to work.
Each time I click the button that should trigger the addition, I get this error message.
"The select command property has not been initialized before calling fill"
If anyone could tell me what is going wrong or point me in the direction that I should go in, it would be much appreciated. Thanks.
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim senderGrid = DirectCast(sender, DataGridView)
Dim OleDbDataAdapter As New OleDb.OleDbDataAdapter
If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso
e.RowIndex >= 0 Then
' If addition
If e.ColumnIndex = 3 Then
Try
'from microsoft tut
ProjectDatabaseDataSet.Tables(0).Rows(e.RowIndex).Item("ProductStock") = ProjectDatabaseDataSet.Tables(0).Rows(e.RowIndex).Item("ProductStock") + 1
OleDbDataAdapter.Fill(ProjectDatabaseDataSet.Product)
OleDbDataAdapter.Update(ProjectDatabaseDataSet.Product)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If

Event handling for an array of objects [duplicate]

I have a user form that displays line-by-line validation errors (in text box) that I want to supplement with user form labels that act as hyperlinks that users can click to go directly to the cell with issues.
I have code that builds labels on the fly and have added a click event through class modules but I cannot get it the click event in the class module to fire.
I did modify this code from working code that builds this type of label and click event on the fly, but that code loads labels at userform initiation and places each class object into a collection. I don't know if that is necessary to build into my solution, but I played with it and could not get it to work.
Here is my procedure to place label on the userform if needed. It runs inside another procedure if validation is needed. Userform is than shown, filled out with message (and this one label that gets created for now), if validation is needed.
Sub PlaceLinkLabel(SayWhat As String, WhichSheet As String, WhichRange As String)
Dim lblNew As MSForms.Label
Set lblNew = frmValidationMessage.Controls.Add(bstrProgID:="Forms.Label.1", Name:=SayWhat, Visible:=True)
With lblNew
With .Font
.Size = 10
.Name = "Comic Sans MS"
End With
.Caption = SayWhat
.Top = 55
.Height = 15
.Left = 465
.Width = 100
End With
Dim clsLabel As UserFormLabelLinks
Set clsLabel = New UserFormLabelLinks
Set clsLabel.lbl = lblNew
With clsLabel
.WhichRange = WhichRange
.WhichSheet = WhichSheet
End With
'not sure if this is needed or not
'Dim pLabels As Collection
'Set pLabels = New Collection
'pLabels.Add clsLabel
End Sub
Here is UserFormLabelLinks class module:
Option Explicit
Private WithEvents pLabel As MSForms.Label
Private sWhichRange As String
Private sWhichSheet As String
Public Property Set lbl(value As MSForms.Label)
Set pLabel = value
End Property
Public Property Get WhichSheet() As String
WhichSheet = sWhichSheet
End Property
Public Property Let WhichSheet(value As String)
sWhichSheet = value
End Property
Public Property Get WhichRange() As String
WhichRange = sWhichRange
End Property
Public Property Let WhichRange(value As String)
sWhichRange = value
End Property
Private Sub pLabel_Click()
MsgBox "hi" 'when i click label, this does not fire
'Application.Goto ThisWorkbook.Worksheets(WhichSheet).Range(WhichRange), True
'ActiveWorkbook.FollowHyperlink ("#" & WhichSheet & "!" & WhichRange)
End Sub
The MSForms.Label object is going out of scope as soon as PlaceLinkLabel exits, as does the UserFormLabelLinks object reference; thus you're creating a label, but it's a fire-and-forget thing that you can't programmatically access as soon as End Sub is reached, hence the events never fire.
You need a private field to hold on to the UserFormLabelLinks object reference (and thus keep the MSForms.Label reference around via the encapsulated pLabel field):
Option Explicit
Private clsLabel As UserFormLabelLinks
Then remove this line in the procedure:
Dim clsLabel As UserFormLabelLinks
In other words, promote that local variable to a field, to keep it around after the procedure has completed.
Another approach that worked:
Placing
Private pLabels As Collection atop the module where PlaceLinkLabel is stored
and using
If pLabels Is Nothing Then Set pLabels = New Collection
pLabels.Add clsLabel
at the end of PlaceLinkLabel module

WF: Workflow Designer: List of InArgument added dynamically : How to get value during workflow execution

I have built a workflow designer that allows to enter a list of email addresses.Each email in the list needs to be an InArgument(Of String) so they can be individually edited/added using variables.
On my Activity, I have a property that is declared like so:
Public Property [To] As ObservableCollection(Of InArgument(Of String))
My designer is wired up and populating this collection properly.
However during the execution, I do not know how to get the run-time value for each InArgument that was added.
When we are executing the workflow and iterating for each InArgument added to the list, I attempted to get the value as shown below but that fails:
For Each toAddress As InArgument(Of String) In Me.To.ToList()
Dim emailToAddress As String = toAddress.Get(_Context)
Next
The error we get is “The argument of type '<type>' cannot be used. Make sure that it is declared on an activity” and type is a string in my case...
The error we get sort of make sense because we haven’t declared a property on the activity since it was added dynamically to the list and therefore cannot get the value using the syntax shown below:
The_Property_Name.Get(_Context)
Can someone help? I can't seem to find anything. Should I be doing a different approach?
I figured it out so I will answer my own question! All we need to do is explicitly add the collection items to the metadata by overriding CacheMetadata() method on the activity. This then makes it available to the workflow context.
First we add them to the context:
Protected Overrides Sub CacheMetadata(ByVal metadata As CodeActivityMetadata)
MyBase.CacheMetadata(metadata)
Dim i As Integer = 0
For Each item As InArgument(Of String) In Me.To
Dim runTimeArg As RuntimeArgument = New RuntimeArgument("TO" & i.ToString(), item.ArgumentType, item.Direction, False)
metadata.Bind(item, runTimeArg)
metadata.AddArgument(runTimeArg)
i = i + 1
Next
End Sub
Then when executing, we get the values like this
For Each item As InArgument(Of String) In Me.To
Dim email As String = _Context.GetValue(item)
Next

Excel VBA: Dynamic range for ComboBox.Rowsource values not displayed when userForm called from commandbutton

The title should give a fair overview of the problem but I'm running a dynamic named range for use in a combo box in a userform. When I run the form, the values appear as intended. When I call a module sub-routine via a command button, the values don't appear and I've no idea why.
I'll paste all code and highlight the offending snippet(s) below:
Private Sub btnGetGAToken_Click()
'--------------------------------
'Obtain API Token from Google Analytics (GA), indicate to user that token has been obtained and populate Account combobox
'with a unique list of accounts, which will in turn populate the Profile combobox with the profiles associated with the chosen
'account
'--------------------------------
Dim txtEmailField As String
Dim txtPasswordField As String
'Values written to sheet for use in UDFToken and UDFGetGAAcctData array formulas
Range("FieldEmail").Value = Me.txtEmailField.Text
Range("FieldPassword").Value = Me.txtPasswordField.Text
Range("GAToken").Calculate
With Me.lblGATokenResponseField
.Caption = Range("GAToken").Value
.ForeColor = RGB(2, 80, 0)
End With
Call FindUniqueAccountNames
cboAccountNamesComboBox.RowSource = Sheet1.Range("ListUniqueAccountNames").Address
End Sub
Private Sub cboAccountNamesComboBox_Change()
'Value written to sheet for use in the 'ListProfileNames' dynamic, named range
Range("ChosenAccount").Value = Me.cboAccountNamesComboBox.Value
With Me.cboProfileNamesComboBox
.Value = ""
.RowSource = Sheets("CodeMetaData").Range("ListProfileNames").Address
End With
End Sub
The dynamic range was created using the name manager and is below:
Named Range: "ListUniqueAccountNames" =OFFSET(CodeMetaData!$J$5,0,0,COUNTA(CodeMetaData!$J$5:$J$5000))
and for ease of reference, the code I'm using to run it is below:
cboAccountNamesComboBox.RowSource = Sheets("CodeMetaData").Range("ListUniqueAccountNames").Address
The sub-routine calling the userform is here:
Public Sub ShowReportSpecsForm()
Load frmReportSpecs
frmReportSpecs.Show
End Sub
Forgive me for posting so much of the code, but I'm not sure exactly what it is that's causing the problem - I'm still very much a rookie with forms.
Any help will be greatly appreciated. Thanks.
If you are using the rowsource property and named ranges then I would suggest setting the rowsource property of the combobox's at design time. Then to debug where required use:
Debug.Print Range("ListUniqueAccountNames").Address
This will return the named range address to the immediate window where you can check it is correct.
Remember that the property Address from a named dynamic range returns a normal static address.
For example, Range("ListUniqueAccountNames").Address can returns $J$5:$J$20.
You do not need use a Excel address in RowSource property. You can use a Excel name.
Besides, when you show a Userform it is necessary refresh the RowSource property from a ComboBox or ListBox control in order to update its values. (Excel control does not watch if the range or data change)
That refresh can be made inside Activate event (it runs immediately before form Show and it is shown below) and any situation where data or range changes.
Private Sub UserForm_Activate()
Me.cboAccountNamesComboBox.RowSource = ""
Me.cboAccountNamesComboBox.RowSource = "ListUniqueAccountNames"
End Sub

Infragistics UltraWinGrid EmptyDataText Equivalent?

We're using Infragistics UltraWinGrid as a base class for customized controls. One of the projects that will use this control to display search results has a requirement to display a user friendly message when no matches are located.
We'd like to encapsulate that functionality into the derived control - so no customization beyond setting the message to display is required by the programmer who uses the control. This would have to be done in generic fashion - one size fits all datasets.
Is there allowance in the UltraWinGrid for this type of usage already? If so, where would I find it hidden. :-)
If this functionality needs to be coded, I can think of an algorithm which would add a blank record to whatever recordset was set and place that into the grid. In your opinion, is this the best way to handle the solution?
I don't know if this will help, but here's to finishing up the thread. I didn't find a built in way, so I solved this problem as follows: In my class which inherits UltraGrid
Public Class MyGridPlain
Inherits Infragistics.Win.UltraWinGrid.UltraGrid
I added two properties, one to specify what the developer wants to say in the empty data case, and another to enable the developer to place their message where they want it
Private mEmptyDataText As String = String.Empty
Private mEmptyDataTextLocation As Point = New Point(30, 30)Public Shadows Property EmptyDataTextLocation() As Point
Get
Return mEmptyDataTextLocation
End Get
Set(ByVal value As Point)
mEmptyDataTextLocation = value
setEmptyMessageIfRequired()
End Set
End Property
Public Shadows Property EmptyDataText() As String
Get
Return mEmptyDataText
End Get
Set(ByVal value As String)
mEmptyDataText = value
setEmptyMessageIfRequired()
End Set
End Property
I added a method which will check for empty data and set the message if so. And another method which will remove the existing empty message.
Private Sub setEmptyMessageIfRequired()
removeExistingEmptyData()
'if there are no rows, and if there is an EmptyDataText message, display it now.
If EmptyDataText.Length > 0 AndAlso Rows.Count = 0 Then
Dim lbl As Label = New Label(EmptyDataText)
lbl.Name = "EmptyDataLabel"
lbl.Size = New Size(Width, 25)
lbl.Location = EmptyDataTextLocation
ControlUIElement.Control.Controls.Add(lbl)
End If
End SubPrivate Sub removeExistingEmptyData()
'any previous empty data messages?
Dim lblempty() As Control = Controls.Find("EmptyDataLabel", True)
If lblempty.Length > 0 Then
Controls.Remove(lblempty(0))
End If
End Sub
Last - I added a check for empty data to the grid's InitializeLayout event.
Private Sub grid_InitializeLayout(ByVal sender As Object, _
ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) _
Handles MyBase.InitializeLayout
setEmptyMessageIfRequired()
End Sub

Resources