By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

IndexOf Problem

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

"Chris Mahoney" <ch***@nzweb.net> wrote in message
news:BD340875.7D8%ch***@nzweb.net...
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

Nov 20 '05 #2

P: n/a
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
Nov 20 '05 #3

P: n/a
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


On 2/8/04 6:56 PM, in article eb**************@TK2MSFTNGP09.phx.gbl, "One
Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote:
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.


Nov 20 '05 #4

P: n/a
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

"Chris Mahoney" <ch***@nzweb.net> wrote in message
news:BD36706A.B8E%ch***@nzweb.net...
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


On 2/8/04 6:56 PM, in article eb**************@TK2MSFTNGP09.phx.gbl, "One
Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote:
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.

Nov 20 '05 #5

P: n/a
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

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:Oo*************@tk2msftngp13.phx.gbl...
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

"Chris Mahoney" <ch***@nzweb.net> wrote in message
news:BD36706A.B8E%ch***@nzweb.net...
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


On 2/8/04 6:56 PM, in article eb**************@TK2MSFTNGP09.phx.gbl, "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote:
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.


Nov 20 '05 #6

P: n/a
Aha!

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

Thanks!

Chris

On 4/8/04 8:09 PM, in article Oh**************@TK2MSFTNGP09.phx.gbl,
"Microsoft" <me@me.com> wrote:
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

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:Oo*************@tk2msftngp13.phx.gbl...
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

"Chris Mahoney" <ch***@nzweb.net> wrote in message
news:BD36706A.B8E%ch***@nzweb.net...
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


On 2/8/04 6:56 PM, in article eb**************@TK2MSFTNGP09.phx.gbl, "One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote:

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.




Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.