Hello all,

I'm taking my first BASIC programming class this year, and for a final project, I'm trying to make a book-keeping log.

What I want to have happen, is to open a list of books, highlight a book title, click the "check in" button and have the text change to green, or red if the "check out" button is clicked, but I'm having problems trying to change the color of the text.

If anyone could explain how to do so, it would be greatly appreciated.
Thanks in advance!

Dani AI

Generated

Brief summary and practical options for ’s bookkeeping idea. ’s pointer about using the RichTextBox’s selection-color capability is correct, but a few practical details are often missing: the exact control type (classic VB6 vs. WinForms VB.NET) changes the API, a zero-length caret needs an explicit selection before coloring, and RTF must be used when saving so colors persist. The snippets below show a safe WinForms (VB.NET) approach and outline what to do in VB6, plus alternatives that scale better than inline formatting.

WinForms (VB.NET) — example that selects the current line if nothing is selected, then applies green for check‑in:

Private Sub btnCheckIn_Click(sender As Object, e As EventArgs) Handles btnCheckIn.Click
    Dim rtb = RichTextBox1
    If rtb.SelectionLength = 0 Then
        Dim lineIdx = rtb.GetLineFromCharIndex(rtb.SelectionStart)
        Dim startPos = rtb.GetFirstCharIndexFromLine(lineIdx)
        Dim length = rtb.Lines(lineIdx).Length
        rtb.Select(startPos, length)
    End If
    rtb.SelectionColor = Color.Green
    rtb.SelectionLength = 0   ' collapse selection if desired
    ' Persist formatting with: rtb.SaveFile("books.rtf", RichTextBoxStreamType.RichText)
End Sub

Classic VB (VB6) — key points. The same idea applies there but with the control’s SelStart/SelLength and SelColor properties: set SelStart/SelLength to cover the title, set SelColor (or use RGB for custom colors), then save as RTF to preserve formatting. If the project mixes many updates, repeatedly manipulating selection can be awkward; wrap selection changes so the previous caret/selection is restored after formatting.

Better alternative for a data-oriented log. For reliable state, store each book’s status (checked in/out) in a data structure or file and render the list with a ListView/DataGridView (apply item forecolor based on status) rather than embedding semantic state in RichTextBox formatting. Troubleshooting tips: saving as plain text strips colors, copy/paste can carry or lose formatting unexpectedly, and printing/output may not match on-screen colors unless RTF/formatting is preserved.

Use the SelColor method.
Example code:

Private Sub cmdCheckIn_Click()
Me.RichTextBox1.SelColor = vbRed
End Sub

A further explanation (and example) is in the VB help file. Wonderful thing, that help file. You should browse it some time just for some interesting insights.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.