Saving textfield background Color

I have a combobox that allows you to select the background color of textfields, how would I go about saving the selection to an SQLite Database.

Thanks Shane

Use a text field and save the hex values of the color into the database.

Sorry Beatrix but I don’t understand what you are saying. I need to save the data from the textfield as well as the chosen textfield color.

Convert Color to String to save it in a database or in a textfile. Then convert that string back to a color object when you want to reuse it, for instance:

Function imColorToString(Col As Color) As String Return Str(Col) End Function

Convert String back to color:

Function imStringToColor(ColorString As String) As Color Dim v As Variant = ColorString Dim c As Color = v.ColorValue Return c End Function

Thanks Oliver, I am only learning so I need to know how to save it to the database.

Thank Shane

[quote=112226:@Shane Rampling]Thanks Oliver, I am only learning so I need to know how to save it to the database.

Thank Shane[/quote]
Inserting a color string into a database is exactly the same as inserting any text into a database. You can learn and find examples about the Xojo included SQLite database here:

Look at the example section:

More details about SQLite:

Study the example database projects coming with Xojo, like this one in your Xojo Example folder:
/Example Projects/Database/SQLite/SQLiteExample.xojo_binary_project

Download the Framework User Guide and look at chapter 4 (Databases).

Watch this SQLite webinar:

Thanks Oliver, I know how to save and retrieve data from a SQLite Database, I just don’t know what to do or where to start with this.

Cheers Shane.

It is not clear to me what exactly you are asking.

In the code you gave me above, I don’t know how to implement it to save “what” to the database, what I am trying to achieve is say someone selects green, I then have a row of textfields that turns green when the data that is entered is saved and retrieved.

Maybe you start with writing some step-by-step pseudo code which describes what exactly you are trying to do, but not here on the forum but on a piece of paper for yourself. When the program flow is clear, then you start writing each step in actual code.

The information that you need has all been given.

… why are you using a combobox to select a color? There is a standard color picker:

Hi oliver, here is a link to another question I asked that may make it a bit clearer on what I am trying to achieve.

link text

What it is, is a golf program that if someone enters data for say the red tees on the golf course the the data is displayed on a red background in the textfields.

I have it working the right way with the answer in the post I quoted, but don’t know how to go about saving the data for the background color to the database.

Cheers Shane.

Okay, you must make a decision about WHEN to save those color values to a database. You can add a save button or you save the background colors of each textfield whenever the selected item has changed in your combobox.

You add a method (aka “SaveColors”) which reads the backcolor of each textfield, for instance: Str(TextField1.backcolor), and then inserts or updates those strings to your database.

You call that method from the combobox1.change event handler (Or however your combo is named).

You write a second method (“RetrieveColors”) where you retrieve those saved strings and reaply them to the textfields (see imStringToColor function above in this thread).

You can call your method “RetrieveColors” from Window1.Open event handler.

Thanks Oliver, will give it a go.


Thanks Oliver, I think it’s a project for a bit further down the track when I learn a bit more.

Cheers Shane.

Hi Oliver, this is what I have come up with and it seems to be saving the color, when I go into the SQLite Database and have a look

This code is in the change event of the combobx

[code] Dim d As new dictionary
d.value(“Mens”) = &cFF00FF 'RGB(255,0,255)

Dim myColor As string = combobox1.Text
if d.hasKey(myColor) Then
test1.backcolor = d.value(myColor)
test2.backcolor = d.value(myColor)
end if[/code]

This is the method I have came up with

[code] dim mycolor as string
mycolor = Str(Test1.backcolor)
dim dr as New DatabaseRecord
dim rs as RecordSet

if golfer <> nil then
rs = golfer.SQLSelect("select * from test where PK = "+ str(testpk))

end if

dr.column(“test1”) = (mycolor)
app.golfer.InsertRecord “test”, dr


msgbox"New Record Added"[/code]

It saves the color as &h00FF00FF, is this correct.

The color string looks good to me.

You would have to try to retrieve it from the database, convert it to color and re-aply it to the background of a textfield. Is the same color showing up?

Here some quick and dirty code, not tested:

[code]Dim rs As RecordSet
rs =golfer.SQLSelect( “SELECT test1 FROM test WHERE PK = '” + Str(testpk) + “’”)

If rs <> Nil And Not rs.EOF Then
Dim v As Variant = rs.Field(“test1”).StringValue
Test1.backcolor = v.ColorValue
End If[/code]

Thanks Oliver that works fine, one more question, there is test1 to test21 what would I need to do to fill them all at once.

Cheers Shane

Have tried this but gives an error :

[code] dim i as integer
for i = 1 to 21

  Dim v As Variant = rs.Field("test1").StringValue
  Test(i).backcolor = v.ColorValue

[quote=113007:@Shane Rampling]Thanks Oliver that works fine, one more question, there is test1 to test21 what would I need to do to fill them all at once.

Cheers Shane[/quote]
If they all have different backcolors, then you will have to save, retrieve and assign those colors individually.

If there is one color for all those fields I probably would do as follows:

  1. Add a class to your project, name it ColorTextField and set its Super to TextField
  2. Change the Super of all of your Textfields which need to be colored to ColorTextField
  3. Now you can change the colors of the textfields in a loop, something similar to this:

[code] Dim c As Color
If SelectColor(c,“Choose color”) Then

Dim i,j As Integer
j = Window1.ControlCount
For i = 0 to j-1
  If Window1.Control(i) IsA ColorTextField Then
    ColorTextField(Window1.Control(i)).BackColor = c
  End If

End If[/code]

Hi Oliver, there are 4 rows of textfields in the project and each row has 21 textfields, each row will have it’s own color selected at the start of the row.

Cheers Shane