• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

Urgent... .RichTextBox ,Changing direction to RTL

Hello

How to change writing direction to right-to-left programmatically in RichTextBox
manually if I press right Ctrl+Shift ,the direction will be changed but how to automate it.
I have tried SendKeys but I don't know how to pass R.Ctrl Key.
what about API ,can I do it using one of API Declaration

best regards
0
fadloun
Asked:
fadloun
  • 5
  • 4
1 Solution
 
List244Commented:
Do you mean:
RichTextBox1.SelAlignment = 1
?
0
 
BrownheadCommented:
I'm not sure what your asking... but heres what you suggested.. it simulates the keystrokes for you.

'//Code\\
'This code requires nothing
keybd_event VK_CONTROL, 0, 0, 0 'Press down the control key
keybd_event 16, 0, &H1, 0 'Press down the shift key
keybd_event VK_RIGHT, 0, 0, 0 'Press down the right key
keybd_event VK_CONTROL, 0, &H2, 0 'Release the control key
keybd_event 16, 0, &H1 Or &H2, 0 'Release the shift key
keybd_event VK_RIGHT, 0, &H2, 0 'Release the right key
'\\Code//

Does this help?
0
 
BrownheadCommented:
Oh, and heres the constants you'll need.

'//Code\\
'This code requires nothing. Place it in the declarations section of your code
Const VK_CONTROL As Long = &H11
Const VK_RIGHT As Long = &H27
Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
'\\Code//
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
fadlounAuthor Commented:
Hello All
List
No ,SelAlignment will set alignment only,what I am looking for is setting Text alignment to Right side+(Insertion Right-To-Left) ,
 Arabic + Hebrew are RTL Language ,if you write in a textbox using english +arbaic Language setting direction to RTL is a Must to display the paragraph properly.
in normal VB Textbox this property available by defualt ,

BrownHead
I have tried your code ,but it didn't work ... nothing happen
I replaced shift para with Rshift
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  keybd_event(VK_RSHIFT, 0, 0, 0)
  keybd_event(VK_CONTROL, 0, 0, 0)
  keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
  keybd_event(VK_RSHIFT, 0, KEYEVENTF_KEYUP, 0)
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
this code doesn't do what actually pressing(Rshift+Control) DO


Regards
0
 
BrownheadCommented:
When I press the Right Shift key on my keyboad, and the control key, and then type things in, nothing changes... thus i'm still not sure what your trying to accomplish. Maybe someone else does though. And the VK_Right is the right directional pad key.. which is what I thought you said, but now I see I misinterpreted it. 16 is actually the keycode for shift...
0
 
fadlounAuthor Commented:
if you have bilingual System,English + Rtl Language( Arabic or Hebrew) enabled
pressing Ctrl + Rshift will do the following
 change the language to To arabic
 align the text to right
 correct insert direction to RTL
Pressing Ctrl +LShift will reverse this

regards
0
 
BrownheadCommented:
Let me take another wack at simulating the keystrokes... try this:

'//Code\\
'This code requires nothing
keybd_event &H11, 0, 0, 0 'Press down the control key
keybd_event &HA1, 0, 0, 0 'Press down the shift key
DoEvents
keybd_event &H11, 0, &H2, 0 'Press down the control key
keybd_event &HA1, 0, &H2, 0 'Press down the shift key
'\\Code//
0
 
fadlounAuthor Commented:
Your code works very fine on normal textbox
but on RichTextBox it did not. ....I am confused
0
 
BrownheadCommented:
Hehe, I should have just used google. Found an answer in 30 seconds of searching. The code was extremly badly spaced and was nearly impossible to read and execute, but I fixed it up for you, see if this is wat your looking for, worked perfectly for me :D

'//Code\\
'This code requires nothing, place it in a module.
Option Explicit
Public Const WM_USER = &H400
Public Const EM_SETPARAFORMAT = WM_USER + 71
Public Const EM_SETBIDIOPTIONS = WM_USER + 200
Public Const PFM_DIR = &H10000 'Direction mask bit
Public Const PFE_RTLPAR = &H1 'RTL paragraph style bit
Public Const BOM_DEFPARADIR = &H1 'Default direction mask
Public Const BOE_RTLDIR = &H1 'Default RTL para style
Type vbParaFormat
    cbSize As Long
    dwMask As Long
    wNumbering As Integer
    wEffects As Integer
    dxStrtIndent As Long
    dxRightIndent As Long
    dxOffset As Long
    wAlignment As Integer
    cTabCount As Integer
    rgxTabs(31) As Long
End Type
Public vbPF As vbParaFormat
Public Declare Function SendPFMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As vbParaFormat) As Long
Type vbBiDiOptions
    cbSize As Long
    wMask As Integer
    wEffects As Integer
End Type
Public vbBO As vbBiDiOptions
Public Declare Function SendBOMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As vbBiDiOptions) As Long

Public Function SetParaDirection(Direction As Integer) As Boolean
vbPF.cbSize = LenB(vbPF) 'Size
vbPF.dwMask = PFM_DIR 'Attribute to set
vbPF.wEffects = Direction 'New direction
SetParaDirection = SendPFMessage(Form1.RichTextBox1.hWnd, EM_SETPARAFORMAT, 0, vbPF)
End Function
'\\Code//

'//Code\\
'This code requires a rich text box control. Buttons named btnLTR and btnRTL are supported but not necassarily needed
Option Explicit
Private Sub btnLTR_Click()
SetParaDirection (Not PFE_RTLPAR)
RichTextBox1.SetFocus
End Sub
Private Sub btnRTL_Click()
SetParaDirection (PFE_RTLPAR)
RichTextBox1.SetFocus
End Sub

Private Sub Form_Load()
Dim Result As Long
SetParaDirection (PFE_RTLPAR)
vbBO.cbSize = LenB(vbBO) 'Size
vbBO.wMask = BOM_DEFPARADIR 'Attribute to set
vbBO.wEffects = BOE_RTLDIR 'Default direction
Result = SendBOMessage(RichTextBox1.hWnd, EM_SETBIDIOPTIONS, 0, vbBO)
RichTextBox1.RightMargin = 2000
End Sub
'\\Code//

I hope this works, G'day to ya :D
0
 
fadlounAuthor Commented:
YES YES YES
BrownHead ,thank you very much. You saved me
goodDay to you too

best regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now