Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

How do I select all the items in a ListBox with a CheckBox and then display the Strings associate with each item in the ListBox on separate lines in bold in a RichTextBox?
Posted
Comments
OriginalGriff 16-Aug-11 3:59am    
Sorry? That doesn't make complete sense. You can't put CheckBoxes inside list boxes, so where does that come into it?
Please use the "Improve question" widget to edit your question and provide better information.
DaveAuld 16-Aug-11 4:20am    
No, but you can use a CheckedListBox :-)
OriginalGriff 16-Aug-11 4:26am    
Coffee, I need more coffee. :Doh!:
Terence J 16-Aug-11 4:42am    
I do not want to put a CheckBox inside a ListBox. The ListBox is on Form. When I check the CheckBox all the items of the ListBox must be selected and the strings associated with the ListBox must be displayed in the RichTextBox on seperate lines in bold font.

Use The Fallowing Coding



VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '  String 1
        Dim Position As Int16 = 0
        Dim string1 As String = "This is string1. Font=Arial: FontSize=14: Color=Blue" & vbNewLine
        Dim myFont As New Font("Arial", 14, FontStyle.Regular, GraphicsUnit.Point)
        Dim myColor As Color = Color.Blue
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1
        Position += string1.Length

        '  String 2
        string1 = "This is string2. Font=microsoft sans serif: FontSize=10: Color=Red" & vbNewLine
        myFont = New Font("microsoft sans serif", 10, FontStyle.Regular, GraphicsUnit.Point)
        myColor = Color.Red
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1
        Position += string1.Length

        '  String 3
        string1 = "This is string3. Font=courier new: FontSize=30: Color=Green: BOLD" & vbNewLine
        myFont = New Font("courier new", 30, FontStyle.Bold, GraphicsUnit.Point)
        myColor = Color.Green
        rtb.Select(Position, 0)
        rtb.SelectionFont = myFont
        rtb.SelectionColor = myColor
        rtb.SelectedText = string1
        Position += string1.Length
    End Sub
End Class
 
Share this answer
 
See this demo i knocked up.
Add a CheckedListBox and RichTextBox to a form

The load event will add 20 items to a list box, it will also add the same 20 items to the RTB.
When you select an item in the listbox, it will highlight in the RTB, you can toggle the BOLD state of the item by changing the checked status.

VB
Public class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Add 20 items to list box and Add the 20 items to the Rich Text Box
    For x as Integer = 1 To 20
      CheckedListBox1.Items.Add("Item" + x.ToString)
      RichTextBox1.Text = RichTextBox1.Text + "Item" + x.ToString
    Next
End Sub

Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
    'Get the Current Selected Item
    Dim item as String = CheckedListBox1.SelectedItem

    If item.Length > 0 Then
      'Find the item selected in the RTB
      Dim index As Integer = RichTextBox1.Text.IndexOf(item)
      If index >=0 Then
        'item found
        RichTextBox1.SelectionStart = index
        RichTextBox1.SelectionLength = item.Length

        'Check the checked box state for the item selected and adjust font style
        If CheckedListBox1.checkedIndices.Contains(CheckedListBox1.SelectedIndices(0)) Then
          'take current font and make it bold
          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily,  RichTextBox1.SelectionFont.FontFamily,  FontStyle.Bold)
        Else
          'take the current font and make it regular
          RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.FontFamily,  RichTextBox1.SelectionFont.FontFamily,  FontStyle.Regular)
        End If
      End If
    End If
End Sub
End Class
 
Share this answer
 
v2
"I do not want to put a CheckBox inside a ListBox. The ListBox is on Form. When I check the CheckBox all the items of the ListBox must be selected and the strings associated with the ListBox must be displayed in the RichTextBox on seperate lines in bold font."

Ah!
Selection is easy: Handle the CheckBox.Changed event:
VB
If myCheckBox.Checked Then
    For i As Integer = 0 To myListBox.Items.Count - 1
        myListBox.SetSelected(i, True)
    Next
End If

The RTF display is also simple:
VB
Dim sb As New StringBuilder()
sb.Append("{\rtf1\ansi ")
For Each i As Integer In myListBox.SelectedIndices
	sb.Append("\b ")
	sb.Append(myListBox.Items(i).ToString())
	sb.Append("\b0 ")
	sb.Append("\par")
Next
sb.Append("}")
myRichTextBox.Rtf = sb.ToString()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900