-
Sep 28th, 2021, 11:52 AM
#1
Thread Starter
New Member
[RESOLVED] color textbox
hi, I wanted to know if it was possible to insert in a text box the color you want and once inserted change the background of the form.
for example:
textbox(A) =BLUE
background of the form----> blue
if it was possible someone could tell me how to do it?
The idea would be this, obviously is wrong written so
Private Sub t1_TextChanged(sender As Object, e As EventArgs) Handles t1.TextChanged
t1.Text = "write your color"
Me.BackColor = Color.(t1.Text)
End Sub
thank you in advance
-
Sep 28th, 2021, 12:02 PM
#2
Re: color textbox
Yeah, you can do it, but you might consider some other approach. The problem with that approach is that it isn't particularly user friendly. They'd have to get the name right, and might want to write an RGB value as an alternative, or something like that. Using a color picker of some sort would be less error prone and a bit more user friendly.
Whatever you enter has to be turned into a color. The Color structure has the FromARGB member that would allow you to convert a number into a color, so that would be relatively easy. Converting a name into a color would also be pretty easy, except that you'd have to get the name right, and that's not so easy. If you look at the Color structure, you can see all the different named colors that are out there, which is just a tiny subset of the colors that your computer can show. Still, those names are the only ones that could be typed into a textbox and turned into colors, so that's a limitation to typing in names. You'd also have to spell them correctly, which is a second limitation. A color chooser of some sort would allow you to get ALL the colors that are possible.
My usual boring signature: Nothing
-
Sep 28th, 2021, 12:22 PM
#3
Thread Starter
New Member
Re: color textbox
I'm studying from the book and I have this exercise that I have to do, that's why I was asking if it was possible because in the book there isn't an example with fromARGB or a simple exercise like this there is only an exercise where I change color with colordialog through a button. anyway thanks for answering
this is the exercise:
Create a form with a text box from which the user can set the background color.
I tried to do this now, it works...but it would be too long and useless.
for now I have come to this, although it is a bit pointless and long but it works
Private Sub t1_TextChanged(sender As Object, e As EventArgs) Handles t1.TextChanged
If t1.Text = "blue" Then
Me.BackColor = Color.Blue
End If
End Sub
-
Sep 28th, 2021, 02:16 PM
#4
Re: color textbox
What you want to look at is Color.FromName
I haven't used this in a long time, and not for quite this situation. In my case, the name would never be wrong. I'm not quite sure what happens if the string passed in doesn't match any name. I'd guess that it would return black, in those cases, but I'm not sure. An exception is the other possibility.
My usual boring signature: Nothing
-
Sep 28th, 2021, 08:20 PM
#5
Re: color textbox
Originally Posted by Shaggy Hiker
I'm not quite sure what happens if the string passed in doesn't match any name. I'd guess that it would return black, in those cases, but I'm not sure.
From the documentation:
If the name parameter is not the valid name of a predefined color, the FromName method creates a Color structure that has an ARGB value of 0 (that is, all ARGB components are 0).
That means that you won't actually get any warning if the user enters a nonsense value. If you want to validate the input, you can use the KnownColor enumeration and the Enum.TryParse method.
-
Sep 29th, 2021, 05:11 AM
#6
Thread Starter
New Member
Re: color textbox
ok, but how do i use these codes? some example if possible, please. i know it could be a silly question, but for me it is not, i want to learn and understand where i am wrong. to me just replace the : RED , or WATER GREEN, etc.. that I insert in text box
in
texbox1=" "
me.backcolor= color.(textbox1.text)
but this is wrong"...color.(textbox1.text)"
-
Sep 29th, 2021, 05:44 AM
#7
Re: color textbox
Shaggy told you to use the Color.FromName method but you haven't done that. He also provided a link to the documentation for the method. ALWAYS read the documentation first. You should have a link to the home page of the Microsoft Docs site in your browser favourites/bookmarks but you can go straight to the topic for a type or member by clicking it in the code window and pressing F1.
-
Sep 29th, 2021, 07:12 AM
#8
Thread Starter
New Member
Re: color textbox
when shaggy answered me, I went to see immediately the link he put: color.Fromname
and look for other explanations on the internet.
same thing when you wrote : KnownColor and the Enum.TryParse method.
but I understood little, and I hoped that with some example of yours a little more detailed maybe I would have understood better how to do it.
the point is that i'm starting to learn vb from only 1 week and half, i don't know any other language, and the book i have at school starts from vb, i would like to learn and improve, but the book is bad.
-
Sep 29th, 2021, 09:12 AM
#9
Re: color textbox
MSDN documentation typically included documentation. In this case, I'm only seeing the example in C# for some reason. Still, the first line is an example that uses FromName:
Code:
Color slateBlue = Color.FromName("SlateBlue");
Which would be this in VB:
Code:
Dim slateBlue As Color = Color.FromName("SlateBlue")
So, you could see that a string is passed to FromName, in which case you could do this:
Code:
Dim yourColor As Color = Color.FromName(textbox1.Text)
My usual boring signature: Nothing
-
Sep 29th, 2021, 09:14 AM
#10
Re: color textbox
Originally Posted by Shaggy Hiker
I'm only seeing the example in C# for some reason.
You change the language using the drop-down at the top of the page. Some examples aren't available in VB but this one is.
-
Sep 29th, 2021, 09:16 AM
#11
Re: color textbox
I glanced at it, and didn't see the drop down. I expect I was looking straight at it, too, because I was looking for such a drop down.
My usual boring signature: Nothing
-
Sep 29th, 2021, 09:27 AM
#12
Thread Starter
New Member
Re: color textbox
After going into a depressive state for hours, and not sleeping most of the night, and trying to figure out the codes.... I succeeded.
working code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim testo As String
testo = t1.Text
t1.Text = ("")
Me.BackColor = Color.FromName(testo)
End Sub
-
Sep 29th, 2021, 11:37 AM
#13
Re: color textbox
What does this line do:
t1.Text = ("")
It might be useful, or not. What it will do is clear the textbox, which you may want, but it has no purpose when it comes to assigning the color, so you could also have written the same this way:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.BackColor = Color.FromName(t1.Text)
t.Text = ""
End Sub
There is no particular advantage to either one. In theory, this way might be ever so slightly faster, since it avoids a single assignment, but that really shouldn't matter, and may be optimized away.
My usual boring signature: Nothing
-
Sep 29th, 2021, 12:01 PM
#14
Thread Starter
New Member
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
|