テキストボックスのキャレットの位置から行番号を取得しようとしています。これが私が持っているものです。
int Editor::GetLineFromCaret(const std::wstring &text)
{
unsigned int lineCount = 1;
for(unsigned int i = 0; i <= m_editWindow->SelectionStart; ++i)
{
if(text[i] == '\n')
{
++lineCount;
}
}
return lineCount;
}
しかし、私はいくつかの奇妙なエラーを受け取っています。たとえば、テキストボックスに10行のテキストがあり、この関数を使用すると、キャレットが行に約10文字入っていて、一部の行に文字がないために正しくない場合を除いて、正しい行番号が表示されません。
これが私がDamirArhの助けを借りて問題を解決した方法です:
int Editor::GetLineFromCaret(const std::wstring &text)
{
unsigned int lineCount = 1;
unsigned int selectionStart = m_editWindow->SelectionStart;
for(unsigned int i = 0; i <= selectionStart; ++i)
{
if(text[i] == '\n')
{
++lineCount;
++selectionStart;
}
}
return lineCount;
}