Code not setting RichTextBox to a StringCollection item value - c#-to-vb.net

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

Related

WPF - Search for Incremental Numbered Controls using Visual Basic

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

WPF VB.NET : how to insert a notepad in listbox

I already used the code as below.
Dim openfile1 = New Microsoft.Win32.OpenFileDialog With {.Filter = "Text (*.Text)|*.txt"}
If (openfile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
For Each line As String In File.ReadAllLines(openfile1.FileName)
ListBox1.Items.Add(line)
Next
End If
This code serves on windows form, when I use in WPF there are no errors but can not display the contents of the notepad on the listbox. there is no other solution
File.IO.ReadAllLines will return an array of strings and not a collection. You're better off using a For loop.
Dim openfile1 = New Microsoft.Win32.OpenFileDialog With {.Filter = "Text (*.Text)|*.txt"}
If (openfile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
For i = 0 To Ubound(File.ReadAllLines(openfile1.FileName),1)
ListBox1.Items.Add(File.ReadAllLines(openfile1.FileName)(i))
Next
End If
Foreach statement = You need a collection object

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

how to observe changes in an array in vb .net

i have an array A, i just want to monitor the changes in that array, return the changed position of that array.
myOldTextBox = myTextBox
myTextBox = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
Dim i As Integer
For i = 0 To myTextBox.Length - 1
If myTextBox(i).Text <> myOldTextBox(i).Text Then
Dim fs As Integer
fs = farray.Length
farray(fs) = i
End If
Next i
i am newbie in vb .net. Thank you.
I don't believe you can do that.
For a regular variable I would suggest using get/set.
For an array, I would suggest creating a method to update the values, instead of setting the values directly (you can enforce this by making the array private and only giving it access through a get and set method).
In that method you can then do anything you want.
Pseudo code:
private _array
Public Function GetArray(ByVal key As String) As String
return _array(key)
End Function
Public Function SetArray(ByVal key As String, ByVal val As String) as String
_array(key) = val
return val;
End Function
add a listbox, and whenever the if condition is satisfied, add content to the listbox
myTextBox = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
Dim i As Integer
For i = 0 To myTextBox.Length - 1
If myTextBox(i).Text <> myOldTextBox(i).Text Then
Dim fs As Integer
fs = farray.Length
farray(fs) = i
Listbox1.items.add(Format(now,"yyyy-MM-dd hh:mm:ss") & _
" array number changed: " & i
End If
Next i
EDITED: (Didn't notice you were doing what I posted)
I would change the For loop to a Do While loop, because I have found issues when comparing substring in a For Loop, try it and see if that doesn't fix your problem...
(I've even tried Microsoft code that failed in for Loops w/ substrings)
Also, I'd HIGHLY suggest you use a MessageBox.Show(sMsg) or Debug.WriteLine(sMsg) to ENSURE that data is correct...

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