Hi, I face a problem with the TextBox RightToLeftChanged event, it is not triggered when the user in runtime choose "Right to left Reading order" from the text box menu. The RightToLeftChanged event should be triggered as I understand, or the "Right to left Reading order" doesn`t trigger it ? If so, how can I know that that user is writing a Right To Left text in my TextBox Code I expected to work:
class CustomTextBox : TextBox
{
protected override void OnRightToLeftChanged(EventArgs e)
{
base.OnRightToLeftChanged(e);
MessageBox.Show("Iam Triggered");
}
}
|
| MHesham Thursday, September 17, 2009 12:30 PM |
WF is not keeping up with the changes in the operating system. You can fix this by overriding WndProc() and watching for the WM_STYLECHANGED message. Then use GetWindowLongPtr to get GWL_EXSTYLE and check if WS_EX_RTLREADING has changed. Visit pinvoke.net for the declarations you'll need.
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorTuesday, September 22, 2009 11:10 AM
-
|
| nobugz Thursday, September 17, 2009 1:05 PM |
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. You'll need to use the RightToLeftEx property and the RightToLeftChangedEx event to detect changes. using System; using System.Windows.Forms; using System.Runtime.InteropServices; class TextBoxEx : TextBox { public event EventHandler RightToLeftChangedEx; private RightToLeft mPrev = RightToLeft.No; public RightToLeft RightToLeftEx { get { int exstyle = GetWindowLong(this.Handle, GWL_EXSTYLE); return (exstyle & WS_EX_RTLREADING) == 0 ? RightToLeft.No : RightToLeft.Yes; } } private void OnRightToLeftChangedEx(EventArgs e) { if (RightToLeftChangedEx != null) RightToLeftChangedEx(this, e); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_STYLECHANGED) { RightToLeft rtl = this.RightToLeftEx; if (rtl != mPrev) { mPrev = rtl; OnRightToLeftChangedEx(EventArgs.Empty); } } } [DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr hWnd, int index); private const int GWL_EXSTYLE = -20; private const int WM_STYLECHANGED = 0x7d; private const int WS_EX_RTLREADING = 0x2000; }
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorTuesday, September 22, 2009 11:10 AM
-
|
| nobugz Saturday, September 19, 2009 7:17 PM |
It will be trigrred if Previous value is different with Changed one. Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
|
| _SuDhiR_ Thursday, September 17, 2009 12:53 PM |
WF is not keeping up with the changes in the operating system. You can fix this by overriding WndProc() and watching for the WM_STYLECHANGED message. Then use GetWindowLongPtr to get GWL_EXSTYLE and check if WS_EX_RTLREADING has changed. Visit pinvoke.net for the declarations you'll need.
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorTuesday, September 22, 2009 11:10 AM
-
|
| nobugz Thursday, September 17, 2009 1:05 PM |
Thanx alot, I understod the reason now, I saw the link but I found it hard on me, actually I have never dealt with messages, and I am not API Geek Yet, I think I should be soon. Can you write for me a fast code in the WndProc of the TextBox ? to listen for RightToLeft changings I couldnt write more than:
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_STYLECHANGED:
//here I should write code I think
break;
}
base.WndProc(ref m);
}
- Edited byMHesham Thursday, September 17, 2009 1:23 PMAttach code
-
|
| MHesham Thursday, September 17, 2009 1:20 PM |
You are not going to become a Master Geek if you don't try. 15 minutes is not enough.
Hans Passant. |
| nobugz Thursday, September 17, 2009 6:45 PM |
well , i really need that part soon , and iam really stuck on this with alot of work ! , i would be grateful if u leave becoming a Geek a delayed task :D |
| MHesham Friday, September 18, 2009 8:23 PM |
Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. You'll need to use the RightToLeftEx property and the RightToLeftChangedEx event to detect changes. using System; using System.Windows.Forms; using System.Runtime.InteropServices; class TextBoxEx : TextBox { public event EventHandler RightToLeftChangedEx; private RightToLeft mPrev = RightToLeft.No; public RightToLeft RightToLeftEx { get { int exstyle = GetWindowLong(this.Handle, GWL_EXSTYLE); return (exstyle & WS_EX_RTLREADING) == 0 ? RightToLeft.No : RightToLeft.Yes; } } private void OnRightToLeftChangedEx(EventArgs e) { if (RightToLeftChangedEx != null) RightToLeftChangedEx(this, e); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_STYLECHANGED) { RightToLeft rtl = this.RightToLeftEx; if (rtl != mPrev) { mPrev = rtl; OnRightToLeftChangedEx(EventArgs.Empty); } } } [DllImport("user32.dll")] private static extern int GetWindowLong(IntPtr hWnd, int index); private const int GWL_EXSTYLE = -20; private const int WM_STYLECHANGED = 0x7d; private const int WS_EX_RTLREADING = 0x2000; }
Hans Passant.- Marked As Answer byLing WangMSFT, ModeratorTuesday, September 22, 2009 11:10 AM
-
|
| nobugz Saturday, September 19, 2009 7:17 PM |