I wanna make color selector, when picked color write color code to text box. I created color dialog and text boxes, how make a rgb and hex codes from picked color?
I'm trying this code, but it have a problem:
TextBox1.Text = ColorDialog1.Color.R + ", " + ColorDialog1.Color.G + ", " + ColorDialog1.Color.B
Getting:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string ", " to type 'Double' is not valid.
Something like this will get you what you need...
Dim MyColor = Color.LightGreen
Dim R = MyColor.R
Dim G = MyColor.G
Dim B = MyColor.B
Dim HexString = String.Format("{0:X2}{1:X2}{2:X2}", R, G B)
Visual Basic is usually pretty accommodating when you try to combine numbers and text, automatically converting the number to a string to make the statement work. But the Color.R, G and B properties are a bit special, they are of type Byte. That type didn't exist in earlier versions of VB. They didn't add the automatic conversion.
Best thing to do here is to use the composite formatting feature supported by the String.Format() method:
With ColorDialog1.Color
Label1.Text = String.Format("{0}, {1}, {2}", .R, .G, .B)
End With
And for the hex version just change the formatting string:
Label1.Text = String.Format("#{0:x2}{1:x2}{2:x2}", .R, .G, .B)
Related
I want to call a random color between 8 different colors and display it in a label as its backcolor in Visual Basic. How can I display the colors without repeating a color that has been called out on a specific label?
For example, if color red is called out and displayed in labelA1, how can I make sure that color red won't be called out and displayed in labelB1, labelC1 or labelD1 but can be called out in labelA13 or labelB16?
Below is a picture to help understand the example above.
Use this code to make a list of colors then knock them off the list every time one is used.
Private Function RandomizeLabelColors() As Integer
Randomize()
Dim listOfColors As List(Of Color) = {Color.Red, Color.Blue, Color.Green, Color.Orange}.ToList
Dim labels As List(Of Label) = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8,
Label9, Label10, Label11, Label12, Label13, Label14, Label15, Label16}.ToList
Dim i As Integer = 0
Do Until listOfColors.Count = 0
Dim targetIndex As Integer = Int(Rnd() * listOfColors.Count)
labels(i).BackColor = listOfColors(targetIndex)
labels(i + 4).BackColor = listOfColors(targetIndex)
labels(i + 8).BackColor = listOfColors(targetIndex)
labels(i + 12).BackColor = listOfColors(targetIndex)
listOfColors.RemoveAt(targetIndex)
i += 1
Loop
Return 0
End Function
I have my labels in a 4x4 grid.
-Mg
I am trying to set ≤ character into a VB6 Label.
Looking at https://en.wikipedia.org/wiki/List_of_Unicode_characters
The code would be 2264 .
Label.Text = Chr(2264) generates an error
Label.Text = ChrW$(2264) sets a question mark "?"
Does anyone know how to get this ≤ character
Label.Text = ChrW(&H2264) ' <-- &H for Hexadecimal
2264 is the Hexadecimal code, you can see it here.
In Decimal, it is ChrW(8804).
The stock Symbol font contains that symbol at an ansi codepoint and so works without requiring unicode awareness, as a contrived example with 3 autosizing labels:
lbl_left.Caption = "999"
lbl_middle.Font.Name = "Symbol"
lbl_middle.Caption = ChrW$(&HA3)
lbl_middle.Left = lbl_left.Left + lbl_left.Width + Me.TextWidth(" ")
lbl_right.Caption = "1000"
lbl_right.Left = lbl_middle.Left + lbl_middle.Width + Me.TextWidth(" ")
I want to add the lable box value in vb6
Label1 = 200
Label2 = 500
'Adding
Label3 = Label1 + Label2
'Showing output as
Label3 = 200500
I want add the 2 values
Expected Output
Label3 = 700
What was the problem in my code
Need code help
The two answers are correct, but neither of it explain to you why this happens. VB 6 (or 5 or 4 or 3) has a default property for the controls. In the case of the label, the default property is caption. Since caption is a string, and string can be concatenate using & or +, VB pickup the type and then it does the math (in this case, concat).
Label3= val(Label1) + val(Label2)
This works good.. Also you can use Cint or any other convert to number function.
long time haven't worked with VB6 but try
Label3.caption = val(Label1.caption) + val(Label2.caption)
Do something like this:-
textbox3.text = val(textbox1.text) + val(textbox2.text)
i'm tring to create some label programmatically, the code doesn't return any error but i cannot see any label in my window.
dim dr As DatabaseRecord
dim sql As String
sql = "SELECT * FROM pack WHERE applicabilita_modello LIKE '%" + versione + "%'"
dim rs As RecordSet = database.SQLSelect(sql)
dim i As Integer = 1
dim test(10) As Label
while not rs.EOF
test(i) = new Label
test(i).Text = rs.Field("descrizione").StringValue
test(i).Left = me.Left
test(i).Top = me.Top * i
test(i).Enabled = true
test(i).Visible = true
rs.MoveNext
i = i + 1
wend
rs.Close
i've verified that the recordset contain some data, the loop work correctly but no label is shown and cannot understand why.
thanks for any help
There are two ways to create controls at runtime in Real Studio. The first is to create a control array. You could name the control MyLabel and give it an index of zero. Then your code would be:
test(i) = new MyLabel
The second is to use a ContainerControl. This container would contain a label and because you can add them to your window (or other container) using the NEW command and using the ContainerControl.EmbedWithin method.
I generally prefer the ContainerControl approach for many reasons, but mostly because control arrays make the logic a big more convoluted. The only drawback with containers is that it requires Real Studio Professional or Real Studio Enterprise.
http://docs.realsoftware.com/index.php/UsersGuide:Chapter_5:Creating_New_Instances_of_Controls_On_The_Fly
http://docs.realsoftware.com/index.php/ContainerControl
I'm trying to create a new macro that takes the currently selected text and puts curly braces around it (after making a newline), while, of course, indenting as needed.
So, for example, if the user selects the code x = 0; and runs the macro in the following code:
if (x != 0) x = 0;
It should turn into:
if (x != 0)
{
x = 0;
}
(Snippets don't help here, because this also needs to work for non-supported source code.)
Could someone help me figure out how to do the indentation and the newlines correctly? This is what I have:
Public Sub NewScope()
Dim textDoc As TextDocument = _
CType(DTE.ActiveDocument.Object("TextDocument"), TextDocument)
textDoc.???
End Sub
but how do I figure out the current indentation and make a newline?
Sub BracketAndIndent()
Dim selection = CType(DTE.ActiveDocument.Selection, TextSelection)
' here's the text we want to insert
Dim text As String = selection.Text
' bracket the selection;
selection.Delete()
' remember where we start
Dim start As Integer = selection.ActivePoint.AbsoluteCharOffset
selection.NewLine()
selection.Text = "{"
selection.NewLine()
selection.Insert(text)
selection.NewLine()
selection.Text = "}"
' this is the position after the bracket
Dim endPos As Integer = selection.ActivePoint.AbsoluteCharOffset
' select the whole thing, including the brackets
selection.CharLeft(True, endPos - start)
' reformat the selection according to the language's rules
DTE.ExecuteCommand("Edit.FormatSelection")
End Sub
textDoc.Selection.Text = "\n{\n\t" + textDoc.Selection.Text + "\n}\n"
Of course the amount of \t before the { and } and Selection depend on the current indention.
Since there is a difference between selected Text and Document data, it's hard to find out where the cursor is within the document's data (at least in Outlook it is).
The only way I figured out how to do this in Outlook is to actually move the selection backwards until I got text that I needed, but that resulted in undesirable effects.
Try taking the selection start, and using that position in the document text, looking at that line and getting the amount of tabs.
I would think there wouldn't be formatting characters in VStudio.