WPF application page doesn't update when I update the content of the labels - wpf

I'm writing a VB.Net WPF application which uses a frame on the MainWindow to show a page that contains multiple labels. I dynamically update those labels in my logic from the page but once those labels are updated, this doesn't reflect in the frame which shows the page.
This is the sub that updates the labels in Page2.xaml.vb:
Public Sub UpdateLabels(Name As String, microphonefinal As List(Of String))
Dim labelList As New List(Of Label)
For i As Integer = 0 To microphonefinal.Count - 1
For Each Label As Label In Page1Grid.Children
If Label.Name.Contains(Name) Then
Label.Content = microphonefinal(i)
Exit For
End If
Next
Next
End Sub
I call the above sub from MainWindow.xaml.vb using the following:
Dim form = New Page2
If microphonefinal.Count > 0 Then
form.UpdateLabels("Microphones", microphonefinal)
End If
I'm not sure if the issue is because I'm creating a new instance of Page2. But if I don't include this then I get the error: "Reference to a non-shared member requires an object reference".
I tried using frame.Refresh() to see if this would update the page with the new values in the labels but it doesn't. I've also used MsgBox to display the contents of the labels to make sure they've changed.
Am I doing something wrong? Or is there anything I'm missing? Please let me know if there's anymore information you need to help. Thank you!

I managed to access the sub UpdateLabels() of class Page2 by using the frame as you said. So to call the sub I coded it like this:
frame.Content.UpdateLabels("Microphones", microphonefinal)

Related

VB.NET Change visibility of multiple labels on runtime

im trying to get to change the visibility of different labels during runtime after a button click
Dim labelsEmpty As New ArrayList
Dim allTxt As New List(Of Control)
For Each txt As TextBox In FindControlRecursive(allTxt, Me, GetType(TextBox))
If txt.Text = "" Then
If txt.Name = "TextBox1" Then
Else
'Dim textBoxName = txt.Name.ToString.Remove("txt").ToLower
labelsEmpty.Add(txt.Name.ToString.Replace("txt", "lblMsg"))
End If
End If
Next
For Each label In labelsEmpty
Dim Label1 As New Label
Label1.Name = label
Label1.Text = "Insert a value"
Label1.Visible = True
Next
This is what i have, each textbox in my form has a label used to tell the user that there was some kind of error, in this case i need to get which textbox is left empty and set the visibility of its label to true (already hidden from form start), so what i do is i go through every textbox in my form to see which one the user left empty and then take their name (ex: txtAge) and replace txt with "lblMsg" and then insert them into an array.
Doing so i get an array with all the label names that should be set to visible = true
Now i need to set their visibility and text value, so what i did is i created a for each loop and getting every label in the array, but the code i used is not working, i already checked if there are items in the array and they are there, any help? Thanks.
My Form: https://i.stack.imgur.com/qrawz.png
In your final loop, you are creating new labels and never adding them to your form. If you already have existing labels on your form, you want to reference those:
For Each label In labelsEmpty
With Controls(label)
.Text = "Insert a value"
.Visible = True
End With
Next
Setting the Text property may be unnecessary if your labels already have the necessary text.
If you want to create new labels and then add them to your form, you need to add the control to the form after you have created it:
For Each label In labelsEmpty
Dim Label1 As New Label
With Label1
.Name = label
.Text = "Insert a value"
'.Top = 100
'.Left = 100
.Visible = True
End With
Controls.Add(Label1)
Next
If you do it this way, you will need some way to set the Top and Left properties so that the labels align correctly with your existing text boxes. There are several ways to do that but I'll leave that to you as I'm fairly sure that my first solution answers your question.

Vlookup in Excel userform is not working

I have a userform containing a combobox. Depending on the value of this combobox I want to fill a textbox in the same userform. I'm using the next code:
Private Sub ComboBox1_Afterupdate()
With Me
.TextBox4 = Application.WorksheetFunction.VLookup(Me.ComboBox1,
Sheets("Members").Range("B6:D15"), 2, 0)
End With
End Sub
Every time I try to run the userform I get a Runtime Error 1004 message.
What am I doing wrong?

Handling click events for dynamically created buttons in array

I have an array of buttons that are created through code. The reason why I've done this is because the amount of buttons could change, and in this case, I'm trying to create buttons that are displayed as days in a month on a calendar, and the amount can't be at a set amount because if the amount of buttons were being created for February, the amount would either be 28 or 29.
I've done this how I want to, however the problem comes at having to handle a click event for each button. Since I'm looking for the general idea how to handle a click event in the below example, I want to messagebox what the content is for the button.
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length
btns(ButtonCount) = New Button With {.Content = ButtonCount}
'Handler goes here.
Next
The way that I reference these buttons individually is through btns(ButtonCount), I do not name them.
So is there a way to add a click event to these buttons created in the example?
Use the AddHandler statement to assign an event handler to your button. In your event handler check which button was pressed.
First of all, take a look at the documentation as suggested in the comments.
(AddHandler Doc.)
Second, you could follow this example to achieve what you want to do:
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length - 1
btns(ButtonCount) = New Button With {.Content = ButtonCount}
AddHandler btns(ButtonCount).Click, AddressOf OnBtnClick
Next
Private Sub OnBtnClick(sender As Object, e As RoutedEventArgs)
'Your Event Handling
End Sub

Click Event not working anymore when using Helixtoolkit.SortingVisual3D

I want to add transparency to Objects (without loosing the Click-Event).
Google told me to try SortingVisual3D.
Without SortingVisual3D everything (except transparency) worked well, Click-Events also.
Now i tried to implement it (simplified code):
Public SV3d As New HelixToolkit.Wpf.SortingVisual3D
Public Model3DUI As New ModelUIElement3D
'Apply geometry
Model3DUI.Model = geometry 'skipped geometry code in this post
'Add Click Event
AddHandler Model3DUI.MouseLeftButtonUp, AddressOf ClickEvent
'Add to SortingVisual3D
SV3d.Children.Add(Model3DUI)
'Add to ViewPort
Viewport.Children.Add(SV3d)
'Setup SortingVisual3D
SV3d.SortingFrequency = 2
SV3d.Method = HelixToolkit.Wpf.SortingMethod.BoundingBoxCorners
SV3d.IsSorting = True
Basically it works fine, everything renders as it should and transparency is working too. But for some reason now the Click Event doesn't work.
Anybody has an idea what i'm doing wrong?
I'm not very experienced with Helixtoolkit, so it could be that my way is completely wrong.
First create SortingVisual3D, after that at the beginning add the visual clickable objects and at the end add the transparent object, with the hit test visible set to false ! This worked for me !

How to Populate Label with String Array on ComboBox Item Selection in VB.Net

I have an application in VB.Net where I'm trying to fill a label with some string data that I have in an array. I have a ComboBox which holds some states as the index collection/values. When I select a particular value in the combobox, I want to pull string data from the assigned array, and populate the label with it as a "clickable link a browser window. I'm lost on this, yet here is what I have in my code stub:
Private Sub cboSelectState_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboSelectState.SelectedIndexChanged
'Create a string array
Dim AlabamaCities() As String = {"http://www.rolltide.com/", "http://www.crimsontidehoops.com/", "http://centralalabamapride.org/"}
Dim strAlabama As String
'Populate label with the array data, on a particular value selection in combo box.
If cboSelectState.SelectedValue("Georgia") Then
strAlabama = CStr(AlabamaCities(3))
lblLinkOutput.Text = strAlabama
End If
End Sub
So when I pick Alabama in my combo box, I want the label to show:
http://www.rolltide.com
http://www.crimsontidehoops.com
http://centralalabamapride.org
The links will be clickable from the label and populate in the same tab whenever clicked. I haven't tried the clickable link part yet, and I will try once I get this down.
I know it's probably bad starting form out the gate. But I'm trying to get the form down to gain the knowledge and plan out a bigger project, and accomplish something better when I think of it. I appreciate your knowledge and assistance.
Firstly, it would make sense to use a Dictionary to store the data. The state names will be the keys and the values would be the arrays of URLs. You would then display the keys in the ComboBox and, when a selection is made, use the selected key to get the corresponding value from the Dictionary.
At that point, you won't be using a Label if you want clickable links. You should use a TableLayoutPanel as a container and then add one LinkLabel to the table for each URL in the array. You can then use a single handler for all the LinkClicked events.

Resources