C#中如何获取textbox当前所在的行号
发布网友
发布时间:2024-10-24 02:13
我来回答
共4个回答
热心网友
时间:2024-10-30 15:59
austinlei
在form中放一个textbox两个label
const em_getsel = &hb0
const em_linefromchar = &hc9
const em_lineindex = &hbb
private declare function sendmessage lib "user32" alias "sendmessagea" _
(byval hwnd as long, byval wmsg as long, byval wparam as long, _
lparam as any) as long
public sub getcaretpos(byval hwnd5 as long, lineno as long, colno as long)
dim i as long, j as long
dim lparam as long, wparam as long
dim k as long
i = sendmessage(hwnd5, em_getsel, wparam, lparam)
j = i / 2 ^ 16 取得目前caret所在前面有多少个byte
lineno = sendmessage(hwnd5, em_linefromchar, j, 0) 取得前面有多少行
lineno = lineno + 1
k = sendmessage(hwnd5, em_lineindex, -1, 0)
取得目前caret所在行前面有多少个byte
colno = j - k + 1
end sub
private sub form_load()
dim lineno as long, colno as long
call getcaretpos(text1.hwnd, lineno, colno)
label1.caption = lineno
label2.caption = colno
end sub
private sub text1_keyup(keycode as integer, shift as integer)
dim lineno as long, colno as long
call getcaretpos(text1.hwnd, lineno, colno)
label1.caption = lineno
label2.caption = colno
end sub
private sub text1_mousedown(button as integer, shift as integer, x as single, y as single)
dim lineno as long, colno as long
call getcaretpos(text1.hwnd, lineno, colno)
label1.caption = lineno
label2.caption = colno
end sub
热心网友
时间:2024-10-30 16:00
也就是从0行到当前行的"\n"的个数! 用
TextBox.Text.Substring(0,TextBox.SelectionStart).Split('\n').Length
热心网友
时间:2024-10-30 16:00
textBox1.GetLineFromCharIndex(textBox1.GetFirstCharIndexOfCurrentLine())
热心网友
时间:2024-10-30 16:01
FocusedNumber或FocusValue属性,你试下吧