C#, .NET
UI 프로그래밍 ( 컨트롤에 focus 주기, textbox 스크롤바 가장 하단으로 내리기 등)
휘사마
2011. 6. 17. 19:33
결론부터 이야기 하자면 이렇다.
1.Win32 API 함수가 최고!!!
2.다른 쓰레드에서 메인 쓰레드의 Control들을 접근할시엔 InvokeRequired 속성과 Invoke()를 사용하자.
2번을 한다 해도 Control.Focus() 나 Control.Select() 등의 함수가 작동하지 않을 수 있다.
이럴땐 Win32 API 함수를 쓰자..
private const int WM_VSCROLL = 0x115;
private const int SB_BOTTOM = 7;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SetFocus(IntPtr hWnd);
// Scroll to the bottom, but don't move the caret position.
SendMessage(tb.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, IntPtr.Zero);
//control에 focus 주기
SetFocus(textbox.Handle);