WPF - Search for Incremental Numbered Controls using Visual Basic - wpf

First time using WPF coming from WinForm and have been adjusting fairly well however I am stumped on one procedure I used quite commonly. This was the code that I used in WinForm to autopopulate controls that were labeled like so; label1, label2, label3, etc..
Dim lbl As Label
Dim matcheslbl() As Control
For i As Integer = 1 To 24
matcheslbl = Me.Controls.Find("label" & i, True)
lbl = DirectCast(matcheslbl(0), Label)
If matcheslbl.Length > 0 AndAlso TypeOf matcheslbl(0) Is Label Then
lbl.Text = "Data Here"
End If
Next
How do I use the same procedure in WPF? My hierarchy layout in the form goes from WrapPanel > StackPanel > Canvas > Controls
After modifying the code to my knowledge I get hung up on the Me.Controls aspect and cant find anything after extensively searching, or im not fully understanding it. This is my modified code...
For i As Integer = 1 To 24
Dim lbl As Label
Dim matcheslbl() As Control
matcheslbl = Me.WrapPanel.FindName("lbl" & i)
lbl = DirectCast(matcheslbl(0), Label)
If matcheslbl.Length > 0 AndAlso TypeOf matcheslbl(0) Is Label Then
lbl.Content = "Data Here"
End If
Next
This hangs up here..
matcheslbl = Me.WrapPanel.FindName("lbl" & i)
Any help on how to accomplish my previous procedure in WPF and give a detailed description on how to achieve it since I am very new to WPF

If you look at the intellisense on Me.WrapPanel.FindName you will notice it does not return an array, just a single object. I fixed your code with the following
For i As Integer = 1 To 24
Dim o As Object = wpMain.FindName("Label" + i.ToString())
DirectCast(o, Label).Content = "Data Here"
Next

Related

Code not setting RichTextBox to a StringCollection item value

And I would like to share this issue with you.
This piece of code:
Dim np As New notepad
np.RichTextBox1.Text = My.Settings.SDBodies.ToString(ListBox1.SelectedIndex)
Is doing nothing. It is supposed to do what it is supposed to do. It is susposed to set the RichTextBox text to the StringCollection Item Value.
Yes, notepad and RichTextBox1 is defined.
RichTextBox1 is the RichTextBox I am talking about.
And notepad is the form RichTextBox1 is in.
Please help!
New code:
If My.Settings.SDBodies Is Nothing Then
My.Settings.SDBodies = New System.Collections.Specialized.StringCollection
End If
Dim np As New notepad
ListBox1.Enabled = False
For i = 0 To My.Settings.SDBodies.Count - 1
If i = ListBox1.SelectedIndex Then
np.RichTextBox1.Text = My.Settings.SavedDocuments(i)
ListBox1.Enabled = True
Else
' Do nothing
End If
Next

How to check a value of a listbox in visual studio 2015

I am using visual basic.
I have a list box full of around 10-12 values. I was curious how I would check the selected value.
All i'm trying to do is change an image source depending on the selected value in the listbox.
Below is the code I have so far:
' For Each item As ListItem In lstBox.Items
If item.Value = "My Value"
lblNumber.Content = "$123"
End If
Next
If I understand your question correctly, you could use the following code:
Private Sub Button_Click()
If ListBox.ListIndex <> -1 Then MsgBox ListBox.Value
End Sub
Please let me know if I do not understand, however.
EDIT: Is the below what you are asking for? This code should display the selected value within the listbox.
Dim I As Long
Dim msg As String
If ListBox1.ListIndex <> -1 Then
For I = 0 To ListBox1.ColumnCount - 1
msg = msg & ListBox1.Column(I) & vbCrLf
Next I
End If
MsgBox msg

Datagridviewcell Tooltip doesn't display after modifying image of any cell

In my winforms application, I loop through the cells of a datagridview and add a tooltip for specific cells based on values in other columns. I do this in the cellformatting event handler, and it worked perfectly. Here is the code:
Private Sub dgvResults_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvResults.CellFormatting
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "TradeInValue"
DirectCast(dgvResults.Rows(e.RowIndex).Cells("TradeInValue"), DataGridViewTextBoxCell).ToolTipText = "Min = " & CDec(dgvResults.Item("BB_Min", e.RowIndex).FormattedValue).ToString("$#####.##") & ", Max = " & CDec(dgvResults.Item("BB_Max", e.RowIndex).FormattedValue).ToString("$#####.##")
If Not IsNothing(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue) AndAlso dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString.Trim.Length > 0 AndAlso CInt(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString) <> -1 Then
If dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Length > 0 Then
Dim ValueParts() As String = dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Split("|")
'Dim selectedTrim As String = ValueParts(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue)
End If
End If
End Select
End Sub
Then, I added code in the cellpainting event handler to hide specific images, again based on values in the datagridview. Here is that code.
Private Sub dgvResults_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvResults.CellPainting
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "VIN_Pic"
If dgvResults.Rows(e.RowIndex).Cells("VIN_Value").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
Case "EmailDisplayImage"
If dgvResults.Rows(e.RowIndex).Cells("ListingContactEmail").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
End Select
End If
End Sub
When this code as added, the tooltips no longer display. The CellToolTipTextNeeded event fires, and it shows the correct text in the argument, but it never displays. Comment out the lines that assign a new image to the datagridviewimagecells, and the tooltips start displaying again.
I hope this explanation was sufficient. Any ideas?
With the below code, you will run into memory and GDI object leaks very easily as a new Bitmap instance is created every time the cell is painted. Also this is causing your ToolTip to be not displayed.
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
Define the empty bitmap in class level or add as embedded resource and use it.
Dim emptyBitmap As New Bitmap(1, 1);
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = emptyBitmap

Iterate through dynamically created controls wpf

I have been searching for this but I am not able to find anything related to how to iterate through all dynamically created controls on the grid. I'm basically only looking to grab data from a dynamically created textbox, I have tried the following but it is not pulling up any children
Dim ctl As FrameworkElement = Me
Dim children As Integer = VisualTreeHelper.GetChildrenCount(ctl)
For i = 0 To children - 1
Dim Child As FrameworkElement = VisualTreeHelper.GetChild(ctl, i)
Next
Code used to add a textbox onto the form
For i = 0 To tb - 1
Dim t As New TextBox
t.Width = 75
t.Height = 25
t.Focus()
Grid.SetColumn(t, columns)
MyGrid.Children.Add(t)
t.Focus()
Next

remove bulletstyle on WPF richtextbox selection

I've managed to applying bulleted list formatting possible, but how to remove it again?
How to detect if the selection is/contains a List?
Did I overcomplicate things? Is there a straightword way to convert a selection to a bulleted list and back?
Private Sub bullet(o As Windows.Forms.ContextMenuStrip, e As Windows.Forms.ToolStripItemClickedEventArgs)
Dim lst As New Windows.Documents.List()
lst.MarkerStyle = bullets(e.ClickedItem.Text)
If rtf.Selection.IsEmpty Then
lst.ListItems.Add(New Windows.Documents.ListItem())
Else
Dim li As Windows.Documents.ListItem
Dim lines() As String = rtf.Selection.Text.Split(vbCrLf)
For Each s As String In lines
li = New Windows.Documents.ListItem()
li.Blocks.Add(New Windows.Documents.Paragraph(New Windows.Documents.Run(s.Trim())))
lst.ListItems.Add(li)
Next
rtf.Selection.Text = ""
End If
Dim curCaret = rtf.CaretPosition
Dim curBlock = rtf.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
rtf.Document.Blocks.InsertAfter(curBlock, lst)
Dim vMove As Windows.Documents.TextPointer = Nothing
vMove = curCaret.GetNextInsertionPosition(Windows.Documents.LogicalDirection.Forward)
If vMove IsNot Nothing Then rtf.CaretPosition = vMove
rtf.Focus()
End Sub
I've since come to drop this code from my project because it's unreliable in certain situations. Would a solution based on dynamic XAML insertion be more reliable? Many aspects of WPF seem to very poorly conceived....
If you could use the windows forms version of richtextbox, you could use the SelectionBullet property.
http://msdn.microsoft.com/en-us/library/ms742875.aspx
Try EditingCommands.ToggleBullets.Execute(null, richTextBox) to toggle bullet style in selected paragraphs.

Resources