I got a console application that draws line in a 3D CAD program. Now to make this more clear i want to change these lines in different colors.
My code read variables from a text-file and calculates data from it and then makes a lines from this calculated data.
This process gets repeated with every line in the text file wich contains data.
Now i want visual basic to change the color everytinme a new line is drawn, so i get different colored lines.
I tried using a For.. To.. Step method, but this didnt work. I also tried to use the variables from my text file( these are different so when a new line got read the RGB code will change) but this will geve me only a lot of blue colors.
Any suggestions?
EDit:
This is what i use to draw the curves, the RGB code has to cahnge everytime when a line with new data is made:
' Creating a Curve2d object by using the above defined points
objLineString = objLineStrings.AddByPoints(PointCount:=points, points:=dataArray)
objGeometricStyle = objLineString.Style
color = objGeometricStyle.LinearColor
objGeometricStyle.LinearColor = RGB(0,0,0)
What about :
Dim rand As New Random() ' Used to generate random numbers
Dim colors(100) as Integer
' Create the colors
For i as Integer = 0 to 100 Step 1
colors(i) = RGB(rand.Next(256), rand.Next(256), rand.Next(256))
Next
For i As Integer = 0 To 100 Step 1 ' Adjust to your needs
' Creating a Curve2d object by using the above defined points
objLineString = objLineStrings.AddByPoints(PointCount:=points, points:=dataArray)
objGeometricStyle = objLineString.Style
color = objGeometricStyle.LinearColor
objGeometricStyle.LinearColor = colors(i Mod 100) ' Mod returns the remainder of i / 100, so it's always less than 100.
Next
This won't always give you "pretty" colors, but they'll be different for each line. If you want control over the generated colors, you could set up an array of predefined colors and use these in your iteration.
Related
I have a text box on an MS-Access 2007 Form (its old, but its what I have) called text_record. I want to display the characters in the text box with a positional line directly underneath the text. e.g.
ABCDEF
123456
I used the following code to set the field:
dim x as string
x = Forms![mapped field names].Form![text_record] & vbcrlf
For i = 1 To Len(x) - 1
x = x & CStr(i Mod 10)
Next
Forms![mapped field names].Form![text_record] = x
The code works and shows the integer positions underneath, but the characters are not aligned directly above the numbers. I need the characters directly above the positional number so a user can see what is in a particular column.
I tried several different font types, all said they were monospaced, and they all fail to align directly above the number. Depending on the text in the record, the position was off as much as 5 columns.
I must be missing something simple.
Thanks in advance for any and all responses
Use a fixed font. Courier New is a fixed size.
So, on the form, I drop in a text box, and right below another text box
(I set the border to transparent, and I set enabled = false (so user can't edit, or tab into).
so we have this:
And the font for both text boxes is this:
And my code (it puts numbers below as you type).
Private Sub Text0_Change()
Dim sResult As String
Dim i As Integer
For i = 1 To Len(Text0.Text)
sResult = sResult & i Mod 10
Next i
Me.Text2 = sResult
End Sub
So, we see this (and I typed in "i" and "l", since they are small compared to numbers, and we get this:
so, not sure what font you used, but Courier new is a fixed font, and should work as per above.
You might be better off splitting the text and putting it into a multi-column list box.
I wrote some macro code in Word (Office 365) to set the color of a shape outline to one of the theme colors. The code for doing that to a shape looks like this:
shape.line.foreColor.ObjectThemeColor = wdThemeColorAccent2
By assigning a 'wdXX' color to the ObjectThemeColor field, the color of the line around the shape will automatically change when the document ColorTheme is changed.
My problem (or the first weirdness) is that when I assign Accent2 with the code above and then do: select the shape, Menu, Format, Shape Outline, and hover over the color box with a red outline (which marks the active line color), the tooltip says "Turquoise, Accent 1" not "Accent 2."
I would have expected the wdThemeColorAccent2 color to be called Accent 2 in the tooltip, but it is not.
The second problem is that there is apparently no way for me to assign the last color shown in the menu using macro code. Because of the offset (Accent 2 in code = Accent 1 in the menu), I would need to use wdThemeColorAccent7 in code to assign the last color shown in the menu (labeled Accent 6 in the tooltip).
I'm wondering if this is a bug in Word (it sure looks like it to me), or if I am doing something wrong. To reproduce the situation, I created a simple empty rectangle, selected it, and ran the line of code above to change the outline color of the shape. Here's a little subroutine that illustrates the problem (select your shape before running the subroutine).
Sub TestAccent()
Dim shp As Shape
Set shp = selection.ShapeRange(1)
shp.line.foreColor.ObjectThemeColor = wdThemeColorAccent4
shp.line.Weight = 0.5
shp.line.Visible = True
End Sub
I believe the colors in the "theme scale" (see image below) don't correspond to the names of the WdThemeColorIndex, but rather to the underlying numerical value. If you look in the VBA Editor's Object Browser (F2), and search wdThemeColorAccent you'll get the full list. Click on a member in the list and at the bottom you'll see the numerical value.
The value 0 is assigned to MainDark1 and isn't recognized by VBA. Values 1, 2 and 3 are assigned to MainLight1, MainDark2 and MainLight2 which are black, white and the first entry in the image (These colors repeat in the last four enumerations for background and text). Values 4 (wdThemeColorAccent4) through 9 (wdThemeColorAccent6) correspond to the remainder of the colors in the image below. (Note: more discussion after image!)
So, no, I don't think it's a bug, just your expectations don't match what the developers were thinking when they assigned the numerical enumeration to the enumeration names. Or maybe the people who designed the color schemes changed their minds after the VBA code was locked down... And I imagine the names you see in the tooltips are another step removed from the VBA. You might find the information in this article helpful.
If you use the values, rather than the names, things could be less confusing. Or, define your own Enum:
Public Enum ColorSchemeAccents
Accent1 = 3
Accent2 = 4
Accent3 = 5
Accent4 = 6
Accent5 = 7
Accent6 = 8
Accent7 = 9
Accent8 = 10
End Enum
Sub TestAccent()
Dim shp As Shape
Set shp = Selection.ShapeRange(1)
shp.Line.ForeColor.ObjectThemeColor = ColorSchemeAccents.Accent8
shp.Fill.ForeColor = RGB(250, 250, 250)
shp.Line.Weight = 2
shp.Line.Visible = True
End Sub
Although the ColorFormat object's .ObjectThemeColor is defined as a wdThemeColorIndex in fact the value depends on context.
If it is a Word object - such as text, then you should use the wdThemeColorIndex constants, but if it is an Office object - such as shape, then you have to use the msoThemeColorIndex constants. These are weirdly NOT the same - mostly the mso constants are one more than the wd constants, but not for the Background1&2 and Text1&2 cases - Text1&2 are the same in both cases, but for Background1&2 mso is two more than wd.
A side effect of this is that it appears impossible in VBA to set the Background2 colour, as its mso value is 16 and so out-of-range BUT if you use the native GUI to set it, it can be set to 16!
Looks really poor design/implementation that needs cleaning up!
I have a x,y scatter chart with lines connecting the dots from one source. I have between 1 and 8 lines and I need to have the MarkerStyle assigned to each line. Since the lines are not fixed and depend on the current data, I can't say which lines are there.
In general I could just assign
ActiveChart.FullSeriesCollection(i).MarkerStyle = xlMarkerStyleAutomatic
But that also assigns unwanted/unreadable Markers. So could I create a Collection with the Markerstyles I want and than assign that?
I tested
Dim colMarker As Collection
Set colMarker = New Collection
colMarker.Add "xlMarkerStyleCircle"
colMarker.Add "xlMarkerStyleSquare"
colMarker.Add "xlMarkerStyleTriangle"
With ActiveChart.FullSeriesCollection(i)
.MarkerStyle = colMarker(1)
End With
But the error msg is wrong type
What type do I need?
Thanks Kaz
You can set the marker style for each series individually:
Dim s as Series
s = ActiveChart.SeriesCollection.NewSeries
s.MarkerStyle = xlMarkerStyleCircle
Edit:
To assign different marker styles use the approach you suggested, but when adding styles to the collection it should be done like this:
colMarker.Add xlMarkerStyleDiamond
etc, ie without quotation marks.
I need to write some fixed-width font (i.e Courier New or Consolas) text to a .net Winforms window in the Paint event - not using a label or any other winforms control - rather using a graphics object method to render the text onto the form's client area.
I am having difficulty aligning text on different lines under headings even though it is fixed width font. How can I get a precise measurement of the width of a single character in the fixed-width font? How can I get 2 lines to print out aligned horizontally in successive text out calls?
For example:
Heading 1 Heading 2
Short Other text
A bit longer Still aligned?
I need a separate call to render each cell of text under Heading 2. For argument's sake - let's say column 1 items are printed in black and column 2 are printed in blue - we can't use the same text out call for the entire line.
Graphics.MeasureString may be what you are looking for.
Ok so here is the code that works the way I want using MeasureString. A string is printed twice. One time using a single call to DrawString. The second time, character by character in a loop. What I needed was that the 2 strings should appear identical but I was having trouble getting the correct horizontal position of each char when drawing the second string. You can put this code into the Paint event of a form to try it out (set the form font to Consolas or other fixed width font):
Dim i As Single
Dim sf As StringFormat
Dim String1 As String = "Here is out test string"
Dim CharSizeF As SizeF
sf = StringFormat.GenericTypographic
CharSizeF = e.Graphics.MeasureString(String1, Me.Font, 10000, sf)
CharSizeF.Width /= String1.Length
e.Graphics.DrawString(String1, Me.Font, Brushes.Black, 0, 0, sf)
For Each c As Char In String1
e.Graphics.DrawString(c.ToString, Me.Font, Brushes.Black, i * CharSizeF.Width, CharSizeF.Height, sf)
i += 1
Next
Microsoft also helped with:
http://msdn.microsoft.com/en-us/library/957webty.aspx
To obtain metrics suitable for adjacent strings in layout (for example, when implementing
formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that
takes a StringFormat and pass GenericTypographic. Also ensure the TextRenderingHint for the
Graphics is AntiAlias.
I am doing a program using vb6 which is stored datas from mouseclick in term of coordinates. I succeeded doing the first stage which is displaying the clicked coordinates. My problem now is I need to save the coordinate in term of variables so i can call them back to use for another purpose for example to find distance between two points.
if its just two coordinates its easier to find the distance. but when it comes to many coordinates I am stuck. I tried to do an array to stored the data inside loop
1. InputX(ListNum, 0) = Int(x)
2. InputY(ListNum, 1) = Int(y)
3. ListNum=ListNum+1
when I try to call for InputX(2,0) = Text1.Text or Text1.Text=InputX(2,0) none of them working. It seems that the data will be erased after I do a mouseclick
Is there any way which I can set the dynamic variables which stored each my clicked coordinates such as Input1,Input2,Input3 ...InputN
I do this in VB6.
The problem you're having is that you're using a two dimensional array there. A two dimensional array looks like a table. That's not what you want though. You want a list of pairs of points. So, create a structure with two integers in it, x and y, and make an array of those structures:
'Right underneath your Class Form1 declaration:
Structure Point
Dim x As Integer
Dim y As Integer
End Structure
Dim length As Integer = 10
Dim Points(length) As Point
'When you want to start using your points put this in the method:
Points(0).x = 10
Points(0).y = 10
Points(1).x = 20
Points(1).y = 40
Dynamic variables in VB6
First you declare the variable without giving size:
Dim InputX() As String
Then you give for the first time size to your array using ReDim:
ReDim InputX(5)
If you want to preserve whatever data is already in your array you use ReDim Preserve:
ReDim Preserve InputX(10)
I hope this is what you need.
it appears that the first method
Text1.Text=InputX(2,0)
is working. I just need to declare x and y As Single