VB.NET How can I change cursor direction to right-to-left or left-to-right? - vb.net

I have a vb.net form that containing 2 richtextbox. The first richtextbox in English and the second in Arabic. How can I change the cursor direction so that its direction turns to the right when entering the Arab richtextbox and to the left when entering the English richtextbox??

All controls have a property named RightToLeft which is used to dictate the direction of text entry.
Here it is as described in the docs:
Gets or sets a value indicating whether control's elements are aligned
to support locales using right-to-left fonts.
In the case of your RichTextBoxes, either set the property in the Properties window in the Form editor, or programatically like this:
EnglishRichTextBox.RightToLeft = RightToLeft.No
ArabicRichTextBox.RightToLeft = RightToLeft.Yes
If the RichTextBoxes are dedicated for English or Arabic input, you should set them at design time. There is a side effect of changing the value at runtime (in code), which is detailed in the docs:
If the value of the RightToLeft property is changed at run time, only
raw text without formatting is preserved.

Perhaps you mean changing the input language on enter a RichTextBox control? If that's what you are after, then you need to handle the Enter event of each RTB to switch the language through the InputLanguage class. The class has static properties to get the installed input languages, their cultures, the default and the current input languages.
Add Enter event handler for each RTB and handle them as follows:
The English Language RTB
Private Sub enRTB_Enter(sender As Object, e As EventArgs) Handles enRTB.Enter
Dim lang = InputLanguage.InstalledInputLanguages.
Cast(Of InputLanguage).
FirstOrDefault(Function(x) x.Culture.TwoLetterISOLanguageName = "en")
If lang IsNot Nothing Then InputLanguage.CurrentInputLanguage = lang
End Sub
The Arabic Language RTB
Private Sub arRTB_Enter(sender As Object, e As EventArgs) Handles arRTB.Enter
Dim lang = InputLanguage.InstalledInputLanguages.
Cast(Of InputLanguage).
FirstOrDefault(Function(x) x.Culture.TwoLetterISOLanguageName = "ar")
If lang IsNot Nothing Then InputLanguage.CurrentInputLanguage = lang
End Sub

Related

How do I set the Richtextbox.rtf to a rich text string?

I am trying to add a header to the rich text in a RichTextBox, using VB.NET and Visual Studio 2017. According to the documentation, Richtextbox.rtf should allow me to get or set the rich text including control codes. However, I am unable to set *.rtf to a string containing rich text. I know that the rich text is correct because if I paste it into a *.rtf file, it is displayed correctly.
The test code looks like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hdr As String = "{\header This is a header}"
Dim s As String = RichTextBox1.Rtf
s = s.Insert(s.LastIndexOf("}"c) - 1, hdr)
MsgBox(s)
With RichTextBox1
RichTextBox1.Rtf = s
MsgBox(RichTextBox1.Rtf)
End With
End Sub
The string s is correctly formatted as rich text, but RichTextBox1.Rtf is unchanged after the assignment. What am I missing? If I can't assign RichTextBox1.Rtf this way, is there an alternative?
Thanks again #PerpetualStudent!
The problem appears to be that the RichTextBox1.RTF field does not accept the "{\header This is a header}" control code. That is probably by design because a RichTextBox cannot display a header. I tried putting the control code in a different location in the rich text string, but that didn't work either.
I can edit the rich text in other ways (see below), but I cannot insert the header control code. That's unfortunate because it is part of the rich text standard. Anyhow, now that I know what the problem is, I can come up with a solution. A workaround might be to modify the print and save code of my rich text box print control form to add the header and footer in the print or save actions.
This works:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
With RichTextBox1
Dim s As String = .Rtf
s = s.Replace("Hello", "Good morning")
MsgBox(s)
.Rtf = s
MsgBox(.Rtf)
End With
End Sub

How do I add a VScrollBar to a textbox?

I am using a theme from the web for my vb.net application and the textbox does not have scrollbars or a scrollbar property. The theme did come with a VScrollBar Control, but I don't know how to add code to it to make it scroll the textbox like normal. Can anyone help me?
These are Custom Controls.
It's a Windows Form. (WinForms)
Textbox and its Properties:
Vertical scroll bars can be added to TextBox form objects, but however they must be Multiline:
This can either be done by setting Multiline to True and ScrollBars to Vertical:
or it can be done via code, programmatically, as per se:
TextBox1.Multiline = True
TextBox1.ScrollBars = ScrollBars.Vertical
You can set ScrollBars to be only horizontal, vertical, both, or none (default):
Remember, you should:
Be sanitizing the user's input if you're sending the textbox's contents off to a database
Limit the amount of characters that the user can input (see below)
Be using proper programming technique by naming your objects properly, for example, try not to name your textbox TextBox1
As mentioned above, you may want to show the amount of characters the user can input, for example:
the code for this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
text1.MaxLength = 140
charsLeft.Text = "0/" + CStr(text1.MaxLength)
End Sub
Private Sub textHasChanged() Handles text1.TextChanged
charsLeft.Text = CStr(text1.TextLength) + "/" + CStr(text1.MaxLength)
End Sub

Choosing between two sets of tootips

Working with the vbnet form editor, I have instancied 2 times the tooltip class: one for English Langage, one for French langage. (you can also imagine: short version /extended version)
How to choose dynamically between the to instancies at runtime?
I found this way to handle the topic:
Private Sub Tip_En_CheckedChanged(sender As Object, e As EventArgs) Handles Tip_En.CheckedChanged
If Tip_En.Enabled Then
Me.ToolTip_Fr.Active = False
Me.ToolTip_en.Active = True
End If
ToolTip_en.IsBalloon = True
End Sub
Private Sub Tip_Fr_CheckedChanged(sender As Object, e As EventArgs) Handles Tip_Fr.CheckedChanged
If Tip_Fr.Enabled Then
Me.ToolTip_en.Active = False
Me.ToolTip_Fr.Active = True
End If
End Sub
Could surely be shortened and factorized...
You can use 1 instance, only changing the text displayed at runtime.
You can use an extra class like ToolTipTranslationTexts that holds all the text translations after setting a language this class knows what language you have set.
Put a method like PopulateToolTipText into it, from which you change all tooltip texts to the right language.
This is one approach I could think off, but there are many more.
That way you can also add more languages only by adding text to this class.

How to Dynamically Update Label with TextBox As Input Changes

I have a spelling application that I am building in VB.Net, where I have a textbox receiving simple input (spelling words), and a label which will show the output. What I want to accomplish is when I enter something in the textbox, I can see it in my label - as I am typing into the textbox.
I will admit that I don't know what I'm doing, as I've never tried this before, so I don't know to begin in terms of setting up what I need to do. I know that I'll need some variable to hold my String input, and will probably need some type of loop, but beyond that, I am lost. The only other example is in C#, and doesn't help me any.
Can anyone give me a simple model to work off of, so I can put the approach into memory? For now, all I have is code stub from my TextChanged event handler:
Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
'Set variables to hold values.
Dim someText As String
'Connect the label and textbox.
lblShowInput.Text = txtWordInput.Text
'Process loop to populate the label from textbox input.
for '(This is where I am lost on the approach)
End Sub
I know that I'll need some variable to hold my String input, and will
probably need some type of loop
I don't think you'll need a loop, or the variable to hold the value. You almost have it:
Private Sub txtSpell_TextChanged(sender As Object, e As EventArgs) Handles txtSpell.TextChanged
'Connect the label and textbox.
lblShowInput.Text = txtSpell.Text
End Sub
In the code you provided, you are referencing an object named txtWordInput inside your txtSpell text changed event handler. If you are entering the text in the txtWordInput input, you'll want to handle this in the txtWordInput textChanged event handler:
Private Sub txtWordInput_TextChanged(sender As Object, e As EventArgs) Handles txtWordInput.TextChanged
'Connect the label and textbox.
lblShowInput.Text = txtWordInput.Text
End Sub
Follow-up:
The TextChanged event is the correct event for this.
In your code, you are assigning lblShowInput.Text to txtWordInput.Text, but in the txtSpell TextChanged event handler.
You want to be in the TextChanged event handler for whatever TextBox you would like to use to update the label, as the text is changing.
To give a better example, I have created a simple Winforms VB application that has only a textbox named InputTextBox and a label named Output Label.
The Form:
The Code:
Public Class Form1
Private Sub InputTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles InputTextBox.TextChanged
OutputLabel.Text = InputTextBox.Text
End Sub
End Class
Explanation:
InputTextBox_TextChanged is the method name generated by Visual Studio for our event handler
Handles InputTextBox.TextChanged ties the method to an actual event it is handling.
When the InputTextBox text property is changed (typically by user input), whatever we have in our InputTextBox_TextChanged Sub will execute. In this case, I am assigning the Text of OutputLabel to the Text of the InputTextBox
Output:
Resources:
I've uploaded this simple demo to GitHub if you'd like a closer look.
Take a look at the TextChanged documentation

how to format text box during input in vb.net

I have 200+ text boxes in vb.net application. Let me make it clear all are simple text boxes. now customer demand to have formatted number value while input or viewing record. With Format() i can play for viewing but during add/edit mode in text box (While user typing value) nothing happened
I want this result 1234567.0090 to 1,234,567.0090 during input.
or guide me any way by which i change all text boxes to mask textboxes through any tool or code.
Any help appreciated. Thanks in Advance.
First, I would recommend very strongly that you try to talk your customer out of this requirement. Masked text boxes in general are a royal pain in the butt, both for the programmer and for the end user. In my opinion, if you must format user input, it is far better to format whatever they have entered after the control loses focus than to attempt to format their input while they are still typing it.
With either approach, the easiest way to do this is to create your own user control (unless you want to use a third-party control, which I wouldn't advise for this purpose for a bunch of reasons) that inherits from TextBox (instead of inheriting from UserControl). If you wish to format the text after the user has finished entering input and has moved on to another control, you can add an EventHandler to your control's LostFocus event and format their input there.
If, however, you wish to format as they're typing, you have a couple of grisly choices. First, you can handle the control's KeyPress or KeyDown events, and intercept-and-cancel non-numeric characters, or else format the overall Text property at this time. This is a common approach which often fails in unexpected ways, since it ends up not dealing with text that is copy-and-pasted into the control (which happens quite often in data-entry applications).
An alternative approach is to handle the TextChanged event, which will respond to both keyboard input and pasted-in text, and re-format the text on the fly. Since you're often changing the text as they type, your code needs to pay attention to the SelectionStart property (among others), so that you don't unexpectedly change the caret's position as the user is typing. Also, when you change your control's text property while formatting it, this change will itself produce another TextChanged event, so you need to be careful that you don't get stuck in an endless loop.
To reiterate my main point, you will be much happier formatting in the LostFocus event, and so will your end users.
Once you've written your control, you can just do a global replace in your code, substituting "MyMaskedTextBox" for "TextBox" (case-sensitivity is recommended here).
Update: Here is some simple parsing/formatting code you can use in your TextBox's LostFocus event:
double d;
TextBox tb = (TextBox)sender;
if (double.TryParse(tb.Text, out d))
{
tb.Text = d.ToString("#,###,###,###.0000");
tb.BackColor = SystemColors.Window;
}
else
{
tb.BackColor = Color.Red;
}
This code will format the user's input as a number in the way that you require if the text entered can be parsed as a double. If the input is not a valid double, the text is left as is and the BackColor is changed to red. This is a good way of indicating invalid input to the user (as opposed to popping up a MessageBox).
Override these events in your text box derived custom control. But, remember no formating as they're typing,
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
MyBase.OnLostFocus(e)
Me.Text = Strings.FormatNumber(Me.Text, _
m_FormatNumDigitsAfterDecimal, _
m_FormatIncludeLeadingDigit, _
m_FormatUseParensForNegativeNumbers, _
m_FormatGroupDigits)
End Sub
Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(e)
If Me.Focused = False Then
Me.Text = Strings.FormatNumber(Me.Text, _
m_FormatNumDigitsAfterDecimal, _
m_FormatIncludeLeadingDigit, _
m_FormatUseParensForNegativeNumbers, _
m_FormatGroupDigits)
End If
End Sub
That´s another method.
Private Sub TBItemValor_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TBItemValor.KeyPress
If (Char.IsDigit(e.KeyChar) = False AndAlso Char.IsControl(e.KeyChar) = False AndAlso Char.IsPunctuation(e.KeyChar) = False) OrElse Not IsNumeric(Me.TBItemValor.Text & e.KeyChar) Then
e.Handled = True
End If
End Sub
Public Sub checktextbox2(txt As TextBox)
dim bg as string
For t = 1 To txt.Text.Length
If txt.Text.Chars(txt.Text.Length - (txt.Text.Length - t)) = "." Then
bq = txt.Text.TrimEnd(New String({"0", "."}))
txt.Text = bq
Exit For
End If
Next
end sub
this will format number in textbox as ###.###