Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code
Setting the font size and style with vb code Setting the font size and style with vb code
Setting the font size and style with vb code
Go Back  Xtreme Visual Basic Talk > > > Setting the font size and style with vb code


Reply
 
Thread Tools Display Modes
  #1  
Old 01-14-2010, 12:43 PM
Azza1988 Azza1988 is offline
Newcomer
 
Join Date: Jan 2010
Posts: 3
Default Setting the font size and style with vb code


Well as the title says how would i set the font size and style using Vb code as i'm designing a Notepad like application, and when i start a new form i want to reset all the settings.

Everything else is working, i'm just having a hard time working these out


Thanks in advance
Reply With Quote
  #2  
Old 01-14-2010, 01:47 PM
AtmaWeapon's Avatar
AtmaWeaponSetting the font size and style with vb code AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Generally font settings have a kind of inheritance; if you set them on your form and don't change the values for the child controls, the child controls use whatever the form has.

If that's not what you meant, could you clarify what you are asking for? Do you mean "How do I make every form in the project use a particular font?"
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 01-15-2010, 04:21 AM
Azza1988 Azza1988 is offline
Newcomer
 
Join Date: Jan 2010
Posts: 3
Default

hey this is an example of what i mean


Public Function DefaultSettings()

'Store default settings for start up and new documents
'Set back color as white
txtText.BackColor = Color.White
'Set text color as black
txtText.ForeColor = Color.Black
txtText.Font =
txtText.Size =

End Function

thats the basic idea of it, im just not understanding how to get the size and font options set
Reply With Quote
  #4  
Old 01-15-2010, 11:03 AM
AtmaWeapon's Avatar
AtmaWeaponSetting the font size and style with vb code AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I'll show you not only how to do it but how to figure out how to do it yourself for other types! Here's the general algorithm:
  • Figure out what type you need to create.
  • Find the documenation for that type.
  • Find the type's constructors.
  • Choose a constructor and use it to create an instance.*
  • Set any properties of that instance.*
  • Assign the instance to the target property.

* Steps marked this way might require you to create other types, thus recursively sending you to step 1 again.

The Font is actually difficult to create and is a good tutorial.

Step 1 is to figure out what type you need to create. To do this, take a look at the property you are trying to set. You want to set TextBox.Font. So go to http://msdn.microsoft.com. In the search box, search for 'TextBox class'. One of the first results should be the documentation for System.Windows.Forms.TextBox. Click through to "TextBox Properties", then find the "Font" property. You should end up at the documentation for Control.Font. Under the "Property Value" header, we see that the property is of type System.Drawing.Font; this is what we need to create. Click on the link to that type to get ready for the next step.

Step 2 is to find the documentation for the type you need to create. If you followed my instructions in the last step, you're already there. If not, search MSDN for "System.Drawing.Font" and you should get to the page.

Step 3 is to find the type's constructors. From the page you reached in step 2, look for the "Font constructor" link. That should get you to the list of Font constructors.

Step 4 is to choose a constructor and use it. There's a *lot* of different font constructors. The longest one lets you set the font family, size, size unit, style, character set, and whether the font is vertical. The shortest lets you set the font family and point size. Some constructors let you do something special, like use an IntPtr handle to a system font or start from an existing font. Let's presume you just want 12 point Arial font. You might choose this constructor that takes a font family and point size. Oh no! FontFamily is another class; how do we create it? We'll come back to this later. For now, just know that from how far we are right now we can at least write this to get 10-point Arial:
Code:
Dim newFont As New Font(New FontFamily(???), 10)
Step 5 involves setting any properties the constructor didn't set. To do this, look for "Font Properties" in the Font documentation. Font is essentially a read-only type; notice how all of the properties say "Gets..."? If you could set the properties, it would say "Gets or sets..." instead. So if you wanted 10-Point Bold Arial, you have to use a constructor for Font that sets the font style to bold. More on this later.

Step 6 is assigning the value. To do this, you set the property to the new instance you have made. Remember, we haven't looked at how to create a FontFamily yet, so this isn't complete:
Code:
Dim newFont As New Font(New FontFamily(???), 10)
txtText.Font = newFont
Now we're stuck at FontFamily. Find its constructors in the documentation, and you'll see that we can use a String name, the GenericFontFamilies factory type, or a string and a FontCollection. The String name seems easiest, so we'll use that:
Code:
Dim newFont As New Font(New FontFamily("Arial"), 10)
txtText.Font = newFont
Now let's say you want Times New Roman Bold, Italic, and you need it to be 80 pixels tall. Looking at the Font constructors, you might chooose this one, since it lets you set a font family, size, size unit, and style. The result would look like:
Code:
Dim newFont As New Font(New FontFamily("Times New Roman"), _
    80, _
    FontStyle.Bold Or FontStyle.Italic, _
    GraphicsUnit.Pixel)
Now you know how to answer this question for any type.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 01-15-2010, 12:26 PM
Azza1988 Azza1988 is offline
Newcomer
 
Join Date: Jan 2010
Posts: 3
Default

ah brilliant!!! Thanks alot for that awesome article
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code Setting the font size and style with vb code
Setting the font size and style with vb code
Setting the font size and style with vb code
 
Setting the font size and style with vb code
Setting the font size and style with vb code
 
-->