473,324 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Parsing

TNT
48
Can you change a string to a colour and vice versa, and convert a string to a font and vice versa on Visual Basic 2005 Express Edition? If so, how do you do it
Feb 14 '07 #1
7 1482
Killer42
8,435 Expert 8TB
Can you change a string to a colour and vice versa, and convert a string to a font and vice versa on Visual Basic 2005 Express Edition? If so, how do you do it
I'm not sure exactly what it is you're asking, but it sounds as though you might want to check out the RichTextBox control.
Feb 14 '07 #2
TNT
48
I want a program to suck a colour out of a file (it will be a string) and the program should be able to change the string (e.g. "255, 188, 0") into a proper colour and display it. I want it to do the same thing with a font.
Feb 15 '07 #3
Killer42
8,435 Expert 8TB
I want a program to suck a colour out of a file (it will be a string) and the program should be able to change the string (e.g. "255, 188, 0") into a proper colour and display it. I want it to do the same thing with a font.
I guess you can take the string, Split it on the commas, assign to numeric variables, then use the RGB() function to put them together. For example, here's a function which you can pass the string to, and it will return the colour value.
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim ColourPart() As String
  3.  
  4. Private Function ExtractedColour(ByVal s As String) As Long
  5.   ColourPart = Split(s, ",")
  6.   ExtractedColour = RGB(ColourPart(0), ColourPart(1), ColourPart(2))
  7.   Erase ColourPart
  8. End Function
Feb 15 '07 #4
TNT
48
What about the font?
Feb 15 '07 #5
Killer42
8,435 Expert 8TB
What about the font?
Well, what are you seeing in the file, and what do you want to do with it? More detail, please.
Feb 15 '07 #6
TNT
48
I need the program to convert a font to a string and a string to a font. I can't pass the font directly to the text file, because it needs to be converted to a string. Also, I can't use a string from a text file to set the font, because I need to convert the string to a font to use it. How do I do this? There must be some general built-in function to do this.

Also, some thing I didn't think of earlier:
How do I convert the colours (like 255, 188, 0) to strings?
How do I convet preset colours (like Color.LightGreen) to strings?
How do I convert strings (like "Color.LightGreen" to colours?
I look forward to your reply.
Feb 15 '07 #7
Killer42
8,435 Expert 8TB
I need the program to convert a font to a string and a string to a font. I can't pass the font directly to the text file, because it needs to be converted to a string. Also, I can't use a string from a text file to set the font, because I need to convert the string to a font to use it. How do I do this? There must be some general built-in function to do this.

Also, some thing I didn't think of earlier:
How do I convert the colours (like 255, 188, 0) to strings?
How do I convet preset colours (like Color.LightGreen) to strings?
How do I convert strings (like "Color.LightGreen" to colours?
I look forward to your reply.
Converting a colour to a string is relatively simple. The question is, do you actually need to save it as RGB values, or would it be sufficient to save the colour number? In other words, why store 255, 188, 0 when you can just store 48383 (decimal) or BCFF (hex)? They're just different ways of representing the same number, but separate RGB values entail extra work in splitting up the colour into it’s components (and putting them back together).

As for the "preset" colours like LightGreen, I think you will find that they are just a convenient way of referring to a number. You can just save the number - does it matter that it was originally referenced by a name?

Converting a string like "Color.LightGreen" back to an actual colour might involve a lot of IF...ELSE... type processing. But if you just store the number rather than the name, that should be all you need to worry about when reading it back.

Converting a font back and forth may not be too hard. Remember that I'm using VB6, so things may work differently for you. But given a font object, you can decide which properties you're interested in and just place them in your string. I've put together a working sample in VB6 which illustrates one method of converting both ways between a font and a string. You will probably need to adapt it for VB.Net, sorry (best I can do in my lunch hour). Anyway, here are the steps to create the sample...

Create a form called Form1, and place on it a Textbox called Text1. Set the font of the form (or textbox) to something visually quite different to the default (it doesn't matter what, as long as you can see a difference between text on the form and in the textbox). Create these two functions in the code module...
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. DefLng A-Z
  3. Private MyArray() As String
  4.  
  5. Public Function FontToString(f As StdFont) As String
  6.   With f
  7.     FontToString = .Bold & "|" & _
  8.                    .Italic & "|" & _
  9.                    .Size & "|" & _
  10.                    .Strikethrough & "|" & _
  11.                    .Underline & "|" & _
  12.                    .Weight & "|" & _
  13.                    .Name
  14.   End With
  15. End Function
  16.  
  17. Public Sub StringToFont(s As String, f As StdFont)
  18.   Erase MyArray
  19.   MyArray() = Split(s, "|")
  20.   With f
  21.     .Name = MyArray(6)
  22.     .Bold = (MyArray(0) = "True")
  23.     .Italic = (MyArray(1) = "True")
  24.     .Size = Val(MyArray(2))
  25.     .Strikethrough = (MyArray(3) = "True")
  26.     .Underline = (MyArray(4) = "True")
  27.     .Weight = Val(MyArray(5))
  28.   End With
  29. End Sub
In the click event for the form, place this code...
Expand|Select|Wrap|Line Numbers
  1. Dim s As String
  2. s = FontToString(Me.Font)
  3. StringToFont s, Text1.Font
  4.  
As an example of what this does, here’s the result when I invoked the FontToString function from the Immediate window...
Expand|Select|Wrap|Line Numbers
  1. Print FontToString(Form1.Font)
  2. True|True|15.75|True|True|700|Palatino Linotype
Oh! I need to correct myself. I thought that names like LightGreen were just simple numeric constants like vbGreen and so on. But I had a look at the doco on the MSDN site and of course it’s not that simple. I don’t know yet how the Color structure works. But we can work on that, if necessary.
Feb 16 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
0
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
5
by: randy | last post by:
Can some point me to a good example of parsing XML using C# 2.0? Thanks
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
7
by: Daniel Fetchinson | last post by:
Many times a more user friendly date format is convenient than the pure date and time. For example for a date that is yesterday I would like to see "yesterday" instead of the date itself. And for...
1
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.