8 Replies - 418 Views - Last Post: 15 December 2018 - 07:48 PM Rate Topic: -----

#1 BigD3v3loper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-December 18

Font changing problem

Posted 13 December 2018 - 09:38 AM

Hi, I've got another problem creating my own notepad program. I tried searching it but I found nothing, so I'm gonna ask it here. When I change font in textbox, it works if I only write with that language, but if I change my system input language, font wouldn't work anymore and it will be ms default font. For example:
https://imgur.com/a/SPda5cg
So if someone could tell me if its fixable or not, I'd appreciate a lot :)/>

And also it's almost same for every font, they are few exceptions which changing language wouldn't effect it

Is This A Good Question/Topic? 0
  • +

Replies To: Font changing problem

#2 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 195
  • View blog
  • Posts: 1,089
  • Joined: 05-December 13

Re: Font changing problem

Posted 13 December 2018 - 12:24 PM

Sounds like a strange issue. Paste your code where you are setting these fonts?

so... you set the font in your app, but when you change computer language, the font changes on your app too?

What font are you using in your app? What font does your language use which you're changing too?
Was This Post Helpful? 0
  • +
  • -

#3 BigD3v3loper   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-December 18

Re: Font changing problem

Posted 13 December 2018 - 01:06 PM

Thanks for replying, here is the code I use for this:
Code for "Change font" button:
      FontDialog1.ShowDialog()

        If FontDialog1.ShowDialog = DialogResult.OK Then
            RichTextBox1.Font = FontDialog1.Font
            My.Computer.FileSystem.WriteAllText(Application.StartupPath + "\font.txt", FontDialog1.Font.Name, False)
            My.Computer.FileSystem.WriteAllText(Application.StartupPath + "\fontsi.txt", FontDialog1.Font.Size, False)
        End If

Code for reading font:
   Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged  
            RichTextBox1.Font = New Font(l, l2, FontStyle.Regular)
    End Sub

"l" reads "font.txt" file and "l2" reads "fontsi.txt"(which is for font size).
Sometimes language change aswell when I type the first letter, but I prevent it with changing input lanuage method
And when I change my system input language and type something, font would be completely changed (even if I delete everything in richtextbox, only thing I can do is to restart program so it can read the font again

But of course, it works only if the font script is arabic
https://imgur.com/qjQjOSj
And I believe the font is changing to ms default font which is Microsoft Sans Serif
Thanks :D/>
Was This Post Helpful? 0
  • +
  • -

#4 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 195
  • View blog
  • Posts: 1,089
  • Joined: 05-December 13

Re: Font changing problem

Posted 13 December 2018 - 02:02 PM

View PostBigD3v3loper, on 13 December 2018 - 08:06 PM, said:

"l" reads "font.txt" file and "l2" reads "fontsi.txt"(which is for font size).

Give me the related code and methods/vars or anything else relative to this issue. I'm not writing it out for you when you can provide it...
Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 195
  • View blog
  • Posts: 1,089
  • Joined: 05-December 13

Re: Font changing problem

Posted 13 December 2018 - 04:32 PM

Stop it from changing by intercepting the firing event that detects the change of font on the RTB. The RTB has a font changed event. It's not perfect but it works without flicker for me. Move the conditional logic to where you see fit. Sample code to play with ::
    Dim lastSetFont As Font
    Dim allowFontChange As Boolean
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If lastSetFont Is Nothing Then
            lastSetFont = FontDialog1.Font
        End If
        If FontDialog1.ShowDialog = DialogResult.OK Then
            If allowFontChange = True Then
                RichTextBox1.Font = FontDialog1.Font
            Else
                lastSetFont = FontDialog1.Font
            End If
        End If
    End Sub
    Private Sub RichTextBox1_FontChanged(sender As Object, e As EventArgs) Handles RichTextBox1.FontChanged
        If allowFontChange = True AndAlso lastSetFont IsNot Nothing AndAlso Not RichTextBox1.Font.Name = lastSetFont.Name Then
            lastSetFont = RichTextBox1.Font
        Else
            Exit Sub 'Do nothing, and exit sub
        End If
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Select Case allowFontChange
            Case True
                allowFontChange = False
                Return
            Case False
                allowFontChange = True
                Return
        End Select
    End Sub

Perhaps if IronRazer stumbles upon this post, he may have better advice and a better way to stop font changing permanently?

Providing the allowFontChange is false, when you change language on your PC, it should not change the font on the RTB. If it does, edit the conditional operators to your liking in RichTextBox1_FontChanged to prevent it.
Was This Post Helpful? 1
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: Font changing problem

Posted 13 December 2018 - 05:21 PM

@ BigD3v3loper,

Is there any special reason you are saving the font name and size to text files? If you are only doing this so you can reset the font to whatever it was set to last, then you should be aware that there are much, much easier ways to do this.

You can bind the font property of the RichTextBox to an application setting with just a few clicks of your mouse. Then absolutely no code is needed to save/read to/from text files. It will all be done taken care of for you when your application is opened or closed. The only thing you would need to do is set the RichTextBox font to the new font when one is chosen with the FontDialog.

You can test this in a new Form project with one RichTextBox and one Button added to the Form. Then follow the six steps in the animated gif below...
Attached Image
All that would be required after this is to allow the user to change the RichTextBox font...
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using fd As New FontDialog
            If fd.ShowDialog = DialogResult.OK Then
                RichTextBox1.Font = fd.Font
            End If
        End Using
    End Sub

End Class




As a side note about your code, using the TextChanged event for assigning a New font to the RichTextBox every time the text changes is really not a great idea. It could easily create unnecessarily high memory consumption or even worse, memory leaks. I would also suggest turning on Option Strict so you can see the type conversion error(s) you have, I can see at least one.

I don't really understand what your desired outcome is but, maybe the above will help in some way. 8)
Was This Post Helpful? 2
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 195
  • View blog
  • Posts: 1,089
  • Joined: 05-December 13

Re: Font changing problem

Posted 13 December 2018 - 06:07 PM

I've always welcomed your expertise Iron, even going back to the days on the other forum we both came from many moons ago. But I think you are mistaken and have miss-read my code this time, where you said TextChanged, which is not what i advocate using. I am using FontChanged. So if you think about it, I doubt that the font would change an awful lot, especially not enough to cause any excessive memory usage.

I assume you're talking to the OP re type conversions? If so, where? Since i only ever use Option Strict / Explicit anyway ;)

+1 for chipping in. Just figured you may have had a better way to do it. :/

This post has been edited by Sheepings: 13 December 2018 - 06:16 PM

Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1532
  • View blog
  • Posts: 3,859
  • Joined: 01-February 13

Re: Font changing problem

Posted 15 December 2018 - 05:27 PM

Quote

... But I think you are mistaken and have miss-read my code this time, where you said TextChanged, which is not what i advocate using. I am using FontChanged.

Did you look at the code that OP posted? If you do you would see that OP is assigning a new font in the TextChanged event. If I was talking to you I would have said I was.

Quote

I assume you're talking to the OP re type conversions? If so, where? ...

Perhapse I was a little sleep deprived at that time but, it was where OP said... "l" reads "font.txt" file and "l2" reads "fontsi.txt"(which is for font size) that made me assume "l" and "l2" are String types read from the text file and are being used in the line below which OP posted. The second parameter is suppose to be a Single type as you know.
RichTextBox1.Font = New Font(l, l2, FontStyle.Regular)

Was This Post Helpful? 0
  • +
  • -

#9 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 195
  • View blog
  • Posts: 1,089
  • Joined: 05-December 13

Re: Font changing problem

Posted 15 December 2018 - 07:48 PM

l and l2 was gibberish, so paid no interest to it. Didn't look to hard at the text event either, one liner there, my focus was on the dialog. It's not important
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1