字体家庭和字体尺寸组合盒编码错误

发布于 2025-02-09 17:40:34 字数 3958 浏览 2 评论 0原文

自从我发布以来已经有一段时间了。已经有一段时间没有太多时间进行编程。几天前才回到它。我正在研究一个个人项目,这是一种具有相当完整的文本编辑器的项目经理。我正在Visual Studio IDE中与vb.net合作。我一直在尝试模仿单词和其他编辑者的行为:

font family :此代码实际上可以根据文本格式在CURSOR上实时更改字体拾取器组合框中的字体族。位置。即单击“丰富文本”框中的不同文本格式,字体系列显示在组合框中! 我使用的是工具带组合框!

错误:字体族代码(即倒数第二个片段)在格式化后重新定义时从文本中删除格式?

字体大小:此代码在CTRL+Shift+<时更改字体大小组合框中的字体大小。或Ctrl+Shift+>使用键盘快捷键。

bug:字体大小代码(即最后一段)会影响字体尺寸,当时使用,。<>键是因为这些键在代码中配置为以更改字体大小Combobox中字体大小的值。因此,按键会导致组合框中的字体尺寸增加或减小,这也意味着RTB中的字体增加或减小?

我需要的内容: 1。选择格式的文本不应删除格式,但是字体系列应根据RTB中的格式显示在组合框中。 2。只有在使用键盘快捷键时,才能更改字体大小组合框值。或Ctrl+Shift+>实时显示真正的字体大小。使用当前代码增量/减少2个步骤2而不是一个字体大小???我意识到此代码相当简单,并且粗略地理解为什么它不起作用,但是我似乎找不到解决问题的方法?任何帮助将不胜感激!

我尝试过的

丰富的文本框(即rtb =描述):

字体家庭组合框代码:(使用计时器)

Private Sub tbSelectFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tbSelectFont.SelectedIndexChanged

        Dim NewFont As New Font(tbSelectFont.SelectedItem.ToString(),
                                Description.SelectionFont.Size,
                                Description.SelectionFont.Style)
        Description.SelectionFont = NewFont

    End Sub

font尺寸组合框代码:

Private Sub tbSelectSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tbSelectSize.SelectedIndexChanged

        Dim NewSize As Single = tbSelectSize.SelectedItem
        Dim NewFont As New Font(Description.SelectionFont.Name, NewSize, Description.SelectionFont.Style)
        Description.SelectionFont = NewFont

    End Sub

相关表格负载事件:

'Display Installed Fonts In Font Picker (tbSelectFont):

        Dim fonts As New InstalledFontCollection()
        For fntFamily As Integer = 0 To fonts.Families.Length - 1
            tbSelectFont.Items.Add(fonts.Families(fntFamily).Name)
        Next

        'Display Font Size in Font Size Picker:

        For fntSize = 10 To 75
            tbSelectSize.Items.Add(fntSize)
        Next

字体family:

Private Sub Description_SelectionChanged(sender As Object, e As EventArgs) Handles Description.SelectionChanged

        If Description.SelectionFont IsNot Nothing Then

            Dim fontSize As Single = Description.SelectionFont.Size
            tbSelectSize.Text = fontSize

            tbSelectFont.Text = Description.SelectionFont.Name

        End If

End Sub

字体大小:

if(keys.control andlo andalso keys.shift.shift e.KeyCode =键

tbSelectSize.Text -= 1

ElseIf (Keys.Control AndAlso Keys.Shift AndAlso e.KeyCode = Keys.OemPeriod) Then

tbSelectSize.Text += 1

End If

Andalso

。对象的实例。 资料来源:我自己的评论来自:

Dim fontName As String = Description.SelectionFont.Name
    Dim fontSize As Single = Description.SelectionFont.Size

    tbSelectFont.Text = fontName
    tbSelectSize.Text = fontSize

    tbSelectFont.SelectedIndex = tbSelectFont.FindStringExact(Description.SelectionFont.Name)

    tbSelectSize.SelectedIndex = tbSelectSize.FindStringExact(Description.SelectionFont.Size)

此代码有效,但是当您选择格式的文本时,它会引发异常:system.nullreference exception:'对象引用未设置为实例一个对象。 资料来源:如何使用winforms - Markusegle的解决方案在C#中的RichTextbox中获得某个行的字体大小。

Dim comboBox1Index As Integer = tbSelectFont.FindStringExact(Description.SelectionFont.Name)
Dim comboBox2Index As Integer = tbSelectSize.FindStringExact(Description.SelectionFont.Size.ToString())
tbSelectFont.SelectedIndex = comboBox1Index
tbSelectSize.SelectedIndex = comboBox2Index

it's been a while since I posted. Haven't had much time for programming for a while. Just got back into it a couple of days ago. I'm working on a personal project which is a kind of Project Manager with a fairly complete text editor. I am working with VB.NET in the Visual Studio IDE. I have been trying to mimic the behaviour of Word and other editors as follows:

FONT FAMILY: This code actually works to change the Font Family in the Font Picker Combo Box in real time according to text formatting at cursor position. i.e. click on different text formats in the Rich Text Box and the Font Family is displayed in the Combo Box! I am using a Tool Strip Combo Box!

BUG: The Font Family code (i.e. penultimate snippet) removes the formatting from text when it is reselected after formatting?

FONT SIZE: This code changes the Font Size in the Font Size Combo Box when Ctrl+Shift+< OR Ctrl+Shift+> keyboard shortcuts are used.

BUG: The Font Size code (i.e. Last snippet) affects Font Size when using the ,.<> keys because these keys are configured in the code to alter the value of the Font size in the Font Size ComboBox. So, pressing the keys causes the Font Size in the Combo Box to increase or decrease which also means the font in the RTB increases or decreases?!

WHAT I NEED: 1. Selecting formatted text should not remove formatting, but Font Family should be displayed in Combo Box according to formatting in the RTB. 2. The Font Size Combo Box value should only be altered when I use the keyboard shortcuts, Ctrl+Shift+< OR Ctrl+Shift+> to display the true Font Size in real time. Using the current code increments/reduces in steps of 2 rather than one Font Size at a time??? I realize this code is fairly simple and understand roughly why it's not working, but I can't seem to find a way to fix the issue? Any help would be greatly appreciated!

WHAT I HAVE TRIED

Rich Text Box (i.e. RTB = Description):

FONT FAMILY COMBO BOX CODE: (uses a timer)

Private Sub tbSelectFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tbSelectFont.SelectedIndexChanged

        Dim NewFont As New Font(tbSelectFont.SelectedItem.ToString(),
                                Description.SelectionFont.Size,
                                Description.SelectionFont.Style)
        Description.SelectionFont = NewFont

    End Sub

FONT SIZE COMBO BOX CODE:

Private Sub tbSelectSize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles tbSelectSize.SelectedIndexChanged

        Dim NewSize As Single = tbSelectSize.SelectedItem
        Dim NewFont As New Font(Description.SelectionFont.Name, NewSize, Description.SelectionFont.Style)
        Description.SelectionFont = NewFont

    End Sub

RELATED FORM LOAD EVENTS:

'Display Installed Fonts In Font Picker (tbSelectFont):

        Dim fonts As New InstalledFontCollection()
        For fntFamily As Integer = 0 To fonts.Families.Length - 1
            tbSelectFont.Items.Add(fonts.Families(fntFamily).Name)
        Next

        'Display Font Size in Font Size Picker:

        For fntSize = 10 To 75
            tbSelectSize.Items.Add(fntSize)
        Next

FONT FAMILY:

Private Sub Description_SelectionChanged(sender As Object, e As EventArgs) Handles Description.SelectionChanged

        If Description.SelectionFont IsNot Nothing Then

            Dim fontSize As Single = Description.SelectionFont.Size
            tbSelectSize.Text = fontSize

            tbSelectFont.Text = Description.SelectionFont.Name

        End If

End Sub

FONT SIZE:

If (Keys.Control AndAlso Keys.Shift AndAlso e.KeyCode = Keys.Oemcomma) Then

tbSelectSize.Text -= 1

ElseIf (Keys.Control AndAlso Keys.Shift AndAlso e.KeyCode = Keys.OemPeriod) Then

tbSelectSize.Text += 1

End If

OTHER CODE I TRIED:

THIS CODE WORKS, BUT WHEN YOU SELECT FORMATTED TEXT IT THROWS AN EXCEPTION OR REMOVES FORMATTING: System.NullReferenceException: 'Object reference not set to an instance of an object.'
SOURCE: MY OWN COMMENT FROM:
how to get the fontsize of a certain line in a richtextbox in c# using winforms

Dim fontName As String = Description.SelectionFont.Name
    Dim fontSize As Single = Description.SelectionFont.Size

    tbSelectFont.Text = fontName
    tbSelectSize.Text = fontSize

    tbSelectFont.SelectedIndex = tbSelectFont.FindStringExact(Description.SelectionFont.Name)

    tbSelectSize.SelectedIndex = tbSelectSize.FindStringExact(Description.SelectionFont.Size)

THIS CODE WORKS, BUT WHEN YOU SELECT FORMATTED TEXT IT THROWS AN EXCEPTION: System.NullReferenceException: 'Object reference not set to an instance of an object.'
SOURCE: how to get the fontsize of a certain line in a richtextbox in c# using winforms - MarkusEgle's solution.

Dim comboBox1Index As Integer = tbSelectFont.FindStringExact(Description.SelectionFont.Name)
Dim comboBox2Index As Integer = tbSelectSize.FindStringExact(Description.SelectionFont.Size.ToString())
tbSelectFont.SelectedIndex = comboBox1Index
tbSelectSize.SelectedIndex = comboBox2Index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文