Hello!
As the header is implying I'm trying to retrieve a certain line of numbers from a richtextbox and then put it into a separate textbox. I've tried this code below but it doesn't wanna work with me. It's probably way wrong and there's probably easier ways of doing it but I'm quite new to this stuff and I would appreciate all the help I can get in this matter.
I have a textbox called tbPersNr;
A RichTextBox called tbText;
A button which is called btnGet;
string regPattern = @"\\d{6}-\\d{4}";
int indexOfSearch = 0;
private void btnGet_Click(object sender, EventArgs e)
{
int startIndex = 0;
if (tbText.Text.Length > 0)
{
startIndex = HittaPersNr(regPattern, startIndex, tbText.Text.Length);
}
if (startIndex > 0)
{
this.tbPersNr.Text = regPattern.ToString();
}
}
public int HittaPersNr(string txtToSearch, int searchStart, int searchEnd)
{
// Setting default value to -1.
int retVal = -1;
// Validating start of the search
// om indexOfSearch = -1, slutar sökningen
if (searchStart >= 0 && indexOfSearch >= 0)
{
// Validating end of search
if (searchEnd > searchStart || searchEnd == -1)
{
// Searching for results in richtextbox
indexOfSearch = tbText.Find(regPattern, searchStart, searchEnd, RichTextBoxFinds.None);
// Validating if search resulted in any finds.
if (indexOfSearch != -1)
{
// putting index to value in the text.
retVal = indexOfSearch;
}
}
}
return retVal;
}
UPDATE
Cheers for all helpful answers,,,and sorry for expressing myself quite clumsy. But I expected I was on the wrong track here and I'm gonna try fix my app with some of the answers. Welbog,,,good detective work there,,,hehehe,,,good that you understood what I was meaning. Thanks all of you who pointed out how to write the Regex in a proper way,,,quite confusing.
Most greatful
Simon