Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查光标是否位于文本框中的空行?_C#_Winforms_Textbox - Fatal编程技术网

C# 如何检查光标是否位于文本框中的空行?

C# 如何检查光标是否位于文本框中的空行?,c#,winforms,textbox,C#,Winforms,Textbox,我尝试使用以下代码在文本框中查找空(或空白)行,但我不知道如何查找插入符号位置之前的第一个换行符。我怎样才能解决这个问题?或者有没有更短的解决方案 int i = textBox1.SelectionStart; var endOfLine = textBox1.Text.IndexOf(Environment.NewLine,i); /* the next line should find the first occurance of newline before the i po

我尝试使用以下代码在文本框中查找空(或空白)行,但我不知道如何查找插入符号位置之前的第一个
换行符。我怎样才能解决这个问题?或者有没有更短的解决方案

int i = textBox1.SelectionStart;
var endOfLine = textBox1.Text.IndexOf(Environment.NewLine,i);

 /* the next line should find the first occurance 
   of newline before the i postion, which method does that? */
var beginOfLine = textBox1.Text.XXX(Environment.NewLine,i); 

bool isEmpty = textBox1.Text.Substring(
                   beginOfLine,
                   endOfLine-beginofLine).Trim() == "";

使用此
String.LastIndexOf
重载查找特定索引之前字符串的最后一个索引。

您要查找的代码行是:

var begin = textBox1.Text.LastIndexOf(Environment.NewLine,i); 

使用此
String.LastIndexOf
重载查找特定索引之前字符串的最后一个索引。

您要查找的代码行是:

var begin = textBox1.Text.LastIndexOf(Environment.NewLine,i); 

使用此
String.LastIndexOf
重载查找特定索引之前字符串的最后一个索引。

您要查找的代码行是:

var begin = textBox1.Text.LastIndexOf(Environment.NewLine,i); 

使用此
String.LastIndexOf
重载查找特定索引之前字符串的最后一个索引。

您要查找的代码行是:

var begin = textBox1.Text.LastIndexOf(Environment.NewLine,i); 

首先获取光标之前的文本:

string textPriorToCursor = textBox1.Text.Substring(0, textBox1.Text.Length - i);
然后获取光标之前文本中最后出现的新行:

var begin = textPriorToCursor.LastIndexOf(Environment.NewLine);
将上述两行合并为一行将如下所示:

var begin = textBox1.Text.Substring(0, textBox1.Text.Length - i).LastIndexOf(Environment.NewLine);

首先获取光标之前的文本:

string textPriorToCursor = textBox1.Text.Substring(0, textBox1.Text.Length - i);
然后获取光标之前文本中最后出现的新行:

var begin = textPriorToCursor.LastIndexOf(Environment.NewLine);
将上述两行合并为一行将如下所示:

var begin = textBox1.Text.Substring(0, textBox1.Text.Length - i).LastIndexOf(Environment.NewLine);

首先获取光标之前的文本:

string textPriorToCursor = textBox1.Text.Substring(0, textBox1.Text.Length - i);
然后获取光标之前文本中最后出现的新行:

var begin = textPriorToCursor.LastIndexOf(Environment.NewLine);
将上述两行合并为一行将如下所示:

var begin = textBox1.Text.Substring(0, textBox1.Text.Length - i).LastIndexOf(Environment.NewLine);

首先获取光标之前的文本:

string textPriorToCursor = textBox1.Text.Substring(0, textBox1.Text.Length - i);
然后获取光标之前文本中最后出现的新行:

var begin = textPriorToCursor.LastIndexOf(Environment.NewLine);
将上述两行合并为一行将如下所示:

var begin = textBox1.Text.Substring(0, textBox1.Text.Length - i).LastIndexOf(Environment.NewLine);

要使一行为空,光标位置必须位于文本的开头,或者在其前面必须有换行符,光标位置必须位于文本的末尾,或者在其后面必须有换行符:

string s = textBox1.Text;
int i = textBox1.SelectionStart;

string start = s.Substring(0, i).TrimEnd(' ', '\t');
string end = s.Substring(i, s.Length - i).TrimStart(' ', '\t');
bool isEmpty = (start.Length == 0 || start.EndsWith(Environment.NewLine)) &&
               (end.Length == 0 || end.StartsWith(Environment.NewLine));

为了处理空格和制表符,将从起点和终点对其进行修剪。请注意,我明确指定了要修剪的字符,否则CRs和LFs也将被修剪。请注意,有许多空白字符,请参见。

要使一行为空,光标位置必须位于文本的开头,或者必须在其前面有换行符,并且光标位置必须位于文本的末尾,或者必须紧跟在换行符之后:

string s = textBox1.Text;
int i = textBox1.SelectionStart;

string start = s.Substring(0, i).TrimEnd(' ', '\t');
string end = s.Substring(i, s.Length - i).TrimStart(' ', '\t');
bool isEmpty = (start.Length == 0 || start.EndsWith(Environment.NewLine)) &&
               (end.Length == 0 || end.StartsWith(Environment.NewLine));

为了处理空格和制表符,将从起点和终点对其进行修剪。请注意,我明确指定了要修剪的字符,否则CRs和LFs也将被修剪。请注意,有许多空白字符,请参见。

要使一行为空,光标位置必须位于文本的开头,或者必须在其前面有换行符,并且光标位置必须位于文本的末尾,或者必须紧跟在换行符之后:

string s = textBox1.Text;
int i = textBox1.SelectionStart;

string start = s.Substring(0, i).TrimEnd(' ', '\t');
string end = s.Substring(i, s.Length - i).TrimStart(' ', '\t');
bool isEmpty = (start.Length == 0 || start.EndsWith(Environment.NewLine)) &&
               (end.Length == 0 || end.StartsWith(Environment.NewLine));

为了处理空格和制表符,将从起点和终点对其进行修剪。请注意,我明确指定了要修剪的字符,否则CRs和LFs也将被修剪。请注意,有许多空白字符,请参见。

要使一行为空,光标位置必须位于文本的开头,或者必须在其前面有换行符,并且光标位置必须位于文本的末尾,或者必须紧跟在换行符之后:

string s = textBox1.Text;
int i = textBox1.SelectionStart;

string start = s.Substring(0, i).TrimEnd(' ', '\t');
string end = s.Substring(i, s.Length - i).TrimStart(' ', '\t');
bool isEmpty = (start.Length == 0 || start.EndsWith(Environment.NewLine)) &&
               (end.Length == 0 || end.StartsWith(Environment.NewLine));

为了处理空格和制表符,将从起点和终点对其进行修剪。请注意,我明确指定了要修剪的字符,否则CRs和LFs也将被修剪。请注意,有许多空白字符,请参见。

您可以检查插入符号所在的行,并查看其是否为空:

if ((tBox.Lines.Length == 0 && tBox.Text.Trim() == "") ||
   (tBox.Lines[tBox.GetLineFromCharIndex(tBox.SelectionStart)].Trim() == "")) {
  // caret is on an empty line
}

您可以检查插入符号所在的行,并查看其是否为空:

if ((tBox.Lines.Length == 0 && tBox.Text.Trim() == "") ||
   (tBox.Lines[tBox.GetLineFromCharIndex(tBox.SelectionStart)].Trim() == "")) {
  // caret is on an empty line
}

您可以检查插入符号所在的行,并查看其是否为空:

if ((tBox.Lines.Length == 0 && tBox.Text.Trim() == "") ||
   (tBox.Lines[tBox.GetLineFromCharIndex(tBox.SelectionStart)].Trim() == "")) {
  // caret is on an empty line
}

您可以检查插入符号所在的行,并查看其是否为空:

if ((tBox.Lines.Length == 0 && tBox.Text.Trim() == "") ||
   (tBox.Lines[tBox.GetLineFromCharIndex(tBox.SelectionStart)].Trim() == "")) {
  // caret is on an empty line
}

查找光标位置前后的换行符索引如果中间的字符串为空,则该行为空,对于换行符索引为-1的第一行和最后一行,使用0和string.length替换它们

   int i = textBox1.SelectionStart;
   var s = textBox1.Text;

   var start = s.LastIndexOf(Environment.NewLine, i);  
   var end = s.IndexOf(Environment.NewLine, i);

   start = start >= 0 ? start : 0;
   end = end >= 0 ? end : s.Length;

   bool isEmpty = s.Substring(start, end - start).Trim(' ', '\t') == "";

查找光标位置前后的换行符索引如果中间的字符串为空,则该行为空,对于换行符索引为-1的第一行和最后一行,使用0和string.length替换它们

   int i = textBox1.SelectionStart;
   var s = textBox1.Text;

   var start = s.LastIndexOf(Environment.NewLine, i);  
   var end = s.IndexOf(Environment.NewLine, i);

   start = start >= 0 ? start : 0;
   end = end >= 0 ? end : s.Length;

   bool isEmpty = s.Substring(start, end - start).Trim(' ', '\t') == "";

查找光标位置前后的换行符索引如果中间的字符串为空,则该行为空,对于换行符索引为-1的第一行和最后一行,使用0和string.length替换它们

   int i = textBox1.SelectionStart;
   var s = textBox1.Text;

   var start = s.LastIndexOf(Environment.NewLine, i);  
   var end = s.IndexOf(Environment.NewLine, i);

   start = start >= 0 ? start : 0;
   end = end >= 0 ? end : s.Length;

   bool isEmpty = s.Substring(start, end - start).Trim(' ', '\t') == "";

查找光标位置前后的换行符索引如果中间的字符串为空,则该行为空,对于换行符索引为-1的第一行和最后一行,使用0和string.length替换它们

   int i = textBox1.SelectionStart;
   var s = textBox1.Text;

   var start = s.LastIndexOf(Environment.NewLine, i);  
   var end = s.IndexOf(Environment.NewLine, i);

   start = start >= 0 ? start : 0;
   end = end >= 0 ? end : s.Length;

   bool isEmpty = s.Substring(start, end - start).Trim(' ', '\t') == "";

这是我的尝试,假设文本框的行结尾是\r\n

private static bool IsCursorOnEmptyLine(TextBox targetTextBox)
{
    var cursorPosition = targetTextBox.SelectionStart;
    var positionBefore = targetTextBox.Text.LastIndexOf('\n', cursorPosition == 0 ? 0 : cursorPosition - 1);
    var positionAfter  = targetTextBox.Text.IndexOf('\r', cursorPosition);
    if (positionBefore == -1) positionBefore = 0;
    if (positionAfter  == -1) positionAfter  = targetTextBox.Text.Length;
    return targetTextBox.Text.Substring(positionBefore, positionAfter - positionBefore).Trim() == "";
}
用法示例:

Debug.Print("Empty Line: " + (IsCursorOnEmptyLine(myTextBox) ? "Yes" : "No"));

这是我的尝试,假设文本框的行结尾是\r\n

private static bool IsCursorOnEmptyLine(TextBox targetTextBox)
{
    var cursorPosition = targetTextBox.SelectionStart;
    var positionBefore = targetTextBox.Text.LastIndexOf('\n', cursorPosition == 0 ? 0 : cursorPosition - 1);
    var positionAfter  = targetTextBox.Text.IndexOf('\r', cursorPosition);
    if (positionBefore == -1) positionBefore = 0;
    if (positionAfter  == -1) positionAfter  = targetTextBox.Text.Length;
    return targetTextBox.Text.Substring(positionBefore, positionAfter - positionBefore).Trim() == "";
}
用法示例:

Debug.Print("Empty Line: " + (IsCursorOnEmptyLine(myTextBox) ? "Yes" : "No"));

这是我的尝试,假设文本框的行结尾是\r\n

private static bool IsCursorOnEmptyLine(TextBox targetTextBox)
{
    var cursorPosition = targetTextBox.SelectionStart;
    var positionBefore = targetTextBox.Text.LastIndexOf('\n', cursorPosition == 0 ? 0 : cursorPosition - 1);
    var positionAfter  = targetTextBox.Text.IndexOf('\r', cursorPosition);
    if (positionBefore == -1) positionBefore = 0;
    if (positionAfter  == -1) positionAfter  = targetTextBox.Text.Length;
    return targetTextBox.Text.Substring(positionBefore, positionAfter - positionBefore).Trim() == "";
}
用法示例:

Debug.Print("Empty Line: " + (IsCursorOnEmptyLine(myTextBox) ? "Yes" : "No"));

这是我的尝试,假设文本框的行结尾是\r\n

private static bool IsCursorOnEmptyLine(TextBox targetTextBox)
{
    var cursorPosition = targetTextBox.SelectionStart;
    var positionBefore = targetTextBox.Text.LastIndexOf('\n', cursorPosition == 0 ? 0 : cursorPosition - 1);
    var positionAfter  = targetTextBox.Text.IndexOf('\r', cursorPosition);
    if (positionBefore == -1) positionBefore = 0;
    if (positionAfter  == -1) positionAfter  = targetTextBox.Text.Length;
    return targetTextBox.Text.Substring(positionBefore, positionAfter - positionBefore).Trim() == "";
}
用法示例:

Debug.Print("Empty Line: " + (IsCursorOnEmptyLine(myTextBox) ? "Yes" : "No"));

不能使用
string.IndexOf('\n',i)?我用它来查找行尾,但如何查找行首?如果需要查找行首,可以使用
string.LastIndexOf()
,如下面Bradley的答案所示。这将从
i
向后工作,直到找到
\n
。如果你在第一行,可能什么也找不到。不好意思,谢谢,我想它会从字符串的末尾搜索到iCan,你不能使用
string.IndexOf('\n',I)?我用它来查找行尾,但如何查找行首?如果需要查找行首,可以使用
string.LastIndexOf()
,如下面Bradley的答案所示。这将从