Pages

Friday, March 8, 2013

Try the RichTextBox control as a colorful alternative to Windows' data objects

The DataGrid and ListView are usually the display mechanisms of choice for presenting data to users. However, one control you may not have considered is the RichTextBox control. This control allows you to easily customize all or part of its contents. The function shown below accepts a DataSet object and RichTextBox control as arguments and outputs the DataSet data to the control. Of course, you may have to tweak the strings a bit for appearances. But if your goal is just a quick glance at the data; it will certainly do the job. To implement this feature in the companion article, “Fully automate Access imports with a blend of old and new data objects,” replace its ShowDataInRTB() method with this one.

Sub ShowDataInRTB(ByVal ds As DataSet, ByVal rtb As RichTextBox)
rtb.Clear()
Dim dc As DataColumn
Dim dr As DataRow
Dim tmp As System.Text.StringBuilder
Dim tmp1 As New System.Text.StringBuilder
Dim tmp2 As New System.Text.StringBuilder
Dim i As Integer = 0
For Each dc In ds.Tables(0).Columns
   tmp1.Append("{" & i & ",-30} ")
   tmp2.Append(dc.ColumnName & ",")
   i += 1
Next
rtb.SelectionColor = Color.DarkRed
rtb.AppendText(String.Format(tmp1.ToString, Split(

   tmp2.ToString, ",")) & vbCrLf)
tmp = New System.Text.StringBuilder
tmp.Append("-", 30 * (ds.Tables(0).Columns.Count + 1))
rtb.SelectionColor = Color.DarkRed
rtb.AppendText(tmp.ToString & vbCrLf)
For Each dr In ds.Tables(0).Rows
   Dim ia() As Object = dr.ItemArray
   rtb.SelectionColor = Color.DarkCyan
   rtb.AppendText(String.Format(tmp1.ToString, ia) & vbCrLf)
Next
rtb.SelectAll()
rtb.SelectionProtected = True
End Su



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.