void PromptForAutoNotes(string noteToParse) { string note = noteToParse; int selectionStart = textMain.SelectionStart; if (selectionStart == 0) { textMain.Text = note + textMain.Text; } else if (selectionStart == textMain.Text.Length - 1) { textMain.Text = textMain.Text + note; } else if (selectionStart == -1) //?is this even possible? { textMain.Text = textMain.Text + note; } else { textMain.Text = textMain.Text.Substring(0, selectionStart) + note + textMain.Text.Substring(selectionStart); } List <AutoNoteControl> prompts = new List <AutoNoteControl>(); List <Match> listMatches = Regex.Matches(note, @"\[Prompt:""[a-zA-Z_0-9 ]+""\]").OfType <Match>().ToList(); listMatches.RemoveAll(x => AutoNoteControls.GetByDescript(x.Value.Substring(9, x.Value.Length - 11)) == null); string autoNoteDescript; AutoNoteControl control; string promptResponse; int matchloc; int startLoc = 0; Stack <int> stackLoc = new Stack <int>(); stackLoc.Push(startLoc); for (int i = 0; i < listMatches.Count; i++) { //highlight the current match in red matchloc = textMain.Text.IndexOf(listMatches[i].Value, startLoc); startLoc = matchloc + 1; textMain.Select(matchloc, listMatches[i].Value.Length); textMain.SelectionBackColor = Color.Yellow; textMain.SelectionLength = 0; Application.DoEvents(); //refresh the textbox autoNoteDescript = listMatches[i].Value.Substring(9, listMatches[i].Value.Length - 11); control = AutoNoteControls.GetByDescript(autoNoteDescript); //should never be null since we removed nulls above promptResponse = ""; if (control.ControlType == "Text") { FormAutoNotePromptText FormT = new FormAutoNotePromptText(autoNoteDescript); FormT.PromptText = control.ControlLabel; FormT.ResultText = control.ControlOptions; if (i > 0) { FormT.IsGoBack = true; //user can go back if at least one item in the list exist } if (_listAutoNotePrompts.Count > i) { FormT.CurPromptResponse = _listAutoNotePrompts[i].AutoNoteTextString; //pass the previous response to the form } FormT.ShowDialog(); if (FormT.DialogResult == DialogResult.Retry) //user clicked the go back button { GoBack(i); stackLoc.Pop(); startLoc = stackLoc.Peek(); i -= 2; continue; } if (FormT.DialogResult == DialogResult.OK) { promptResponse = FormT.ResultText; if (_listAutoNotePrompts.Count > i) //reponse already exist for this control type. Update it { _listAutoNotePrompts[i].AutoNoteTextString = promptResponse; _listAutoNotePrompts[i].AutoNoteTextStartPos = matchloc; } else { _listAutoNotePrompts.Add(new AutoNoteListItem(listMatches[i].Value, promptResponse, matchloc)); //add new response to the list } } else { textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); return; } } else if (control.ControlType == "OneResponse") { FormAutoNotePromptOneResp FormOR = new FormAutoNotePromptOneResp(autoNoteDescript); FormOR.PromptText = control.ControlLabel; FormOR.PromptOptions = control.ControlOptions; if (i > 0) { FormOR.IsGoBack = true; //user can go back if at least one item in the list exist } if (_listAutoNotePrompts.Count > i) { FormOR.CurPromptResponse = _listAutoNotePrompts[i].AutoNoteTextString; //pass the previous response if exist to the form. } FormOR.ShowDialog(); if (FormOR.DialogResult == DialogResult.Retry) //user clicked the go back button { GoBack(i); stackLoc.Pop(); startLoc = stackLoc.Peek(); i -= 2; continue; } if (FormOR.DialogResult == DialogResult.OK) { promptResponse = FormOR.ResultText; if (_listAutoNotePrompts.Count > i) //reponse already exist for this control type. Update it { _listAutoNotePrompts[i].AutoNoteTextString = promptResponse; _listAutoNotePrompts[i].AutoNoteTextStartPos = matchloc; } else { _listAutoNotePrompts.Add(new AutoNoteListItem(listMatches[i].Value, promptResponse, matchloc)); //add new response to the list } } else { textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); return; } } else if (control.ControlType == "MultiResponse") { FormAutoNotePromptMultiResp FormMR = new FormAutoNotePromptMultiResp(autoNoteDescript); FormMR.PromptText = control.ControlLabel; FormMR.PromptOptions = control.ControlOptions; if (i > 0) { FormMR.IsGoBack = true; //user can go back if at least one item in the list exist } if (_listAutoNotePrompts.Count > i) { FormMR.CurPromptResponse = _listAutoNotePrompts[i].AutoNoteTextString; //pass the previous response if exist to the form. } FormMR.ShowDialog(); if (FormMR.DialogResult == DialogResult.Retry) //user clicked the go back button { GoBack(i); stackLoc.Pop(); startLoc = stackLoc.Peek(); i -= 2; continue; } if (FormMR.DialogResult == DialogResult.OK) { promptResponse = FormMR.ResultText; if (_listAutoNotePrompts.Count > i) //reponse already exist for this control type. Update it { _listAutoNotePrompts[i].AutoNoteTextString = promptResponse; _listAutoNotePrompts[i].AutoNoteTextStartPos = matchloc; } else { _listAutoNotePrompts.Add(new AutoNoteListItem(listMatches[i].Value, promptResponse, matchloc)); //add new response to the list } } else { textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); return; } } string resultstr = textMain.Text.Substring(0, matchloc) + promptResponse; if (textMain.Text.Length > matchloc + listMatches[i].Value.Length) { resultstr += textMain.Text.Substring(matchloc + listMatches[i].Value.Length); } textMain.Text = resultstr; textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); if (string.IsNullOrEmpty(promptResponse)) { //if prompt was removed, add the previous start location onto the stack. startLoc = stackLoc.Peek(); } stackLoc.Push(startLoc); Application.DoEvents(); //refresh the textbox } textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); _listAutoNotePrompts.Clear(); }
private void listMain_DoubleClick(object sender, EventArgs e) { if (listMain.SelectedIndex == -1) { return; } string note = AutoNotes.Listt[listMain.SelectedIndex].MainText; int selectionStart = textMain.SelectionStart; if (selectionStart == 0) { textMain.Text = note + textMain.Text; } else if (selectionStart == textMain.Text.Length - 1) { textMain.Text = textMain.Text + note; } else if (selectionStart == -1) //?is this even possible? { textMain.Text = textMain.Text + note; } else { textMain.Text = textMain.Text.Substring(0, selectionStart) + note + textMain.Text.Substring(selectionStart); } List <AutoNoteControl> prompts = new List <AutoNoteControl>(); MatchCollection matches = Regex.Matches(note, @"\[Prompt:""[a-zA-Z_0-9 ]+""\]"); string autoNoteDescript; AutoNoteControl control; string promptResponse; int matchloc; for (int i = 0; i < matches.Count; i++) { //highlight the current match in red matchloc = textMain.Text.IndexOf(matches[i].Value); textMain.Select(matchloc, matches[i].Value.Length); textMain.SelectionBackColor = Color.Yellow; textMain.SelectionLength = 0; Application.DoEvents(); //refresh the textbox autoNoteDescript = matches[i].Value.Substring(9, matches[i].Value.Length - 11); control = AutoNoteControls.GetByDescript(autoNoteDescript); if (control == null) { continue; //couldn't find a prompt with that name, so just ignore it. } promptResponse = ""; if (control.ControlType == "Text") { FormAutoNotePromptText FormT = new FormAutoNotePromptText(); FormT.PromptText = control.ControlLabel; FormT.ResultText = control.ControlOptions; FormT.ShowDialog(); if (FormT.DialogResult == DialogResult.OK) { promptResponse = FormT.ResultText; } else { textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); return; } } else if (control.ControlType == "OneResponse") { FormAutoNotePromptOneResp FormOR = new FormAutoNotePromptOneResp(); FormOR.PromptText = control.ControlLabel; FormOR.PromptOptions = control.ControlOptions; FormOR.ShowDialog(); if (FormOR.DialogResult == DialogResult.OK) { promptResponse = FormOR.ResultText; } else { textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); return; } } else if (control.ControlType == "MultiResponse") { FormAutoNotePromptMultiResp FormMR = new FormAutoNotePromptMultiResp(); FormMR.PromptText = control.ControlLabel; FormMR.PromptOptions = control.ControlOptions; FormMR.ShowDialog(); if (FormMR.DialogResult == DialogResult.OK) { promptResponse = FormMR.ResultText; } else { textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); return; } } string resultstr = textMain.Text.Substring(0, matchloc) + promptResponse; if (textMain.Text.Length > matchloc + matches[i].Value.Length) { resultstr += textMain.Text.Substring(matchloc + matches[i].Value.Length); } textMain.Text = resultstr; textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); Application.DoEvents(); //refresh the textbox } textMain.SelectAll(); textMain.SelectionBackColor = Color.White; textMain.Select(textMain.Text.Length, 0); }
///<summary>Returns the length prompt responses added to textMain.Text.</summary> private int PromptForAutoNotes(string noteToParse, List <AutoNoteListItem> listAutoNoteItem) { //AutoNote.MainText which should have all text and the prompts. string note = noteToParse; #region Insert Auto Note Text //Logic for determining where the note should go based on the users cursor location. int selectionStart = textMain.SelectionStart; if (selectionStart == 0) //Cursor is at the beginning of the textbox. { textMain.Text = note + textMain.Text; } else if (selectionStart == textMain.Text.Length - 1) //Cursor at end of textbox { textMain.Text = textMain.Text + note; } else if (selectionStart == -1) //If cursor location is unknown just append the text to the end { textMain.Text = textMain.Text + note; } else //Cursor is in between text. Insert at the selected position. { textMain.Text = textMain.Text.Substring(0, selectionStart) + note + textMain.Text.Substring(selectionStart); } #endregion //List of prompts for the auto note List <AutoNoteControl> prompts = new List <AutoNoteControl>(); //Prompts are stored in the form [Prompt: "PromptName"] List <Match> listPrompts = AutoNoteControls.GetPrompts(note); //Remove all matched prompts that do not exist in the database. listPrompts.RemoveAll(x => AutoNoteControls.GetByDescript(x.Value.Substring(9, x.Value.Length - 11)) == null); //Holds the PromptName from [Prompt: "PromptName"] string autoNoteDescript; AutoNoteControl control; string promptResponse; int matchloc; //This is the index of the Prompt location in textMain. int startLoc = 0; int retVal = 0; //the length of the notes added to textMain.Text. //used to keep track of the start position. This is needed set matchloc. Without this, match loc might find the wrong index. Stack <int> stackLoc = new Stack <int>(); stackLoc.Push(startLoc); bool isAutoNote = false; //Loop through all valid prompts for the given auto note. for (int i = 0; i < listPrompts.Count; i++) { //Find prompt location in the text and highlight yellow. matchloc = textMain.Text.IndexOf(listPrompts[i].Value, startLoc); if (matchloc == -1 || matchloc > textMain.TextLength) //The value wasn't found { continue; } startLoc = matchloc + 1; //Add one to look after the last match location. textMain.Select(matchloc, listPrompts[i].Value.Length); textMain.SelectionBackColor = Color.Yellow; textMain.SelectionLength = 0; Application.DoEvents(); //refresh the textbox so the yellow will show //Holds the PromptName from [Prompt: "PromptName"] autoNoteDescript = listPrompts[i].Value.Substring(9, listPrompts[i].Value.Length - 11); control = AutoNoteControls.GetByDescript(autoNoteDescript); //should never be null since we removed nulls above promptResponse = ""; if (control.ControlType == "Text") //Response just inserts text. No choosing options here. { FormAutoNotePromptText FormT = new FormAutoNotePromptText(autoNoteDescript); FormT.PromptText = control.ControlLabel; FormT.ResultText = control.ControlOptions; isAutoNote = false; if (i > 0) { FormT.IsGoBack = true; //user can go back if at least one item in the list exist } if (listAutoNoteItem.Count > i) { FormT.CurPromptResponse = listAutoNoteItem[i].AutoNotePromptResponseString; //pass the previous response to the form } FormT.ShowDialog(); if (FormT.DialogResult == DialogResult.Retry) //user clicked the go back button { GoBack(i, listAutoNoteItem); stackLoc.Pop(); //remove the start location startLoc = stackLoc.Peek(); //set the new start location i -= 2; continue; } if (FormT.DialogResult == DialogResult.OK) { promptResponse = FormT.ResultText; if (listAutoNoteItem.Count > i) //reponse already exist for this control type. Update it //We need to update retVal with the length of the new promptResponse. First, remove the previous prompts length. { retVal -= listAutoNoteItem[i].AutoNoteTextEndPos; //Update the retVal with the new promptResponse.Length. retVal += promptResponse.Length; //Update the rest of the AutoNoteItem with the new promptResponse. listAutoNoteItem[i].AutoNotePromptResponseString = promptResponse; listAutoNoteItem[i].AutoNoteTextStartPos = matchloc; listAutoNoteItem[i].AutoNoteTextEndPos = promptResponse.Length; } else { //This is a new response. Create a new AutoNoteListItem and add to retVal. retVal += promptResponse.Length; listAutoNoteItem.Add(new AutoNoteListItem(listPrompts[i].Value, promptResponse, matchloc, promptResponse.Length)); //add new response to the list } } else //User cancelled out of the auto note prompt text form or auto log off. { ResetTextMain(); return(-1); } } else if (control.ControlType == "OneResponse") { FormAutoNotePromptOneResp FormOR = new FormAutoNotePromptOneResp(autoNoteDescript); FormOR.PromptText = control.ControlLabel; FormOR.PromptOptions = control.ControlOptions; if (i > 0) { FormOR.IsGoBack = true; //user can go back if at least one item in the list exist } if (listAutoNoteItem.Count > i) { FormOR.CurPromptResponse = listAutoNoteItem[i].AutoNotePromptResponseString; //pass the previous response if exist to the form. } FormOR.ShowDialog(); if (FormOR.DialogResult == DialogResult.Retry) //user clicked the go back button { GoBack(i, listAutoNoteItem); stackLoc.Pop(); //remove the start location startLoc = stackLoc.Peek(); //set the new start location i -= 2; continue; } if (FormOR.DialogResult == DialogResult.OK) { promptResponse = FormOR.ResultText; //The promptResponse will have the AutoNoteName in the format "Auto Note Response : {AutoNoteName}". Use the name to get the note that //will be used in the recursive call below. string autoNoteString = ""; isAutoNote = false; string autoNoteName = GetAutoNoteName(promptResponse); if (!string.IsNullOrEmpty(autoNoteName)) { isAutoNote = true; //For some reason the Auto Note string always contains a new line and a return character at the end of the note. Must be trimmed autoNoteString = AutoNotes.GetByTitle(autoNoteName).TrimEnd('\n').TrimEnd('\r'); //Returns empty string If no AutoNote is found. } if (listAutoNoteItem.Count > i && !isAutoNote) //The response already exist for this control type and it is note an AutoNote. Update it //We need to update retval with the length of the new promptResponse. First, remove the previous promptResponse length. { retVal -= listAutoNoteItem[i].AutoNoteTextEndPos; //Update the retval with the new promptResponse.Length. retVal += promptResponse.Length; //Update the rest of the AutoNoteItem with the new promptResponse. listAutoNoteItem[i].AutoNotePromptResponseString = promptResponse; listAutoNoteItem[i].AutoNoteTextStartPos = matchloc; listAutoNoteItem[i].AutoNoteTextEndPos = promptResponse.Length; } else if (isAutoNote) //The response is an auto note. Recursively call this method. //Remove the response from textMain.Text. The response was already saved. Since this is an AutoNote, the response does not need to stay //in the textMain.Text since more than likely more prompts will happen after we call the recursive method below. { string textMainWithoutResponse = textMain.Text.Substring(0, matchloc) + GetAutoNoteResponseText(promptResponse); if (textMain.Text.Length > matchloc + listPrompts[i].Value.Length) { textMainWithoutResponse += textMain.Text.Substring(matchloc + listPrompts[i].Value.Length); } matchloc += GetAutoNoteResponseText(promptResponse).Length; //set the textMain.Text to the new result. This removes the promptResponse. textMain.Text = textMainWithoutResponse; textMain.SelectionStart = matchloc; //This is needed in order for the recursive method call below. //Pass in the AutoNotes note in the recursive call. int lenthOfAutoNoteAdded = PromptForAutoNotes(autoNoteString, new List <AutoNoteListItem>()); if (lenthOfAutoNoteAdded == -1) { return(-1); } //When we get back from the recursive method, we need to figure out what was added so we can create the AutoNoteListItem promptResponse = textMain.Text.Substring(matchloc, lenthOfAutoNoteAdded); //Update the retVal with the new promptResponse.Length. retVal += promptResponse.Length; //if response already exist, update it. if (listAutoNoteItem.Count > i) { //We need to update retVal with the length of the new promptResponse. First, remove the previous prompts length. retVal -= listAutoNoteItem[i].AutoNoteTextEndPos; //Update the rest of the AutoNoteItem with the new promptResponse. listAutoNoteItem[i].AutoNotePromptResponseString = promptResponse; //should be the same. Updating just in case. listAutoNoteItem[i].AutoNoteTextStartPos = matchloc; listAutoNoteItem[i].AutoNoteTextEndPos = promptResponse.Length; //This is the length of what was added. } else //New Response. Add a new AutoNoteListItem. //Add the response to listAutoNoteItem. This will allow the user to go back. Make the end position equal to the length of the AutoNote { listAutoNoteItem.Add(new AutoNoteListItem(listPrompts[i].Value, promptResponse, matchloc, promptResponse.Length)); } } else { //This is a new response. Create a new AutoNoteListItem and add to retVal. retVal += promptResponse.Length; listAutoNoteItem.Add(new AutoNoteListItem(listPrompts[i].Value, promptResponse, matchloc, promptResponse.Length)); //add new response to the list } } else //User cancelled out of the auto note response form or auto log off. { ResetTextMain(); return(-1); } } else if (control.ControlType == "MultiResponse") { FormAutoNotePromptMultiResp FormMR = new FormAutoNotePromptMultiResp(autoNoteDescript); FormMR.PromptText = control.ControlLabel; FormMR.PromptOptions = control.ControlOptions; isAutoNote = false; if (i > 0) { FormMR.IsGoBack = true; //user can go back if at least one item in the list exist } if (listAutoNoteItem.Count > i) { FormMR.CurPromptResponse = listAutoNoteItem[i].AutoNotePromptResponseString; //pass the previous response if exist to the form. } FormMR.ShowDialog(); if (FormMR.DialogResult == DialogResult.Retry) //user clicked the go back button { GoBack(i, listAutoNoteItem); stackLoc.Pop(); //remove the start location startLoc = stackLoc.Peek(); //set the new start location i -= 2; continue; } if (FormMR.DialogResult == DialogResult.OK) { promptResponse = FormMR.ResultText; if (listAutoNoteItem.Count > i) //reponse already exist for this control type. Update it //We need to update retval with the length of the new promptresponse.First, remove the previous prompts length. { retVal -= listAutoNoteItem[i].AutoNoteTextEndPos; //update the retval with the new promptResponse.Length. retVal += promptResponse.Length; //Update the rest of the AutoNoteItem with the new promptResponse. listAutoNoteItem[i].AutoNotePromptResponseString = promptResponse; listAutoNoteItem[i].AutoNoteTextStartPos = matchloc; listAutoNoteItem[i].AutoNoteTextEndPos = promptResponse.Length; } else { //This is a new response. Create a new AutoNoteListItem and add to retVal. retVal += promptResponse.Length; listAutoNoteItem.Add(new AutoNoteListItem(listPrompts[i].Value, promptResponse, matchloc, promptResponse.Length)); //add new response to the list } } else //User cancelled out of the auto note response form or auto log off. { ResetTextMain(); return(-1); } } string resultstr = textMain.Text.Substring(0, matchloc) + promptResponse; if (!isAutoNote && textMain.Text.Length > matchloc + listPrompts[i].Value.Length) { resultstr += textMain.Text.Substring(matchloc + listPrompts[i].Value.Length); } else if (isAutoNote && textMain.Text.Length > matchloc + promptResponse.Length) { //if any of the prompts had an AutoNote and textmain has more text, add the rest of textMain. resultstr += textMain.Text.Substring(matchloc + promptResponse.Length); //update the startLoc to include the promptResponse of the AutoNote. startLoc += promptResponse.Length - 1; } textMain.Text = resultstr; ResetTextMain(); if (string.IsNullOrEmpty(promptResponse)) { //if prompt was removed, add the previous start location onto the stack. startLoc = stackLoc.Peek(); } stackLoc.Push(startLoc); Application.DoEvents(); //refresh the textbox } ResetTextMain(); listAutoNoteItem.Clear(); return(retVal); }