12 Replies - 2063 Views - Last Post: 23 July 2009 - 03:08 PM Rate Topic: ***-- 2 Votes

#1 Yaroslav94  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 294
  • Joined: 15-July 09

How to use the size from combobox1 in richtextbox1 [Solved]

Post icon  Posted 22 July 2009 - 04:48 PM

How can i make the font's size in richtextbox equal to combobox1?

 RichTextBox1.font.size = combobox1.text 
' not workin

This post has been edited by Yaroslav94: 23 July 2009 - 05:45 AM

Is This A Good Question/Topic? 0
  • +

Replies To: How to use the size from combobox1 in richtextbox1 [Solved]

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,012
  • Joined: 02-July 08

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 05:00 PM

It would be easy to do in the designer, code like this:
 Richtextbox1.Font = New Font("Courier New", combobox1.Font.Size, FontStyle.Bold)

Was This Post Helpful? 0
  • +
  • -

#3 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 372
  • Joined: 17-July 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 05:09 PM

View Posthawkvalley1, on 23 Jul, 2009 - 11:00 AM, said:

It would be easy to do in the designer, code like this:
 Richtextbox1.Font = New Font("Courier New", combobox1.Font.Size, FontStyle.Bold)



Change combobox1.Font.Size to ComboBox1.SelectedItem
Was This Post Helpful? 0
  • +
  • -

#4 Yaroslav94  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 294
  • Joined: 15-July 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 05:23 PM

View PostPDUNZ, on 22 Jul, 2009 - 04:09 PM, said:

View Posthawkvalley1, on 23 Jul, 2009 - 11:00 AM, said:

It would be easy to do in the designer, code like this:
 Richtextbox1.Font = New Font("Courier New", combobox1.Font.Size, FontStyle.Bold)



Change combobox1.Font.Size to ComboBox1.SelectedItem

How can i do that for combobox3, which is the font style(bold/italic etc.)

When I do
  RichTextBox1.Font = New Font(ComboBox1.Text, ComboBox2.Text, Combobox3.text )
'it doesnt work...neither with .selecteditem 



Help?!?!?!?
Was This Post Helpful? 0
  • +
  • -

#5 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 372
  • Joined: 17-July 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 05:34 PM

Ie.
 Richtextbox1.Font = New Font(ComboBox1.SelectedItem, ComboBox2.SelectedItem, ComboBox3.SelectedItem) 


This is assuming the 3rd combobox has items Bold, Italic etc.

Make sure for all three combo's, you change .text to .SelectedItem.

This post has been edited by PDUNZ: 22 July 2009 - 05:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 372
  • Joined: 17-July 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 05:39 PM

Also, make sure you either use the code in a button event (when you click a button, it runs the code) or in each of the comboboxes SelectedIndexChanged event.

Example for Combobox 1.
 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
		Richtextbox1.Font = New Font(ComboBox1.SelectedItem, ComboBox2.SelectedItem, ComboBox3.SelectedItem)
	End Sub

This post has been edited by PDUNZ: 22 July 2009 - 05:40 PM

Was This Post Helpful? 0
  • +
  • -

#7 Yaroslav94  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 294
  • Joined: 15-July 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 05:40 PM

View PostPDUNZ, on 22 Jul, 2009 - 04:34 PM, said:

Ie.
 Richtextbox1.Font = New Font(ComboBox1.SelectedItem, ComboBox2.SelectedItem, ComboBox3.SelectedItem) 


This is assuming the 3rd combobox has items Bold, Italic etc.

Make sure for all three combo's, you change .text to .SelectedItem.


I doesnt work like that... tells me there is an error..and i dont want to use the if code...


Error:

Error	1	Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
	'Public Sub New(familyName As String, emSize As Single, unit As System.Drawing.GraphicsUnit)': Argument matching parameter 'emSize' narrows from 'String' to 'Single'.
	'Public Sub New(familyName As String, emSize As Single, unit As System.Drawing.GraphicsUnit)': Argument matching parameter 'unit' narrows from 'String' to 'System.Drawing.GraphicsUnit'.
	'Public Sub New(familyName As String, emSize As Single, style As System.Drawing.FontStyle)': Argument matching parameter 'emSize' narrows from 'String' to 'Single'.
	'Public Sub New(familyName As String, emSize As Single, style As System.Drawing.FontStyle)': Argument matching parameter 'style' narrows from 'String' to 'System.Drawing.FontStyle'.	C:\Users\zzz\AppData\Local\Temporary Projects\zzz\Form1.vb	40	29	zzz



U see?
Was This Post Helpful? 0
  • +
  • -

#8 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 372
  • Joined: 17-July 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 06:11 PM

Ah ha, just did a test - I get the same error now, but I noticed that if using 1 combo box it works fine, the error is with Combobox 3 - for Bold, Italic etc as it wants "fontstyle.bold" ete not just "Bold".


Give me a bit while I figure it out for you.

Will post back shortly.

This post has been edited by PDUNZ: 22 July 2009 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#9 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 06:27 PM

You need to pay attention to what the IDE is telling you and maybe get a better understanding of properties and data types. Now, first of all you should understand the reason(s) why this didn't work:
RichTextBox1.font.size = combobox1.text

If you take a minute to read the little tool-tips the IDE gives you when you hover your mouse over items in the IntelliSense, or better yet, use the Object Browser and you will learn exactly what kind of a property Size is.

Quote

Public ReadOnly Property Size() As Single
Member of System.Drawing.Font
Summary:
Gets the em-size of this System.Drawing.Font measured in the units specified by the System.Drawing.Font.Unit property.

Return Values:

The em-size of this System.Drawing.Font.

The main thing here is that it is declared ReadOnly, you cannot set this property directly. It is also of type Single, a single-precision floating point number.
So, as the others suggested the way to set a font is to create a new instance of the Font class and pass in the required or desired arguments. You must pass in arguments of the correct type because the constructor of System.Drawing.Font is overloaded, there are several different versions of it and the compiler has no idea which one it is supposed to use because you're passing in three values of type System.Object and in .Net, everything is a System.Object do you see why this could present a problem? If not Google terms Constructor, Overloading methods, overloaded constructor, System.Object and Inheritance.

What you would have to do is something like this to set the font name and size, forget about style for a minute:
rtb.Font = New Font(cmbFName.SelectedItem.ToString, CSng(cmbFSize.SelectedItem))

If you want to be able to set a style you have to pass in a value for 'style' and it has to be of type System.Drawing.FontStyle so let's say you have a combobox and the items are the possible font styles. When the user selects a style, you would have to determine the value in your Combobox and then set the corresponding value to an object of type System.Drawing.FontStyle and finally pass that object in as the 'style' parameter:
	Private Sub cmbFStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFStyle.SelectedIndexChanged
		Dim theStyle As FontStyle = FontStyle.Regular

		Select Case cmbFStyle.SelectedIndex
			Case 0
				theStyle = FontStyle.Regular
			Case 1
				theStyle = FontStyle.Bold
			Case 2
				theStyle = FontStyle.Italic
			Case 3
				theStyle = FontStyle.Underline
			Case 4
				theStyle = FontStyle.Strikeout
		End Select

		rtb.Font = New Font(cmbFName.SelectedItem.ToString, CInt(cmbFSize.SelectedItem), theStyle)
	End Sub

You can set multiple font styles using the bitwise OR operator.
theStyle = FontStyle.Bold Or FontStyle.Underline

That will give you bold, underlined text.

This post has been edited by LoveIsNull: 22 July 2009 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 Yaroslav94  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 294
  • Joined: 15-July 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 06:38 PM

Thank You ... it finally worked...but what is cint - is it some kind of number?

and why would it not work without cint?
Was This Post Helpful? 0
  • +
  • -

#11 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 22 July 2009 - 06:56 PM

CInt() converts an object to an Integer, CSng() converts to a Single. CSng is the one we wanted to use, though CInt is probably fine as long as the compiler knows what version of the constructor you're trying to use.
Chances are it would have worked had you just assured the compiler that your first parameter was a string.
The important thing is that System.Drawing.Font has an overloaded constructor, each version of the constructor has a signature and is identified by the kinds of parameters it takes. Here are all the versions of the constructor and their respective signatures.
Was This Post Helpful? 1
  • +
  • -

#12 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1030
  • View blog
  • Posts: 4,012
  • Joined: 02-July 08

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 23 July 2009 - 06:16 AM

Convert.ToInt32 is a .Net way to do it. Cint is a VB6 leftover.
Was This Post Helpful? 0
  • +
  • -

#13 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: How to use the size from combobox1 in richtextbox1 [Solved]

Posted 23 July 2009 - 03:08 PM

Quote

Convert.ToInt32 is a .Net way to do it. Cint is a VB6 leftover.

I wouldn't definitively agree with that statement. CType, CInt, CStr and related type conversion functions are certainly VB Functions, but I am not so sure they are exactly what you would call 'legacy' functions. They are built into the VB language but do not actually require a reference to the Microsoft.VisualBasic namespace like other true 'legacy' functions such as MsgBox, Split, Str, Int, InStr, Trim, etc.
Take a look at what MSDN has to say about these Type Conversion Functions.
In particular,

Quote

As a rule, you should use the Visual Basic type conversion functions in preference to the .NET Framework methods such as ToString(), either on the Convert class or on an individual type structure or class. The Visual Basic functions are designed for optimal interaction with Visual Basic code, and they also make your source code shorter and easier to read. In addition, the .NET Framework conversion methods do not always produce the same results as the Visual Basic functions, for example when converting Boolean to Integer.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1