How can I delete the selected Lines in Multi line Text Box? - c#

My code below here I used "SelectedLines" to find the current selected lines by user, AllLines to find the no of total lines in Multi line text box. in last for loop when i=1 it runs successfully but when i is +1 = 2 then it gives me error
Error : "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index?"
// Retrieve selected lines
List<string> SelectedLines = Regex.Split(txtNewURLs.SelectedText, #"\r\n").ToList();
// Check for nothing, Regex.Split returns empty string when no text is inputted
if (SelectedLines.Count == 1)
{
if (String.IsNullOrWhiteSpace(SelectedLines[0]))
{
SelectedLines.Remove("");
}
}
// Retrieve all lines from textbox
List<string> AllLines = Regex.Split(txtNewURLs.Text, #"\r\n").ToList();
// Check for nothing, Regex.Split returns empty string when no text is inputted
if (AllLines.Count == 1)
{
if (String.IsNullOrWhiteSpace(AllLines[0]))
{
AllLines.Remove("");
}
}
string SelectedMessage = "The following lines have been selected";
int numSelected = 0;
// Find all selected lines
foreach (string IndividualLine in AllLines)
{
if (SelectedLines.Any(a => a.Equals(IndividualLine)))
{
SelectedMessage += "\nLine #" + AllLines.FindIndex(a => a.Equals(IndividualLine));
// changing the status of the selected lene from 0 to 1
AddURL objAddURL = new AddURL();
objAddURL.Where.SURL.Value = IndividualLine;
objAddURL.Where.ILicenseID.Value = CommonMethods.iLicenseID;
objAddURL.Query.Load();
if (objAddURL.RowCount > 0)
{
AddURL objaddurl1 = new AddURL();
objaddurl1.LoadByPrimaryKey(objAddURL.IAddURLID);
if (objaddurl1.RowCount > 0)
{
objaddurl1.IStatus = 1;
objaddurl1.Save();
}
// AllLines.Remove(IndividualLine);
}
numSelected++;
}
}
// int[] lineNo = new int[AllLines.Count];
int linesNO = SelectedLines.Count;
for (int i = 1; i <= linesNO; i++)
{
SelectedLines.RemoveAt(i);
}
// MessageBox.Show((numSelected > 0) ? SelectedMessage : "No lines selected.");
Could any body please help me to solve this issue or suggest me new code?

Indices in c# are zero based.
for (int i = 0; i < linesNO; i++)
SelectedLines.RemoveAt(i);
But if you want to delete the selected lines from your textBox your code should rather look like
List<string> SelectedLines = new List<string> { "b", "c" };
textBox1.Lines = textBox1.Lines.ToList().Except(SelectedLines).ToArray();

Related

How can I use indexof and substring to find words in a string?

In the constructor :
var tempFR = File.ReadAllText(file);
GetResults(tempFR);
Then :
private List<string> GetResults(string file)
{
List<string> results = new List<string>();
string word = textBox1.Text;
string[] words = word.Split(new string[] { ",," }, StringSplitOptions.None);
for(int i = 0; i < words.Length; i++)
{
int start = file.IndexOf(words[i], 0);
results.Add(file.Substring(start));
}
return results;
}
words contains in this case 3 words System , public , test
I want to find all the words in file and add them to the list results using indexof and substring.
The way it is now start value is -1 all the time.
To clear some things.
This is a screenshot of the textBox1 :
That is why I'm using two commas to split and get the words.
This screenshot showing the words after split them from the textBox1 :
And this is the file string content :
I want to add to the List results all the words in the file.
When looking at the last screenshot there should be 11 results.
Three time the word using three times the word system five times the word public.
but the variable start is -1
Update :
Tried Barns solution/s but for me it's not working good.
First the code that make a search and then loop over the files and reporting to backgroundworker :
int numberofdirs = 0;
void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
{
List<string> filePathList = new List<string>();
int numberoffiles = 0;
try
{
filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null, worker, e).ToList();
}
catch (Exception err)
{
}
label21.Invoke((MethodInvoker)delegate
{
label21.Text = "Phase 2: Searching in files";
});
MyProgress myp = new MyProgress();
myp.Report4 = filePathList.Count.ToString();
foreach (string file in filePathList)
{
try
{
var tempFR = File.ReadAllText(file);
_busy.WaitOne();
if (worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
bool reportedFile = false;
for (int i = 0; i < textToSearch.Length; i++)
{
if (tempFR.IndexOf(textToSearch[i], StringComparison.InvariantCultureIgnoreCase) >= 0)
{
if (!reportedFile)
{
numberoffiles++;
myp.Report1 = file;
myp.Report2 = numberoffiles.ToString();
myp.Report3 = textToSearch[i];
myp.Report5 = FindWordsWithtRegex(tempFR, textToSearch);
backgroundWorker1.ReportProgress(0, myp);
reportedFile = true;
}
}
}
numberofdirs++;
label1.Invoke((MethodInvoker)delegate
{
label1.Text = string.Format("{0}/{1}", numberofdirs, myp.Report4);
label1.Visible = true;
});
}
catch (Exception err)
{
}
}
}
I have the words array already in textToSearch and the file content in tempFR then I'm using the first solution of Barns :
private List<string> FindWordsWithtRegex(string filecontent, string[] words)
{
var res = new List<string>();
foreach (var word in words)
{
Regex reg = new Regex(word);
var c = reg.Matches(filecontent);
int k = 0;
foreach (var g in c)
{
Console.WriteLine(g.ToString());
res.Add(g + ":" + k++);
}
}
Console.WriteLine("Results of FindWordsWithtRegex");
res.ForEach(f => Console.WriteLine(f));
Console.WriteLine();
return res;
}
But the results I'm getting in the List res is not the same output in Barns solution/s this is the results I'm getting the List res for the first file :
In this case two words system and using but it found only the using 3 times but there is also system 3 times in the file content. and the output format is not the same as in the Barns solutions :
Here is an alternative using Regex instead of using IndexOf. Note I have created my own string to parse, so my results will be a bit different.
EDIT
private List<string> FindWordsWithCountRegex(string filecontent, string[] words)
{
var res = new List<string>();
foreach (var word in words)
{
Regex reg = new Regex(word, RegexOptions.IgnoreCase);
var c = reg.Matches(filecontent).Count();
res.Add(word + ":" + c);
}
return res;
}
Simple change this part and use a single char typically a space not a comma:
string[] words = word.Split(' ');
int start = file.IndexOf(words[i],0);
start will be -1 if the word is not found.
MSDN: IndexOf(String, Int32)
for(int i = 0; i < words.Length; i++)
{
int start = file.IndexOf(words[i], 0);
// only add to results if word is found (index >= 0)
if (start >= 0) results.Add(file.Substring(start));
}
If you want all appearance of the words you need an extra loop
int fileLength = file.Length;
for(int i = 0; i < words.Length; i++)
{
int startIdx = 0;
while (startIdx < fileLength ){
int idx = file.IndexOf(words[i], startIdx]);
if (start >= 0) {
// add to results
results.Add(file.Substring(start));
// and let Word-search continue from last found Word Position Ending
startIdx = (start + words.Length);
}
}
int start = file.IndexOf(words[i], 0);
// only add to results if word is found (index >= 0)
if (start >= 0) results.Add(file.Substring(start));
}
MayBe you want a caseinsensitiv search
file.IndexOf(words[i], 0, StringComparison.CurrentCultureIgnoreCase); MSDN: StringComparer Class

How to extract the content of a text file within a scope delimined by a string marker

I have a Console File,
I need to match a string ("Seq Started"), And if I get the string I want to copy all text till I get another string("Seq Ended") in a txt file.
You can use this.
We load the file and parse lines to search the starting and ending deliminers sequences assuming only one of each of them are allowed.
Then if the section is correclty found, we extract the lines of the soure using Linq and we save the result to the desired file.
using System.Linq;
using System.Collections.Generic;
static void Test()
{
string delimiterStart = "Seq Started";
string delimiterEnd = "Seq Ended";
string filenameSource = "c:\\sample source.txt";
string filenameDest = "c:\\sample dest.txt";
var result = new List<string>();
var lines = File.ReadAllLines(filenameSource);
int indexStart = -1;
int indexEnd = -1;
for ( int index = 0; index < lines.Length; index++ )
{
if ( lines[index].Contains(delimiterStart) )
if ( indexStart == -1 )
indexStart = index + 1;
else
{
Console.WriteLine($"Only one \"{delimiterStart}\" is allowed in file {filenameSource}.");
indexStart = -1;
break;
}
if ( lines[index].Contains(delimiterEnd) )
{
indexEnd = index;
break;
}
}
if ( indexStart != -1 && indexEnd != -1 )
{
result.AddRange(lines.Skip(indexStart).Take(indexEnd - indexStart));
File.WriteAllLines(filenameDest, result);
Console.WriteLine($"Content of file \"{filenameSource}\" extracted to file {filenameDest}.");
}
else
{
if ( indexStart == -1 )
Console.WriteLine($"\"{delimiterStart}\" not found in file {filenameSource}.");
if ( indexEnd == -1 )
Console.WriteLine($"\"{delimiterEnd}\" not found in file {filenameSource}.");
}
}
// Read each line of the file into a string array. Each element
// of the array is one line of the file.
string[] lines = System.IO.File.ReadAllLines(#"C:\Users\Public\TestFolder\WriteLines2.txt");
// Display the file contents by using a foreach loop.
System.Console.WriteLine("Contents of WriteLines2.txt = ");
foreach (string line in lines)
{
if(line == "Seq Started" && line != "Seq Ended")
{
//here you get each line in start to end one by one.
//apply your logic here.
}
}
Check the second example in below link-
[https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-read-from-a-text-file][1]

Get text between specific character

I want to get the text between ';' character. However if there are 3 matches like that, i want to get the text between ';' character where current cursor position. I am using multine textbox.
Example;
select * from database;
----> **Lets say my cursor is here**
select
orders from
customer;
select * from employees;
So, i only want to get 'select orders from customer' text.
Could you please share your thoughts on it?
To achieve this, you first have to find all indicies of ;. To do this, iterate through all indicies (source):
private List<int> AllIndicesOf(string strToSearch, string fullText)
{
List<int> foundIndices = new List<int>();
for (int i = fullText.IndexOf(strToSearch); i > -1; i = fullText.IndexOf(strToSearch, i + 1))
{
foundIndices.Add(i + 1);
}
return foundIndices;
}
Then you have to compare your position to those indices, since you only want the index (of ;) that follows immediately after your cursor:
List<int> indicies = AllIndicesOf(";", txtBxText.Text);
try
{
if (indicies.Count > 0)
{
int cursorPos = txtBxText.SelectionStart;
var indicesBefore = indicies.Where(x => x < cursorPos);
int beginIndex = indicesBefore.Count() > 0 ? indicesBefore.Last() : 0;
int endIndex = indicies.Where(x => x > beginIndex).First();
txtBxSelected.Text = txtBxText.Text.Substring(beginIndex, endIndex - beginIndex);
}
}
catch { }
The try-catch statement is used to prevent an Exception if your cursors position is after all other indices.
A sample project can be downloaded here.
This solution perfectly works although you need to check it again and consider some possible exceptions. I did not consider them myself because I thought it was better to be handled by you. I also used richTextBox which is better than the multi line text box. Enjoy the code bro
private void button1_Click(object sender, EventArgs e)
{
var ultimateResult = string.Empty;
var cusrPosition = richTextBox1.SelectionStart;
var currentStr = string.Empty;
var strDic = new Dictionary<string,int>();
var textArr = richTextBox1.Text.ToCharArray();
for (var i = 0; i < textArr.Count(); i++)
{
if (textArr[i] != ';')
currentStr = currentStr + textArr[i];
else
{
strDic.Add(currentStr,i);
currentStr = string.Empty;
}
}
ultimateResult = strDic.First(item => item.Value >= cusrPosition).Key;
textBox1.Text = ultimateResult;
}

How can I make 1 button click fill out 2 different text boxes

I need help making this program fill out 2 different text boxes. It works if I only use it to fill out 1 text box using a First name. But when I try and add another text box to generate a random Last name it just seems to do fail.
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int currentLine = 1;
string pick = null;
foreach (string line in File.ReadLines("C:\\First Names.txt"))
{
if (r.Next(currentLine) == 0)
{
pick = line;
}
++currentLine;
textBox1.Text = pick;
}
Random n = new Random();
int currentLine1 = 1;
string pick1 = null;
foreach (string line1 in File.ReadLines("C:\\Last Names.txt"))
{
if (n.Next(currentLine1) == 0)
{
pick1 = line1;
}
++currentLine1;
textBox2.Text = pick1;
}
}
}
}
Check whether the path for the file is appropriate.
You need not create two objects for the same class and access them differently.One object is enough.
3.Check whether data is there in the file or not.`Random r = new Random();
int currentLine = 1;
string pick = null;
foreach (string line in File.ReadLines("C:\\First Names.txt"))
{
if (r.Next(currentLine) == 0)
{
pick = line;
}
++currentLine;
textBox1.Text = pick;
}
currentLine=0;pick=0;
foreach (string line in File.ReadLines("C:\\Last Names.txt"))
{
if (r.Next(currentLine) == 0)
{
pick = line;
}
++currentLine;
textBox2.Text = pick;
}
}`
If the first iteration gets the value, then the second must also get. Else there is some problem with Text file or the 'textBox2' is not the correct ID you are looking for.

Docx - Removing section of document

Is there a way to remove sections of a document where i can specify the beginning and ending tags?
i need a way that i can remove a section of the document by passing in both my start and end catches, (##DELETEBEGIN and ##DELETEEND)
for example i have this in my document:
Hello, welcome to this document
##DELETEBEGIN{Some values to check in the code}
Some text that will be removed if the value is true
##DELETEEND
Final Line
If you need to delete text from ##DELETEBEGIN to ##DELETEEND, where ##DELETEBEGIN is not at the beginning of a Paragraph and ##DELETEEND is not at the end of a Paragraph, this code should work.
DocX document = DocX.Load("C:\\Users\\phil\\Desktop\\text.docx");
bool flag = false;
List<List<string>> list1 = new List<List<string>>();
List<string> list2 = new List<string>();
foreach (Novacode.Paragraph item in document.Paragraphs)
{
//use this if you need whole text of a paragraph
string paraText = item.Text;
var result = paraText.Split(' ');
int count = 0;
list2 = new List<string>();
//use this if you need word by word
foreach (var data in result)
{
string word = data.ToString();
if (word.Contains("##DELETEBEGIN")) flag = true;
if (word.Contains("##DELETEEND"))
{
flag = false;
list2.Add(word);
}
if (flag) list2.Add(word);
count++;
}
list1.Add(list2);
}
for (int i = 0; i < list1.Count(); i++)
{
string temp = "";
for (int y = 0; y < list1[i].Count(); y++)
{
if (y == 0)
{
temp = list1[i][y];
continue;
}
temp += " " + list1[i][y];
}
if (!temp.Equals("")) document.ReplaceText(temp, "");
}
document.Save();
I have to give some credit to this post for looping through each word.
I think i have found a solution to this, at least it works for me, please let me know if there is anything i can do better:
the deleteCommand would be the ##DELETEBEGIN string and the deleteEndCommand would be the ##DELETEEND
private void RemoveSection(DocX doc, string deleteCommand, string deleteEndCommand)
{
try
{
int deleteStart = 0;
int deleteEnd = 0;
//Get the array of the paragraphs containing the start and end catches
for (int i = 0; i < doc.Paragraphs.Count; i++)
{
if (doc.Paragraphs[i].Text.Contains(deleteCommand))
deleteStart = i;
if (doc.Paragraphs[i].Text.Contains(deleteEndCommand))
deleteEnd = i;
}
if (deleteStart > 0 && deleteEnd > 0)
{
//delete from the paraIndex as the arrays will shift when a paragraph is deleted
int paraIndex = deleteStart;
for (int i = deleteStart; i <= deleteEnd; i++)
{
doc.RemoveParagraphAt(paraIndex);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Categories

Resources