Click here to Skip to main content
12,998,759 members (72,492 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

I am getting following assertion on using the below statement in WPF Application,

"ArgumentOutOfRangeException was unhandled by user code"

In my Print.Xaml.cs

I have TextBox and need to pass the same to managed code(C++/CLI) from C#(.Net)

private UInt32 _ValueCntTextBox = 0;
private void UpdatePrintInfo()
line 1: int value = PrintInfoTextBlk.Text.IndexOf(':');
line 2: string mystring = PrintInfoTextBlk.Text.Substring(value +1,PrintInfoTextBlk.Text.IndexOf(')' )-value -1);
line 3: ValueCntTextBox.Text = value;
line 4: UInt32.Parse(value,out _ValueCntTextBox);

The above Exception is obtained @ line 2:
Any Help in resolving the above is much appreciable.

I am using Windows 7 O/S, VS2008 IDE
With Regards,
Posted 2-Apr-12 5:58am
Updated 2-Apr-12 6:04am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

What is the value of the parameters in the PrintInfoTextBlk.Text.Substring() call? For example what does value contain, and what does PrintInfoTextBlk.Text.IndexOf(')' ) return?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

From the code above, I suppose you are trying to extract the text portion between ':' and ')'.

I would code like:
// save text to avoid accessing underlying control multiple times
string sText = PrintInfoTextBlk.Text;
// save position of ':' if present; -1 otherwise!
int nStartIndex = sText.IndexOf(':');
// save position of ')' if present; -1 otherwise!
int nEndIndex = sText.IndexOf(')');
string mystring = null; // null will later tell that no valid chars have been found
// Both chars were found, so extract substring
if ((nStartIndex >= 0) && (nEndIndex > nStartIndex))
    mystring = sText.Substring(nStartIndex + 1, nEndIndex - nStartIndex - 1);
// Only ':' was found, so extract everything until end of text
else if ((nStartIndex >= 0) && (nEndIndex < 0))
    mystring = sText.Substring(nStartIndex + 1);
ValueCntTextBox.Text = nStartIndex;
UInt32.Parse(nStartIndex, out _ValueCntTextBox);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 3 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100