Broken selections in text box

05-27-2006, 04:18 AM
Alright, I have this program I'm working on that outputs a set of numerical data in table format to a multiline text box in a fixed width font. The data starts as an array of decimals which gets concatenated into a single string with spaces and linebreaks, and that string gets spat out into the text box.

I want this program to be able to automatically hilight (presumably by selecting) the highest value or values in each row.

What exactly would be the best way to go about it? Can you make broken selections programatically, if at all? Should I rework it so that it outputs to XML or excel or something? Could I put a paint box underneath the text box and just paint the necessary hilights onto that or something?

I'm not even really sure where to begin on this one. Any suggestions at all are greatly appreciated.

05-30-2006, 08:40 PM
You probably should use a richTextBox as it can do much of what you want. Here's an example with integers.

Public Class Form1

Dim rtb As RichTextBox
Dim randomgenerator As Random
Dim numbers() As String
Dim nl As String

Sub New()

' This call is required by the Windows Form Designer.

' Add any initialization after the InitializeComponent() call.
rtb = New RichTextBox
rtb.Dock = DockStyle.Fill
nl = Environment.NewLine
randomgenerator = New Random
numbers = New String(99) {}
For i As Integer = 0 To 90 Step 10
For j As Integer = 0 To 8
numbers(i + j) = randomgenerator.Next(0, 10000).ToString
rtb.Text &= numbers(i + j) & " "
numbers(i + 9) = nl
rtb.Text &= nl

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub FillRTB()

' read one line of text at a time
Dim lineStart As Integer = 0
For Each line As String In rtb.Lines
If line = "" Then Continue For
Dim biggest As String = FindBiggest(line)
Dim index As Integer = rtb.Find(biggest, lineStart, RichTextBoxFinds.None)
If index > -1 Then
rtb.Select(index, biggest.Length)
rtb.SelectionColor = Color.Red
End If
lineStart += line.Length + 1 ' not sure why +1, but without it the lineStart goes astray.
rtb.Select(0, 0)

End Sub

Function FindBiggest(ByVal line As String) As String
Dim s() As String = line.Split(" "c)
Dim biggest As Integer
For Each num As String In s
Dim value As Integer
If Integer.TryParse(num, value) Then
If value > biggest Then biggest = value
End If
Return biggest.ToString
End Function

End Class

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum