In .NET, you can easily get the line number of the cursor location of a TextBox (i.e. the "current line") by using GetLineFromCharIndex and SelectionStart:
var currentLine = textBox1.GetLineFromCharIndex(textBox1.SelectionStart);
Is there a "clean/native" way to set the cursor in a given line of a Textbox (i.e. set the "current line")? Or at least a "clean/native" way to get the char index of the first character of a given line (something like getCharIndexFromLine, the opposite of the function I put before)?
A way to do it would involve iterating over the first N-1 elements of the Lines property of the TextBox and summing their lengths plus the lengths of the linebreaks. Any other idea?
There is a GetFirstCharIndexFromLine() function that is available:
int myLine = 3;
int pos = textBox1.GetFirstCharIndexFromLine(myLine);
if (pos > -1) {
textBox1.Select(pos, 0);
}
This was the best I could come up with:
private void SetCursorLine(TextBox textBox, int line)
{
int seed = 0, pos = -1;
line -= 1;
if(line == 0) pos = 0;
else
for (int i = 0; i < line; i++)
{
pos = textBox.Text.IndexOf(Environment.NewLine, seed) + 2;
seed = pos;
}
if(pos != -1) textBox.Select(pos, 0);
}
If you want to start counting lines at 0 remove the line -= 1; segment.
Related
I have a problem in C# with List type. I can't access to List variable elements with decimal value.
I have below function. That get position value and a string_list (exactly list of various characters) and find that specific string in that position in string_list.
private string generator(decimal position, List<string> string_list)
{
List<decimal> mod_list = new List<decimal>();
string output = "";
while (position >= 0)
{
mod_list.Add(position % string_list.Count);
position -= position % string_list.Count;
position /= (decimal)string_list.Count;
position -= 1;
}
int z = 1;
for (decimal i = (mod_list.Count - 1); i >= 0; i += -1)
output += string_list[mod_list[i]];
return output;
}
in line output += string_list[mod_list[i]]; Visual Studio gets an error from me because the index value should be "int". I test this function with "int" data type and get result but i want it with decimal.
Error message in Visual Studio:
Argument 1: cannot convert from 'decimal' to 'int'
Thanx for your answers ^_^
Simply change the decimal type to int, he index type is int, it's said in the error message.
private string generator(int position, List<string> string_list) {
List<int> mod_list = new List<int> ();
string output = "";
while (position >= 0) {
mod_list.Add(position % string_list.Count);
position -= position % string_list.Count;
position /= string_list.Count;
position -= 1;
}
int z = 1;
for (int i = (mod_list.Count - 1); i >= 0; i += -1)
output += string_list[mod_list[i]];
return output;
}
I have an array of string elements of various words. I need the characters of each word be split equally into the text component of 3 buttons. For example, the array could hold the elements "maybe", "his", "car". In each game one of these words will be pulled from the array and its characters divided into the 3 buttons. For example, button 1 will have "ma", button 2 will have "yb" and button 3 "e" (for the word maybe). I then hide the text element of one button for the user to drag and drop the correct missing letter(s) into the space. The purpose of the game is to help children learn to spell. Does anyone know how I could go about dividing the characters equally into the 3 buttons?
Here's a function that would split the word into the amount of segments you want. You can then iterate over that list to set each segment to a button.Text.
public List<string> SplitInSegments(string word, int segments)
{
int wordLength = word.Length;
// The remainder tells us how many segments will get an extra letter
int remainder = wordLength % segments;
// The base length of a segment
// This is a floor division, because we're dividing ints.
// So 5 / 3 = 1
int segmentLength = wordLength / segments;
var result = new List<string>();
int startIndex = 0;
for (int i = 0; i < segments; i++)
{
// This segment may get an extra letter, if its index is smaller then the remainder
int currentSegmentLength = segmentLength + (i < remainder ? 1 : 0);
string currentSegment = word.Substring(startIndex, currentSegmentLength);
// Set the startindex for the next segment.
startIndex += currentSegmentLength;
result.Add(currentSegment);
}
return result;
}
usage:
// returns ["ma", "yb", "e"]
var segments = SplitInSegments("maybe", 3);
Edit
I like the fact that this is for teaching children. So here comes.
Regarding your question on splitting the string based on specific letter sequences: After you've split the string using regex, you will have an array of strings. Then determine the amount of items in the splitted string and concatenate or split further based on the number of segments:
// sequences to split on first
static readonly string[] splitSequences = {
"el",
"ol",
"bo"
};
static readonly string regexDelimiters = string.Join('|', splitSequences.Select(s => "(" + s + ")"));
// Method to split on sequences
public static List<string> SplitOnSequences(string word)
{
return Regex.Split(word, regexDelimiters).Where(s => !string.IsNullOrEmpty(s)).ToList();
}
public static List<string> SplitInSegments(string word, int segments)
{
int wordLength = word.Length;
// The remainder tells us how many segments will get an extra letter
int remainder = wordLength % segments;
// The base length of a segment
// This is a floor division, because we're dividing ints.
// So 5 / 3 = 1
int segmentLength = wordLength / segments;
var result = new List<string>();
int startIndex = 0;
for (int i = 0; i < segments; i++)
{
// This segment may get an extra letter, if its index is smaller then the remainder
int currentSegmentLength = segmentLength + (i < remainder ? 1 : 0);
string currentSegment = word.Substring(startIndex, currentSegmentLength);
// Set the startindex for the next segment.
startIndex += currentSegmentLength;
result.Add(currentSegment);
}
return result;
}
// Splitword will now always return 3 segments
public static List<string> SplitWord(string word)
{
if (word == null)
{
throw new ArgumentNullException(nameof(word));
}
if (word.Length < 3)
{
throw new ArgumentException("Word must be at least 3 characters long", nameof(word));
}
var splitted = SplitOnSequences(word);
var result = new List<string>();
if (splitted.Count == 1)
{
// If the result is not splitted, just split it evenly.
result = SplitInSegments(word, 3);
}
else if (splitted.Count == 2)
{
// If we've got 2 segments, split the shortest segment again.
if (splitted[1].Length > splitted[0].Length
&& !splitSequences.Contains(splitted[1]))
{
result.Add(splitted[0]);
result.AddRange(SplitInSegments(splitted[1], 2));
}
else
{
result.AddRange(SplitInSegments(splitted[0], 2));
result.Add(splitted[1]);
}
}
else // splitted.Count >= 3
{
// 3 segments is good.
result = splitted;
// More than 3 segments, combine some together.
while (result.Count > 3)
{
// Find the shortest combination of two segments
int shortestComboCount = int.MaxValue;
int shortestComboIndex = 0;
for (int i = 0; i < result.Count - 1; i++)
{
int currentComboCount = result[i].Length + result[i + 1].Length;
if (currentComboCount < shortestComboCount)
{
shortestComboCount = currentComboCount;
shortestComboIndex = i;
}
}
// Combine the shortest segments and replace in the result.
string combo = result[shortestComboIndex] + result[shortestComboIndex + 1];
result.RemoveAt(shortestComboIndex + 1);
result[shortestComboIndex] = combo;
}
}
return result;
}
Now when you call the code:
// always returns three segments.
var splitted = SplitWord(word);
Here is another approach.
First make sure that the word can be divided by the desired segments (add a dummy space if necessary) , then use a Linq statement to get your parts and when adding the result trim away the dummy characters.
public static string[] SplitInSegments(string word, int segments)
{
while(word.Length % segments != 0) { word+=" ";}
var result = new List<string>();
for(int x=0; x < word.Count(); x += word.Length / segments)
result.Add((new string(word.Skip(x).Take(word.Length / segments).ToArray()).Trim()));
return result.ToArray();
}
You can split your string into a list and generate buttons based on your list. The logic for splitting the word into a string list would be something similar to this:
string test = "maybe";
List list = new List();
int i = 0, len = 2;
while(i <= test.Length)
{
int lastIndex = test.Length - 1;
list.Add(test.Substring(i, i + len > lastIndex? (i + len) - test.Length : len));
i += len;
}
HTH
I tried implementing GoTo ling in a basic editor-type app but isn't always accurate. More often than not, it gets the right line, but it seems that the more lines there are, the more of a chance it will get the line position wrong and go to the wrong line. Not sure why this isn't working. Can someone please help?
int position = 0;
int lineCount = ((TextBox)tabControl1.SelectedTab.Controls[0]).Lines.Count();
for (int i = 0; i < LineNumber; i++)
{
position += ((TextBox)tabControl1.SelectedTab.Controls[0]).Lines[i].Count();
}
((TextBox)tabControl1.SelectedTab.Controls[0]).Focus();
((TextBox)tabControl1.SelectedTab.Controls[0]).SelectionStart = position;
((TextBox)tabControl1.SelectedTab.Controls[0]).ScrollToCaret();
LineNumber = 0;
position = 0;
lineCount = 0;
I am not sure if I have understood you correctly, but a TextBox control has a method called
TextBoxBase.GetFirstCharIndexFromLine
So if your user wants to go to line 10 (and you have 10 lines) then
int pos = textBox1.GetFirstCharIndexFromLine(9);
textBox1.SelectionStart = pos;
textBox1.ScrollToCaret();
I think #Steve has got you covered with TextBox.GetFirstCharIndexFromLine().
In your original code, though, I think you just needed to account for the carriage return / line feeds at the end of each line (they aren't included when access each line thru the Lines() property). This example assumes the desired line # is 1 (one) based:
int LineNumber = 6;
TextBox TB = (TextBox)tabControl1.SelectedTab.Controls[0];
int position = 0;
for (int i = 1; i <= TB.Lines.Length && i < LineNumber; i++)
{
position += TB.Lines[i - 1].Length + Environment.NewLine.Length;
}
TB.Focus();
TB.SelectionStart = position;
TB.SelectionLength = 0;
TB.ScrollToCaret();
I have a for loop that adds items in an array to a listView.
(It'll grab items on a webpage, remove anything after the ' in the string, then add it to the listView)
The error I am getting is: IndexOutOfRangeException was unhandled- Index was outside the bounds of the array
Here's the code I am using:
string[] aa = getBetweenAll(vid, "<yt:statistics favoriteCount='0' viewCount='", "'/><yt:rating numDislikes='");
for (int i = 0; i < listView1.Items.Count; i++)
{
string input = aa[i];
int index = input.IndexOf("'");
if (index > 0)
input = input.Substring(0, index);
listView1.Items[i].SubItems.Add(input);
}
The error occurs on this line: string input = aa[i];
Anything I did wrong? How can I fix this issue so it'll stop happening? Thanks!
If you're wondering the code for the getBetweenAll method is:
private string[] getBetweenAll(string strSource, string strStart, string strEnd)
{
List<string> Matches = new List<string>();
for (int pos = strSource.IndexOf(strStart, 0),
end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1;
pos >= 0 && end >= 0;
pos = strSource.IndexOf(strStart, end),
end = pos >= 0 ? strSource.IndexOf(strEnd, pos) : -1)
{
Matches.Add(strSource.Substring(pos + strStart.Length, end - (pos + strStart.Length)));
}
return Matches.ToArray();
}
Your looping the elements of 'listView1'
If the item count of listView1 exceeds the number of elements of the string array 'aa' you will get this error.
I would either change the loop to be
for( ..., i < aa.Length, ...)
or inside your for loop put an if statement to make sure that you are not exceeding the elements of aa. (although, I doubt this is what you want to do).
for (int i = 0; i < listView1.Items.Count; i++)
{
if( i < aa.Length)
{
string input = aa[i];
int index = input.IndexOf("'");
if (index > 0)
input = input.Substring(0, index);
listView1.Items[i].SubItems.Add(input);
}
}
Well it is simple, your ListView.Items.Count is bigger then aa.Length. You need to make sure they have the same size.
Your for loop should be changed to
for (int i = 0; i < aa.Length; i++)
Also, when you do the below line, make sure the index matches.
listView1.Items[i].SubItems.Add(input);
Because of your above error, it seems it does not match, You might be better off looping through your list view to find a matching ListView Item and then maniuplate it.
I have a 1-dimensional array that fills up a table of 40 random elements (all the values are either 0 or 1). I want to find the longest consecutive span of values.
For example:
In 111100101 the longest row would be 1111 because it has four consecutive values of 1.
In 011100 the result is 111.
I have no idea how to check upon the "next element" and check if it's a 0 or 1.
Like the first would be 1111 (count 4) but the next would be a 0 value, meaning I have to stop counting.
My idea was placing this value (4) in a other array (example: 111100101), and place the value of the 1's back on zero. And start the process all over again.
To find the largest value I have made another method that checks up the biggest value in the array that keeps track of the count of 0's 1's, this is not the problem.
But I cannot find a way to fill the array tabelLdr up, having all the values of the group of elements of the same kind (being 0 or 1).
In the code below I have 2 if's and of course it will never go into the second if (to check if the next value in the array is != to its current state (being 0 or 1).
public void BerekenDeelrij(byte[] tabel, byte[] tabelLdr)
{
byte LdrNul = 0, Ldréén = 0;
//byte teller = 0;
for (byte i = 0; i < tabel.Length; i++)
{
if (tabel[i] == 0)
{
LdrNul++;
//this 2nd if cleary does not work, but i have no idea how to implend this sort of idea in my program.
if (tabel[i] == 1) //if value != 0 then the total value gets put in the second array tabelLdr,
{
tabelLdr[i] = LdrNul;
LdrNul = 0
}
}
if (tabel[i] == 1)
{
Ldréén++;
if (tabel[i] == 0)
{
tabelLdr[i] = Ldréén;
Ldréén = 0;
}
}
}/*for*/
}
This method should do what you need:
public int LargestSequence(byte[] array) {
byte? last = null;
int count = 0;
int largest = 0;
foreach (byte b in array) {
if (last == b)
++count;
else {
largest = Math.Max(largest, count);
last = b;
count = 1;
}
}
return Math.Max(largest, count);
}
Even while i is the loop counter, it is still just a variable. A valid for statement is for (;;), which is an infinite loop. Notice the for statement increments i, as in i++. The expression i = i + 1 works just as well.
Im unsure if you need the longest "row" of ones or longest row of either 0 or 1. This will work for the latter
var max = 0;
var start = 0;
var current = -1;
var count = 0;
for(int i = 0;i<table.Length;i++)
{
if(current = table[i])
{
count++;
}
else
{
current = table[i];
if(max < count)
{
max = count;
start = i-count;
}
count = 1;
}
}
if(max < count)
{
max = count;
start = i-count;
}
//max is the length of the row starting at start;