|
-
Sep 28th, 2021, 02:15 PM
#1
Thread Starter
Addicted Member
vb.net write color name to file and recall
Hi All... I have several Radio Buttons on one form, and anytime one of them is checked it will assign the color to variable (radioBtnColor):
Code:
Public Shared radioBtnColor As Color
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
radioBtnColor = Color.FromName("Magenta")
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
radioBtnColor = Color.FromName("AliceBlue")
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
radioBtnColor = Color.FromName("Chartreuse")
End Sub
Usage in separate form:
Code:
Dim myClickedColor As String
myClickedColor = Form2.radioBtnColor.ToString
''Write to text file (myClickedColor)
The text file output looks like this:
Code:
JOB1,Color [AliceBlue],A123
JOB2,Color [Chartreuse],A123
JOB3,Color [DarkOrange],A123
How do I retrieve this data in order to create a button color later? Or am I capturing the color incorrectly?
So far what I have is not working and I cannot seem to write data like the actual color to easily assign it later to my buttons such as....
Code:
ButtonColor = Color.AliceBlue
I hope this makes sense and I have explained properly what I am trying to do. Thanks
-
Sep 28th, 2021, 02:19 PM
#2
Re: vb.net write color name to file and recall
You might consider writing the name into the .Tag property of the radio button, then you can write the .Tag to the file rather than the color.ToString. That would give you a smaller thing to write to the file. You could also put the ARGB value (an integer) into the .Tag, and write that. The ARGB value, being only four bytes long, would be more compact than the color name, though that likely doesn't matter any, in this case. It would also be a whole lot harder to read, which might matter.
My usual boring signature: Nothing
 
-
Sep 28th, 2021, 04:13 PM
#3
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
Thanks "Shaggy Hiker".... looking at examples, how would I write this? I get errors about "cannot convert string to color"
-
Sep 28th, 2021, 04:59 PM
#4
Re: vb.net write color name to file and recall
Form1.BackColor = Color.FromName(TextBox1.Text)
Edit: Sorry. Wrong thread
Last edited by .paul.; Sep 28th, 2021 at 05:25 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 28th, 2021, 04:59 PM
#5
Re: vb.net write color name to file and recall
What is the string you are trying to convert? If it is something like what you showed, such as "Color [AliceBlue]", that certainly won't convert. You'd have to pull out the "AliceBlue" part of that string, which is why using the .Tag would be easier.
My usual boring signature: Nothing
 
-
Sep 28th, 2021, 05:44 PM
#6
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
Yes, I want to get the easiest way to both assign the color for each radio button, write it to a text file and easily retrieve the color name from the text file and assign it to the button. I'm just struggling on how to accomplish that.
Also, I did assign color name to the Tag property and I get an error about converting string to color.
-
Sep 28th, 2021, 05:53 PM
#7
Re: vb.net write color name to file and recall
 Originally Posted by mikeg71
Also, I did assign color name to the Tag property and I get an error about converting string to color.
You wouldn’t get an error assigning anything to the .Tag property. If you assign RadioButton1.BackColor.Name to the .Tag property,
You’ll be able to easily retrieve it without errors. To convert a String to a Color, use Color.FromName(yourString)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 29th, 2021, 08:57 AM
#8
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
Thanks for the help on this, its kind of driving me nuts. Here is how I am using the code, if you could, please give me a sample of how this should be written using the .Tag
I start here when RadioButton1 is clicked and I want it to assign a string as a color. So RadioButton1 name is "JOB1" and needs to output "Color.Orange" so that I can use that string from my text file later to create a button on my form: Button1.BackColor = (color from text file) Color.Orange
The way I am using it now is wrong, but this is what I have.
Code:
'Form2
Public Shared radioBtnColor As Color
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
radioBtnColor = Color.FromName("Color.Orange")
End Sub
------------------
Form1
Code:
Dim myClickedColor As Color
myClickedColor = Form2.radioBtnColor
When I write myClickedColor.ToString to the text file, I get: Color [Color.Orange] Which of course I cannot assign later as it doesn't know what that is.
-
Sep 29th, 2021, 09:07 AM
#9
Re: vb.net write color name to file and recall
The .Tag property is type Object, so you can put anything at all in there, but you do have to remember what you put in there. In this case, if you put "Orange" or "Color.Orange" into the tag property, that should work. You would then save .Tag.ToString when you save it to the file. What would be saved then would be whatever you put in there, whether "Orange" or "Color.Orange". Then, when you read it back, you can either put it back into the .Tag, or use it for coloring things, or probably both.
My usual boring signature: Nothing
 
-
Sep 29th, 2021, 10:13 AM
#10
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
I'm lost...
Code:
Public Shared ClickedColor As String 'Should be String or Color? Neither seems to work when I attempt to write
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
RadioButton1.Tag = ("Color.Orange")
ClickedColor = RadioButton1.Tag
End Sub
-
Sep 29th, 2021, 11:30 AM
#11
Re: vb.net write color name to file and recall
First off, turn Option Strict ON. This can be done in Project Properties on the Compile tab, though you should really do so in Options, because you want that on all the time, except for the rare times when you don't.
Option Strict will not allow you to do implicit type conversions, and since implicit type conversions seem to be tripping you up, you want to know about them right away.
The .Tag property is type Object, so it can hold anything. What you put in there is up to you, but what you are putting in there is a string, so when you get that back out, you convert the object to a string with .ToString:
yourString = Tag.ToString
However, in this case you ultimately want a color, and a string is not a color. The string is just the name for a color, so you want to get the right color based on the name that you have. In the code you showed, you almost certainly want ClickedColor to be type Color, so you can't simply say:
ClickedColor = Tag.ToString
because Tag.ToString returns a string, and ClickedColor is a Color, not a string. To turn that string into a color you would use Color.FromName, so the whole line would become:
Code:
ClickedColor = Color.FromName(Tag.ToString)
My usual boring signature: Nothing
 
-
Sep 29th, 2021, 12:01 PM
#12
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
Thanks for the help... Ok, so if I follow this correctly. Yes, Option Strict is ON as well. This is how I set it:
Code:
Public Shared ClickedColor As Color
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
ClickedColor = Color.FromName(Tag.ToString)
End Sub
When I run the form code, I click RadioButton1 and immediately get this error thrown:
Code:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Windows.Forms.Control.Tag.get returned Nothing.
I set the RadioButton1 TAG property to Color.Orange
** EDIT: I get the proper TAG name this way, so this part works.
Code:
ClickedColor = Color.FromName(RadioButton1.Tag.ToString)
Last edited by mikeg71; Sep 29th, 2021 at 12:07 PM.
-
Sep 29th, 2021, 12:34 PM
#13
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
When I create a button on my other form, this is how I am using it but it gets me back to the "Color [Orange]" issue. I guess what I don't understand is, if I am using the variable assigned like this:
Code:
ClickedColor = Color.FromName(RadioButton1.Tag.ToString)
Why would this code on my other form produce the result Color [Orange] ?
This is how I am trying to assign color to the created button on my other form:
Code:
Dim dynamicButton As New Button With {
.Height = 7,
.Width = 7,
.Location = New Point(ptB.X, ptB.Y),
.FlatStyle = FlatStyle.Flat,
.BackColor = Form2.ClickedColor,
.ForeColor = Form2.ClickedColor
}
-
Sep 30th, 2021, 08:14 AM
#14
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
Hi All... any thoughts on this? I have continued trying various things, but have had no luck. This seems so simple but I am very stuck on this.
-
Sep 30th, 2021, 09:23 AM
#15
Re: vb.net write color name to file and recall
At some point, you are calling the .ToString method of the color. You may not be doing it explicitly, but it's happening. You need to do a bit of debugging to figure out where, and when.
Based on the code you posted, it seems most likely that you are putting the color into the .Tag property, not the name of the color.
My usual boring signature: Nothing
 
-
Sep 30th, 2021, 10:01 AM
#16
Addicted Member
Re: vb.net write color name to file and recall
Does this help, you should be saving the name returned in your color variable in both the file and the associated checkbox .tag, not the color variable itself,
Code:
Dim ClickedColor As Color = Color.FromName("AliceBlue") ' .FromName returns a color
RadioButton1.Tag = ClickedColor.Name ' The color variable has a name property
Dim AnotherColor As Color = Color.FromName(RadioButton1.Tag)
Last edited by bmwpete; Sep 30th, 2021 at 10:01 AM.
Reason: Add code tags
-
Sep 30th, 2021, 01:11 PM
#17
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
thanks for the help all... so far, this is what I am using since I was failing to get the correct name from previous:
Code:
Dim fromTextFile As String = Data(7)
Dim color As String = fromTextFile.Split(New Char() {"[", "]"})(1)
The issue with this code is that I have to turn OFF Option Strict due to the error from brackets [ ]
@bmwpete: When I tested yours, I get a return of "Color [empty]".
-
Sep 30th, 2021, 07:34 PM
#18
Re: vb.net write color name to file and recall
Turn Option Strict back on. New Char() expects chars and not strings...
Code:
Dim fromTextFile As String = Data(7)
Dim color As String = fromTextFile.Split(New Char() {"["c, "]"c})(1)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 1st, 2021, 08:43 AM
#19
Thread Starter
Addicted Member
Re: vb.net write color name to file and recall
Thanks paul...that worked great. shaggy hiker & bmwpete, thanks to all of you for your time and assistance with this, its greatly appreciated. Its been a great learning experience and with all of the feedback, I have plenty of testing to still do. Thanks again!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|