views:

1050

answers:

2

I am building a word pad clone with extra a few extra features. I am trying to get two combo boxes (one with font names, the other with font sizes) to apply their selected attribute to the selected text in a richtextbox. This was my latest attempt, and I think I'm starting to get close, but I still get an error for InvalidCastException - Conversion from string "" to type 'Integer' is not valid.

Private Sub FontToolStripComboBox_DropDownClosed(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontToolStripComboBox.DropDownClosed
    Dim ComboFonts As System.Drawing.Font
    ComboFonts = Pad.SelectionFont
    Pad.SelectionFont = New System.Drawing.Font(ComboFonts.Name(FontToolStripComboBox.SelectedText), Font.Size, Font.Style) '<--- this line is giving the exception for the FontToolStripComboBox.SelectedText entry'
    End Sub

I can't figure out how to use the string in the combobox with the font name to actually change the font to that name.

A: 

replace this

FontToolStripComboBox.SelectedText

with this

FontToolStripComboBox.text

Edit: because SelectedText means the text that is selected in the editable portion of a ComboBox check SelectedText at MSDN

Edit:

try replacing:

 Pad.SelectionFont = New System.Drawing.Font(ComboFonts.Name(FontToolStripComboBox.SelectedText), Font.Size, Font.Style)

with:

Pad.SelectionFont = New System.Drawing.Font(FontToolStripComboBox.Text, Font.Size, Font.Style)
Wael Dalloul
I tried it, but I receive the same error. I'm pretty sure that the problem is that the text in the combobox is not able to be cast as a font, but I have searched and have not found a way to convert a font name as a string into something vb.net can use to change a font name
MaQleod
well, almost works. I no longer get the error, but it does not use the text in the combobox to set the font, it always defaults to the font it was previously, but size 8.25.
MaQleod
+1  A: 

Try this. The DropDownClosed event leaves you one font behind all the time. Also, you should assign the size and style somewhere. (Maybe this is already being done outside your function?)

Private Sub FontToolStripComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FontToolStripComboBox.SelectedIndexChanged
  Dim ComboFonts As System.Drawing.Font
  ComboFonts = pad.SelectionFont
  pad.SelectionFont = New System.Drawing.Font(FontToolStripComboBox.Text, pad.SelectionFont.Size, pad.SelectionFont.Style)
End Sub
xpda
Thanks, this worked, but I can't seem to apply the same to the font size. This is what I tried - not even sure if I'm on the right approach:Pad.SelectionFont = New System.Drawing.Font(SizeToolStripComboBox.Text, Pad.SelectionFont.Size, Pad.SelectionFont.Style)
MaQleod
To change the font size and style, use the new font size and style add them in the font assignment: pad.SelectionFont = New system.drawing.font(FontToolStripComboBox.Text, [new size value], [new style value].
xpda
Thanks, that worked, but I had to declare the size value as a single first: Dim NewSize As Single = SizeToolStripComboBox.Text Pad.SelectionFont = New System.Drawing.Font(FontToolStripComboBox.Text, NewSize, Pad.SelectionFont.Style)
MaQleod