Discussion:
TextBoxにおいてキャレットの位置と選択範囲を制御したい
(too old to reply)
Takeshi SHIGIHARA
2006-06-07 05:38:18 UTC
Permalink
$B4D6-$O(BVisual Studio .NET 2003$B!"(BC# 1.1 $B$G$9!#(B
$B8=:_$N$H$3$m!"(BWindows(2K,XP)$B0J30$G$NF0:n$O9M$($F$$$^$;$s!#(B

$B<ALd(B
TextBox$B$K$*$$$F!"%-%c%l%C%H0LCV$r;XDj$7!"$=$3$+$i7h$^$C$?(B
$BJ}8~$KA*Br$r5/$3$7$?$$$N$G$9$,!"$=$NJ}K!$O!)(B

$B>\:Y(B
System.Windows.Forms.TextBox$B$K$*$$$F!"J8;zNs(B ABCDEFGHI $B$r(B
$BI=<($7$F$$$k$b$N$H$7$^$9!#(B
$B$3$3$G!"(B E$B$H(BF$B$N4V$K%-%c%l%C%H$rCV$-!"KvHx$^$G!"$D$^$j(BFGHI
$B$rA*Br>uBV$H$7$?$j!"5U$K(B BCDE $B$rA*Br>uBV$H$7$?$$$N$G$9$,(B
$B$I$&$K$b$d$jJ}$,J,$+$i$:$K:$$C$F$$$^$9!#(B

Win32/SDK$B$G$N(B EM_SETSEL $B$N$h$&$K!"A*Br3+;O0LCV$H=*N;0LCV$r(B
$BM?$($kJ}K!$,$"$l$PNI$$$N$G$9$,!"(BTextBox$B$G;HMQ$G$-$=$&$J(B
$B4X?t!"%W%m%Q%F%#$O(B
SelectionLength $B%W%m%Q%F%#(B
SelectionStart $B%W%m%Q%F%#(B
SelectedText $B%W%m%Q%F%#(B
Select(int start, int length) $B%a%=%C%I(B
$B$J$I$,8+Ev$?$k$P$+$j$G!"$I$&$b;W$C$?$h$&$KA*Br$9$kJ}8~$r(B
$B@)8f$G$-$=$&$K$"$j$^$;$s!#(B

static readonly uint EM_SETSEL = 0x00B1;

[System.Runtime.InteropServices.DllImport("User32.dll")]
static extern UInt32 SendMessage(IntPtr hwnd, uint msg, uint wparam, uint lparam);

public void SetSel(uint start, uint end)
{
SendMessage(this.Handle, EM_SETSEL, start, end);
}

$B$N$h$&$K$G$b$7$F!"(BWindows$B5!G=$rD>@\8F$S=P$9$[$+$O$J$$(B
$B$N$G$7$g$&$+!#(B


----- Takeshi SHIGIHARA
***@tka.att.ne.jp
***@po.jah.ne.jp -----
$B$&$^$$!"Aa$$!"0B$$!?#2$D$@$1A*$Y(B--RFC1925.
Takeshi SHIGIHARA
2006-06-07 06:08:05 UTC
Permalink
$BJdB-$7$^$9!#(B
Post by Takeshi SHIGIHARA
$B$3$3$G!"(B E$B$H(BF$B$N4V$K%-%c%l%C%H$rCV$-!"KvHx$^$G!"$D$^$j(BFGHI
$B$rA*Br>uBV$H$7$?$j!"5U$K(B
$BA*Br%W%m%0%i%`$N<B9T8e$O!"0J2<$N$h$&$K(BE$B$H(BF$B$N4V$K%-%c%l%C%H$,(B
$B$"$j!"(BFGHI$B$NJ8;z$,6/D4I=<($7$F$$$k>uBV$K$7$?$$$N$G$9!#(B

A B C D E|F G H I


----- Takeshi SHIGIHARA
***@tka.att.ne.jp
***@po.jah.ne.jp -----
$B$&$^$$!"Aa$$!"0B$$!?#2$D$@$1A*$Y(B--RFC1925.
UETA, Shin-ichi
2006-06-07 06:27:18 UTC
Permalink
$B$3$s$K$A$O!"?"ED$G$9!#(B
Post by Takeshi SHIGIHARA
$BA*Br%W%m%0%i%`$N<B9T8e$O!"0J2<$N$h$&$K(BE$B$H(BF$B$N4V$K%-%c%l%C%H$,(B
$B$"$j!"(BFGHI$B$NJ8;z$,6/D4I=<($7$F$$$k>uBV$K$7$?$$$N$G$9!#(B
A B C D E|F G H I
$B$I$&$7$F$b%-%c%l%C%H$N0LCV$,A*BrHO0O$N1&C<$KMh$F$7$^$$$^$9$M(B...$B!#(B

TextBox.Select()$B$N(Blength$B$KIi?t$rM?$($k$HA*BrHO0O$,:8$K?-$S$k(B
$B$b$N$N!"%-%c%l%C%H$O(Bstart$B$N0LCV$K$H$I$^$j$^$9!#(B
length$B$***@5?t$K$9$k$H%-%c%l%C%H$,A*BrHO0O$N1&C<$K0\F0$9$k$N$K!#(B

TextBox.SelectionLength$B$,Ii?t$r<u$1IU$1$J$$$H$3$m$r$_$k$H!"(B
$BFbItE*$K$OA*BrHO0O$r(Bnormalize$B$7$h$&$H$7$F$$$k$N$@$m$&$+(B...?

$B4D6-!'(BWinXPSP2 + .NET 2.0 + C#2005 Exp.
--
$B?"ED%7%9%F%`@_7W;vL3=j(B
Ueta System Design Studio
http://www.usdesign.jp/
$B?"ED??0l(B
mailto:***@usdesign.jp
Takeshi SHIGIHARA
2006-06-07 09:48:25 UTC
Permalink
$BAGAa$$$4JVEz$"$j$,$H$&$4$6$$$^$9!#(B
Post by UETA, Shin-ichi
TextBox.Select()$B$N(Blength$B$KIi?t$rM?$($k$HA*BrHO0O$,:8$K?-$S$k(B
$B$b$N$N!"%-%c%l%C%H$O(Bstart$B$N0LCV$K$H$I$^$j$^$9!#(B
$B$3$l$O(B.NET 2.0$B$N5!G=$G$7$g$&$+!#$$$$$J$"!#(B
.NET 1.1$B$G$O!"(BTextBox.Select(start, length)$B$N(Blength$B$KIi$N?t$r(B
$BF~$l$k$HNc30(B
ArgumentException
start $B%Q%i%a!<%?$^$?$O(B length $B%Q%i%a!<%?$KBeF~$5$l$?CM$,(B 0 $BL$K~$G$9!#(B
$B$3$A$i$N4D6-$G(B EM_SETSEL $B$r8F$S=P$9(BP/Invoke$B$r;n$7$F$_$^$7$?$,(B
start=9,end=5$B$K$7$h$&$,(Bstart=5,end=9 $B$K$7$h$&$,!"$$$:$l$N>l9g$b(B
$B%-%c%l%C%H$OKvHx$G(BFGHI$B$,A*Br$5$l$k>u67$G$9!#(B

$B$*$+$7$$$J$H;W$$!"(B.NET$B$G$J$/(BSDK$B%M%$%F%#%V$N%W%m%8%'%/%H$r:***@.$7(B
SendMessaage(hEdit, EM_SETSEL , 9, 5);
$B$H$7$F$_$F$b!"$d$C$Q$j%-%c%l%C%H$,KvHx$KMh$k;OKv!#(B
$B$I$&$b!"$J$K$+Bg$-$J4V0c$$$H$+4*0c$$$H$+$r5/$3$7$F$$$k$h$&$G$9!#(B


----- Takeshi SHIGIHARA
***@tka.att.ne.jp
***@po.jah.ne.jp -----
$B$&$^$$!"Aa$$!"0B$$!?#2$D$@$1A*$Y(B--RFC1925.
Takeshi SHIGIHARA
2006-06-07 10:02:59 UTC
Permalink
Post by Takeshi SHIGIHARA
SendMessaage(hEdit, EM_SETSEL , 9, 5);
$B$H$7$F$_$F$b!"$d$C$Q$j%-%c%l%C%H$,KvHx$KMh$k;OKv!#(B
$B$"$s$.$c!#(BMSDN$B$rFI$_Ht$P$7$F$$$?$i$7$$!#$-$C$H!"@x:_0U<1$,FI$_$?$/(B
$B$J$+$C$?J8>O$r<+F0E*$KHt$P$7$?$s$@!#(B

MSDN$B$K$h$l$P(B EM_SETSEL $B$NKvHx$K!"$3$s$JCp<a$,$"$j$^$7$?!#(B
Edit controls: The control displays a flashing caret at the end position
regardless of the relative values of start and end.

$B$3$j$c$^$$$C$?!#(BEdit control$B$r;H$C$F$k8B$j!"%-%c%l%C%H$r(BEM_SETSEL
$B$G!"$3$A$i$NA[Dj$7$F$$$k>l=j$K0\F0$5$;$k$3$H$OL5M}$N$h$&$G$9!#(B


----- Takeshi SHIGIHARA
***@tka.att.ne.jp
***@po.jah.ne.jp -----
$B$&$^$$!"Aa$$!"0B$$!?#2$D$@$1A*$Y(B--RFC1925.
UETA, Shin-ichi
2006-06-07 10:22:14 UTC
Permalink
$B$I$&$b!"?"ED$G$9!#(B
Post by Takeshi SHIGIHARA
.NET 1.1$B$G$O!"(BTextBox.Select(start, length)$B$N(Blength$B$KIi$N?t$r(B
$BF~$l$k$HNc30(B
$B$"$i$i!"$3$s$J:Y$+$$$H$3$m$K0c$$$,$"$j$^$7$?$+(B...$B!#(B

$BIi?t$N;XDj$r<u$1IU$1$k$h$&$K$J$C$?$H$$$&$3$H$O!"$=$l$J$j$K(B
$B<{MW$,$"$C$?$N$+$J(B?
Post by Takeshi SHIGIHARA
$B$3$j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