I have a list of lines that looks like this:
textbox.text += "p"+b+" the rest\r\np"+b+" more text";
b is supposed to represent the current line number in the textbox that the line is on. I have tried using textbox.lines.count() but it only changes i into the last line number.
Is there any other way about going with this, or do I have to switch to another method?
If you are assigning, I think you can do it manually (calculate the line number). There is no function that could "guess" on which line the tex will appear.
You can create a integer variable and increment it when appending a line/s and use the variable when you need to display the current line number.
I split the lines by the line breaks ("\r\n") and used a for loop to replace "b" (I changed it to string rather than a variable)
for (int i = 0; i < da.Length; i++)
{
//replace char with number
string f = da[i].Replace("n", (i + 1).ToString());
disp.Text += f + "v";
}
I added "v" so that I can replace it outside of the loop with "\r\n" again.
Related
private void Parse_Click(object sender, EventArgs e)
{
for (int i = 0; i < keywordRanks.Lines.Length; i++)
{
int p = keywordRanks.Lines.Length;
MessageBox.Show(p.ToString());
string splitString = keywordRanks.Lines[i];
string[] s = splitString.Split(':');
for (int j = 0; j < keywords.Lines.Length; j++)
{
string searchString = keywords.Lines[j];
if (s[0].Equals(searchString))
{
richTextBox1.Lines[j] = searchString + ':' + s[1];
}
}
}
}
I have an issue with inserting string in a particular line. I have 2 multi line TextBoxes and one RichTextBox.
My application will search for the strings from textbox1 to textbox2 line by line and need to insert those matched values in a RichTextBox control but in the exact index position where it found in textbox2.
If the value found in 5th line of textbox2 then that found line need to be inserted in the RichTextBox 5th line.
Some how my code is not working. I tried a lot but no luck. The code I need something like below but its not working and an IndexOutOfBound exception is raised.
richTextBox1.Lines[j] = searchString + ':' + s[1];
Your RichTextBox must contain all the needed lines before you can set the value using the line index.
If the Control contains no text or line breaks (\n), no lines are defined and any attempt to set a specific Line[Index] value will generate an IndexOutOfRangeException exception.
Here, I'm using a pre-built array, sized as the number of possible matches (the Lines.Length of the keywords TextBox). All matches found are stored here in the original position. The array is then assigned to the RichTextBox.Lines property.
Note: directly using and pre-setting the RichTextBox.Lines won't have effect: the text will remain empty.
string[] MatchesFound = new string[keywords.Lines.Length];
foreach (string currentSourceLine in keywordRanks.Lines)
{
string[] SourceLineValue = currentSourceLine.Split(':');
var match = keywords.Lines.ToList().FindIndex(s => s.Equals(SourceLineValue[0]));
if (match > -1)
MatchesFound[match] = currentSourceLine;
}
richTextBox1.Lines = MatchesFound;
Source Matches Result
(keywordRanks) (keywords) (RichTextBox)
-------------------------------------------
aand:1 aand aand:1
cnd:5 this one
cnds:9 cnds cnds:9
fan:2 another one
gst:0 cnd cnd:5
fan fan:2
I have a list of strings stored in an ArrayList. I want to split them by every occurrence of ';'. The problem is, whenever I try to display them using MessageBox, there's an excess space or unnecessary value that gets displayed.
Sample input (variable = a):
Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;
Below is a line of code I used to split them:
string[] display_document = (a[0] + "").Split(';');
Code to display:
foreach (object doc_properties in display_document)
{
TextBox aa = new TextBox();
aa.Font = new Font(aa.Font.FontFamily, 9);
aa.Text = doc_properties.ToString();
aa.Location = new Point(pointX, pointY);
aa.Size = new System.Drawing.Size(80, 25);
aa.ReadOnly = true;
doc_panel.Controls.Add(aa);
doc_panel.Show();
pointY += 30;
}
The output that displays are the following:
How do I remove the last occurrence of that semicolon? I really need help fixing this. Thank you so much for all of your help.
Wouldnt It be easiest to check if the input ends with a ";" before splitting it, and if so remove the last character? Sample code:
string a = "Arial;16 pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
if (a.EndsWith(";"))
{
a = a.Remove(a.LastIndexOf(";"));
}
//Proceed with split
Split will not print last semicolon if no space character is added and your input is a string.
I don't know why you prefer an array list (which probably is the reason of this strange behaviour) but if you could use your input as a string you could try that
string a = "Arial;16pt;None;None;None;None;None;None;FF0000;None;100;Normal;None;Normal;";
string[] display_document = a.Split(';');
foreach (object doc_properties in display_document)
{
//The rest of your code
}
I've assigned some string values from a data set to the string b.
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
string b = ds.Tables[0].Rows[i].ItemArray[0] + " " + ds.Tables[0].Rows[i].ItemArray[1];
}
What I want to do is add those values, and finally show as a sentence.
Example: if "dog", "cat", and "cow" are the values read by the for loop, I want to display "dog cat cow" in a message box. How to do that?
Edit: Since it appears you are interested in the String.Join() method this could work perfectly for you. You have plenty of options here but if you want to go that route here's how.
First create an array of the items you are returning then you can simple use the String.Join() method to concatenate the items in the array like so:
string separator = whatever seperator you want "," or "|"
string d = String.Join(separator, animalArray);
MessageBox.Show(d);
The first thing you'll need to do is make sure you have imported the System.Windows.Forms namespace to enable your ability to call the MessageBox function.
Essentially you are already there with the concatenation of the strings. If you are looking for a cleaner option I would recommend using String.Format() or using the newer method of concatenation by applying a '$' character in front of a string which allows you to simple add your variables between curly braces.
For example: $"Hello my name is {name}."
What's wrong with the above?
You already have them in your string variable b.
Enclose it in MessageBox.Show(); instead of adding the variable.
string b = "";
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
b += ds.Tables[0].Rows[i].ItemArray[0] + " " + ds.Tables[0].Rows[i].ItemArray[1] + "\n";
}
MessageBox.Show(b);
I split the input paragraph by . and store it in an array like this:
string[] totalSentences = inputPara.Split('.')
then the function below is called which calculates total number of Words from each sentence like this:
public void initParaMatrix()
{
int size = 0;
for (int i = 0; i < totalSentences.Length; i++)
{
string[] words = totalSentences[i].Split();
size = size + words.Length;
//rest of the logic here...
}
matrixSize = size;
paraMatrix = new string[matrixSize, matrixSize];
}
paraMatrix is a 2D matrix equal to length of all words which I need to make in my logic.
The problem here is when I input only one sentence which has 5 words, the size variable gets the value 7. I tried the debugger and I was getting total of 2 sentences instead of 1.
Sentence 1. "Our present ideas about motion." > this is actual sentence which have only 5 words
Sentence 2. " " > this is the exact second sentence I'm getting.
Here is the screenshot:
Why I'm getting two sentences here and how is size getting value 7?
This makes perfect sense. If the second sentence has nothing but a " ", and you split along the " ", then you'll have two empty strings as a result. The easiest thing to do here is change the line you do the split, and add a trim:
string[] words = totalSentences[i].Trim().Split();
I don't know what version of Split that you're using since it accepts no parameters, but if you use String.Split you can set the second parameter so that empty entries are automatically removed by using the option StringSplitOptions.RemoveEmptyEntries.
You're not resetting the size integer to zero. So that's why you get 7 for the second sentence.
For the second sentence, which is a space, try inputPara.Trim() which should remove the space at the end of the string.
I have a program which needs to determine the number of lines in a multiline textbox to know how to process it. I am calling the TextBox.Lines.Length property, which was working. Now however, no matter how many lines of text are visible in the GUI, this value is 1, and all of the "\r\n" strings have disappeared from the TextBox.Text string. Any Ideas? My code is as following :
TextBox.MultiLine = true;
TextBox.WordWrap = true;
for (int i = 0; i < TextBox.Lines.Length - 1; i++)
//Some Code
As I said in the comment, with Multiline=True and WordWrap=True, your textbox will display a long line as multilines (Wrapped)... but actually it is one single line, and that's why your Lines.Length=1, try type in some line break yourself, and test it again. Or you can set WordWrap=False, and you will see there is only one line...
It will need to be marked as multiline, check that, you can parse like so:
string txt = TextBox1.Text;
string[] lst = txt.Split(new Char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)