IndexOf Problem

C

Chris Mahoney

Hi

I am having difficulty with the IndexOf function. Here's what I'm doing:

With TextBox1
.Text &= "Hello Everyone" & vbCrLf
.Text &= "More Stuff" & vbCrLf
.Text &= "Total: " & myTotal
Dim TotalIndex As Integer
TotalIndex = .Text.IndexOf("Total")
End With

The problem I'm having is that the TotalIndex value ends up wrong. If I
place a breakpoint on the last line and step through, then the TotalIndex
variable gets set correctly, but if I remove the breakpoint and run straight
through then the TotalIndex value is wrong.

I've never seen anything like this, where stepping through the code returns
different values from running straight through. It looks like this Should
Not Happen, but it does anyway.

Any ideas?

Thanks
Chris
 
O

One Handed Man \( OHM - Terry Burns \)

When u say its wrong at run time, are you aware that after this loop that
TotalIndex has gone out of scope ?, I put the dim before the loop and used a
messagebox to display the totalIndex value and ran it, it works OK.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
C

Cor Ligthert

Chris,

Simple trick to test

With TextBox1
.Text &= "Hello Everyone" & vbCrLf
.Text &= "More Stuff" & vbCrLf
.Text &= "Total: " & "1000"
Dim TotalIndex As Integer
TotalIndex = .Text.IndexOf("Total")
MessageBox.Show(TotalIndex.ToString)
End With

It gives for me forever 36

I hope this helps?

Cor
 
C

Chris Mahoney

Hi

Maybe I should post the entire block of code, rather than the simplified
stuff.

Dim ReportForm As New ReportViewer
Dim Task As DataRow
Dim TotalTime As TimeSpan
Dim PreviousDate As Date
Adapter.SelectCommand = New OleDb.OleDbCommand("SELECT * FROM Tasks ORDER BY
Date, StartTime;", DBConn)
Adapter.Fill(DS, "Tasks")
With ReportForm.ReportText
For Each Task In DS.Tables("Tasks").Rows
Dim StartTime As Date = Task("StartTime")
Dim FinishTime As Date = Task("FinishTime")
Dim TaskDate As Date = Task("Date")
TotalTime = TotalTime.Add(FinishTime.Subtract(StartTime))
If TaskDate <> PreviousDate Then .Text &= TaskDate.ToShortDateString
& vbCrLf
.Text &= StartTime.ToShortTimeString & "-" &
FinishTime.ToShortTimeString & vbCrLf
PreviousDate = TaskDate
Next
.Text &= "Total: " & Strings.Left(TotalTime.ToString,
Len(TotalTime.ToString) - 3)
Dim SelStart As Integer
SelStart = .Text.IndexOf("Total") - 9
.Select(SelStart, Len(.Text) - SelStart + 1)
.SelectionFont = New Font("Verdana", 12, FontStyle.Bold)
End With
ReportForm.ShowDialog()
ReportForm = Nothing
DS.Clear()

ReportViewer is a form containing a RichTextBox called ReportText.
Everything else comes from a database. What I'm trying to do is highlight
the last line of the RichTextBox, which starts with 'Total:'.

Chris
 
O

One Handed Man \( OHM - Terry Burns \)

Ill take a look at this later today, im off to work right now.

Regards

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
M

Microsoft

Actually, I did find time before I left. I think u were missing a focus on
the control. Here is a narrowed down example.

ReportText.Focus()

With ReportText

..Text = ""

..AppendText("Now is the time for all good men to come to the aid of the
party" & vbCrLf)

..AppendText("Total : This is my total line" & vbCrLf)

..AppendText("Off all the fishes in the sea" & vbCrLf)

Dim SelStart As Integer

SelStart = .Text.IndexOf("Total")

..Select(SelStart, 5)

..SelectionFont = New Font("Verdana", 12, FontStyle.Bold)

End With
 
C

Chris Mahoney

Aha!

I changed my .Text &= "Whatever" to .AppendText("Whatever") and it works now
:)

Thanks!

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top