cjk

I need to know a code to only have capital letters be able to be in a richtextbox. No numbers, no lowercase letters, and no symbols. Can someone help me




Re: Visual Basic Express Edition Only Capital letters in richtextbox

Dave299

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress

If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 90 Then

e.Handled = True

End If

End Sub





Re: Visual Basic Express Edition Only Capital letters in richtextbox

cjk

That works, except when i paste something it still has lowercase, symblas, and numbers. how do i fix this






Re: Visual Basic Express Edition Only Capital letters in richtextbox

Dave299

Tricky

This works for both pasted and typed text although it's a bit slow with large amounts of pasted text.

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim LengthInserted As Integer = RichTextBox1.TextLength - TextLength

TextLength = RichTextBox1.TextLength

If LengthInserted <= 0 Then Exit Sub

Dim Start As Integer = RichTextBox1.SelectionStart - LengthInserted

Dim Letter As String

Dim InsertionPoint As Integer = RichTextBox1.SelectionStart

Dim LettersRemoved As Integer

For count As Integer = RichTextBox1.SelectionStart To Start + 1 Step -1

Letter = RichTextBox1.Text.Substring(count - 1, 1)

If Asc(Letter) < 65 Or Asc(Letter) > 90 Then

RichTextBox1.Text = RichTextBox1.Text.Remove(count - 1, 1)

TextLength -= 1

LettersRemoved += 1

End If

Next

RichTextBox1.SelectionStart = InsertionPoint - LettersRemoved

End Sub





Re: Visual Basic Express Edition Only Capital letters in richtextbox

Solitaire

This should work. I tested it by copying from a textbox to the Richtextbox as a substitute for pasting text. The KeyPress event is the same as previously posted. It can be combined with pasting text in any order.

Code Snippet

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress

If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 90 Then e.Handled = True

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim ch As String, N, x As Integer

N = RichTextBox1.Text.Length

x = 0

Do While x < N

x += 1

ch = RichTextBox1.Text.Substring(x - 1, 1)

If Asc(ch) < 65 Or Asc(ch) > 90 Then

RichTextBox1.Text = RichTextBox1.Text.Remove(x - 1, 1)

N -= 1

x -= 1

End If

Loop

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

RichTextBox1.Text &= TextBox1.Text

End Sub






Re: Visual Basic Express Edition Only Capital letters in richtextbox

Solitaire

Hmm, I see that Dave beat me to it with nearly identical code, although I worked out my code independently, using a Do loop rather than a For loop.






Re: Visual Basic Express Edition Only Capital letters in richtextbox

Dave299

Hi Solitaire

Almost identical but a couple of differences. My code only checks the changed text which could mean better performance on a large text box. Also it resets the insertion point each time you paste or type something in. Without that it keeps going back to the beginning of the text. I couldn't be bothered to work out why, so I just put a couple of lines in to reset it.





Re: Visual Basic Express Edition Only Capital letters in richtextbox

js06

Hi Dave,

I tried your code and i got an error on this line

Letter = RichTextBox1.Text.Substring(count - 1, 1)

argument out of range exception

StartIndex cannot be less than zero.

Can you tell me what is happening

Thanks

Jeff






Re: Visual Basic Express Edition Only Capital letters in richtextbox

Solitaire

Try my code. It may be a little slower than Dave's for long text, but I tested it and hopefully it should work without an error.






Re: Visual Basic Express Edition Only Capital letters in richtextbox

Dave299

Hi Jeff

First rule of bug reporting - Don't just report what happened but say how you made it happen.

Or to put it another way - I can't reproduce that fault.

Just noticed I didn't copy and paste the declaration for TextLength - must stop doing this late at night. I presume you worked it out but just for completeness the code needs

Dim TextLength As Integer

adding as a member variable.





Re: Visual Basic Express Edition Only Capital letters in richtextbox

js06

Hi solitaire,

I did try yours and it works very well. I was trying dave's also and i just was curious why it wouldn't work for me.

Hi Dave,

Yes i added the variable before i tried it.

Basically the error comes up after you start typing or paste text in

If you type capitals it works fine but if you start typing lowercase it will get caught on the first one

The way i can consistently get the error is to type a capital letter first, and then a lowercase

It seems as though if you have no letters typed and you type a lowercase it will remove it

but as soon as you have a capital letter typed in and then type a lowercase it will cause the error






Re: Visual Basic Express Edition Only Capital letters in richtextbox

Solitaire

I added a form-level variable that will reduce the time it takes to execute the code, as Dave had attempted to do. Instead of testing all existing text, it will only affect new text added on to it. I tested it and it works OK. The change is the Private variable curlen, and starting with that value for x in the TextChanged event.

Code Snippet

Public Class Form1

Private curlen As Integer

Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress

If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 90 Then e.Handled = True

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim ch As String, N, x As Integer

N = RichTextBox1.Text.Length

x = curlen

Do While x < N

x += 1

ch = RichTextBox1.Text.Substring(x - 1, 1)

If Asc(ch) < 65 Or Asc(ch) > 90 Then

RichTextBox1.Text = RichTextBox1.Text.Remove(x - 1, 1)

N -= 1

x -= 1

End If

Loop

curlen = N - 1

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

RichTextBox1.Text &= TextBox1.Text

End Sub

End Class






Re: Visual Basic Express Edition Only Capital letters in richtextbox

Dave299

Jeff

I'm perplexed. Try what I may I cannot reproduce what you are seeing. I always test code before posting it, usually doing a final check from the Preview view where I paste back in the code I am posting and make sure that it still works. I've just done that on this post and it definitely works.

Please try this for me. Start a new project and paste this in. It's exactly the same code just with a bit added to create the box.

Public Class Form1

Dim WithEvents RichTextBox1 As New RichTextBox

Dim TextLength As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

RichTextBox1.Dock = DockStyle.Fill

RichTextBox1.Font = New Font("Arial", 12, FontStyle.Bold)

Controls.Add(RichTextBox1)

End Sub

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

Dim LengthInserted As Integer = RichTextBox1.TextLength - TextLength

TextLength = RichTextBox1.TextLength

If LengthInserted <= 0 Then Exit Sub

Dim Start As Integer = RichTextBox1.SelectionStart - LengthInserted

Dim Letter As String

Dim InsertionPoint As Integer = RichTextBox1.SelectionStart

Dim LettersRemoved As Integer

For count As Integer = RichTextBox1.SelectionStart To Start + 1 Step -1

Letter = RichTextBox1.Text.Substring(count - 1, 1)

If Asc(Letter) < 65 Or Asc(Letter) > 90 Then

RichTextBox1.Text = RichTextBox1.Text.Remove(count - 1, 1)

TextLength -= 1

LettersRemoved += 1

End If

Next

RichTextBox1.SelectionStart = InsertionPoint - LettersRemoved

End Sub

End Class





Re: Visual Basic Express Edition Only Capital letters in richtextbox

Solitaire

Dave, I tried copying your code into my program and kept getting errors associated with the count value. Count must keep changing because the length is sometimes the same and sometimes reduced. If reduced, the count must be repeated. That's why I used a Do loop instead of For in my code. I had originally tried it with For and it didn't work.






Re: Visual Basic Express Edition Only Capital letters in richtextbox

js06

Dave,

Your code works well in a new project

Just to make sure i also added a richtextbox with the designer and then tried the code

It works just fine

I guess there was something wrong with my project

Thanks

edit----------------

I think that the problem was with copying code from here

I have noticed before that when i have copied certain things here in the forum they will not work right

I'm not sure if it has anything to do with the formatting in the forum editor

The only other time i really have had any issues like this was when trying to paste event sub code

like this

Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged

sometimes if i paste a sub like this it will throw an error when i try to run it

Maybe this is because of the editor also

I will check better next time