• 0

[VB.NET] Random Colors


Question

Hi,

I'm momentarely programming with GDI+ in Visual Basic .NET 2003.

I'm making a circle that gets filled with a LinearGradientBrush, no big deal so far.

Now I want it to be posible that each time i click a certain button, my brush consists of 2 random colors.

I got the following piece of code :

Private Sub CreateNewCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

		Dim paper As Graphics = Label1.CreateGraphics
		Dim radius As Integer = 280
		Dim circle1 As Rectangle = New Rectangle(300, 0, radius, radius)
		Dim colorA As Color = RandomRGBColor(255)
		Dim colorB As Color = RandomRGBColor(255)
		Dim brush1 As LinearGradientBrush = New LinearGradientBrush(circle1, colorA, colorB, LinearGradientMode.BackwardDiagonal)
		FillCircle(paper, circle1, brush1)

'Return a random RGB color.
	Private Function RandomRGBColor(ByVal alpha As Integer) As Color
		Dim m_Rnd As New Random()
		Return Color.FromArgb(alpha, _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255))
	End Function

Private Sub FillCircle(ByVal paper As Graphics, _
			ByVal circle As Rectangle, _
			ByVal brush As LinearGradientBrush)

		paper.FillEllipse(brush, circle)
	End Sub

Both colorA and colorB are random colors, though they always are the same ... (how come?)

So my circle always has 1 solid color as layout, instead of one color changing to a second one.

Am i overseeying something?

Thx in advance,

dfhwze

Link to comment
https://www.neowin.net/forum/topic/454494-vbnet-random-colors/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Randomize, that's what you need ;)

PS: Quick Re-write of your Code

Private Sub CreateNewCircle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

		Dim paper As Graphics = Label1.CreateGraphics
		Dim radius As Integer = 280
		Dim circle1 As Rectangle = New Rectangle(300, 0, radius, radius)
		Dim colorA As Color = RandomRGBColor(255)
		Dim colorB As Color = RandomRGBColor(255)
		Dim brush1 As LinearGradientBrush = New LinearGradientBrush(circle1, colorA, colorB, LinearGradientMode.BackwardDiagonal)
		FillCircle(paper, circle1, brush1)

'Return a random RGB color.
	Private Function RandomRGBColor(ByVal alpha As Integer) As Color
		Randomize()
		Dim m_Rnd As New Random()
		Return Color.FromArgb(alpha, _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255), _
			m_Rnd.Next(0, 255))
	End Function

Private Sub FillCircle(ByVal paper As Graphics, _
			ByVal circle As Rectangle, _
			ByVal brush As LinearGradientBrush)

		paper.FillEllipse(brush, circle)
	End Sub

Edited by phedot
  • 0

not necessarily

the way you were using the Random object was fine, othe than the fact that you were creating a new Random object in each call to randomcolor()

make a single instance once for the whole application and use that

[edit] the reason behind the colors being the same: the Random object uses the system tick count as a seed if no seed was given to create a seemingly-random number. (there actually is a pattern to the results :p). since you are creating new Random objects in each call to your randomized color method, they return the same value because they have the same seed.

is that understandable?

Edited by Ianmac45
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • This is the reason Microsoft stepped away from Aero for a more subtler transparency. Apple I believe had the perfect balance of transparency and readability, but not with this redesign in its current form. As for people who believe Windows has an 8-bit interface, well, at least you can read stuff.
    • who ever thought this looked good needs to rethink their design credentials.....
    • There's a few here upset Kamala Harris never got the Presidency! lol....
    • Yeah, he identified a whole lot of wasteful spending of US taxpayer money. What's wrong with that? Nothing, unless you had your fingers in the pie of course.
    • How to reduce the annoying Liquid Glass effects on iOS 26 by Aditya Tiwari Apple announced Liquid Glass at WWDC 2025 in all of its glory. It's a new glass-inspired design language from the iPhone-maker making its way to most of its software platforms, including iOS 26, iPadOS 26, macOS 26, tvOS 26, and watchOS 26. Apple markets Liquid Glass as a new translucent material that "behaves like glass in the real world. Its color is informed by surrounding content and intelligently adapts between light and dark environments." The Cupertino giant isn't wrong. The new glass-inspired design does bring a fresh coat of paint and some beautiful visuals to the iPhone and other Apple-made devices. However, as the dust from the mega arrival settles, people are starting to realize the current drawbacks of having too much glass on their device. There have been reports of users, including those at Neowin, experiencing readability issues, background separation issues, and trouble focusing on the content displayed on the screen due to its transparent nature. While these are small nuances on Apple's part that can be fixed without much hassle, it's degrading the initial experience of what the company calls its biggest visual upgrade since iOS 7. That said, there are some workarounds built into iOS 26 that will help you tone down the shortcomings. If you're running the iOS 25 Developer Beta 1 on your supported iPhone model, you can follow these steps to make things a little better: Go to the Settings app on your iPhone. Scroll down and tap on Accessibility > Display & Text Size. Next, you'll find two toggle options: Reduce Transparency and Increase Contrast. When you enable the Reduce Transparency toggle button, iOS 26 can "improve contrast by reducing transparency and blurs on some backgrounds to increase legibility." The other toggle, Increase Contrast, does what its name suggests. It can "increase color contrast between app foreground and background colors." Enabling each of these toggle buttons individually or simultaneously will have different effects on your iPhone's user interface. Control Center is being criticized for the extra transparency, making the UI look cluttered and difficult to focus. Here, the Reduce Transparency button adds a darker background to make the Control Center UI elements stand out. Liquid Glass transparency issues are also prevalent in the Notification Center and the navigation controls of various apps. In the image above, see how the text of the navigation buttons has become unreadable or hard to focus on with certain backgrounds. It's challenging to determine what can trigger such behavior, as one can have countless color combinations on their device. In the image below, this is how these buttons look when both Reduce Transparency and Increase Contrast are enabled. It's still a hit or miss depending on what colors you are dealing with. Part of the blame also goes to the fact that iOS 26 is still an early beta, and the change doesn't render as intended every time. You can also notice the difference in the look and feel of the Control Center when reduced transparency is turned on. On a side note, you can add the Reduce Transparency and Increase Contrast buttons in the Control Center for quicker access. That said, let's wait to see what changes Apple will implement as it continues to gather feedback through the beta program. Hopefully, the software will become more stable when the first public beta of iOS 26 arrives sometime in July. Interested users can try out the latest iPhone update through the developer beta program, noting that early builds might come with unexpected bugs and issues.
  • Recent Achievements

    • One Month Later
      greege earned a badge
      One Month Later
    • Week One Done
      greege earned a badge
      Week One Done
    • Week One Done
      LagFighterZ earned a badge
      Week One Done
    • First Post
      ThatGuyOnline earned a badge
      First Post
    • One Month Later
      5i3zi1 earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      547
    2. 2
      ATLien_0
      230
    3. 3
      +FloatingFatMan
      166
    4. 4
      Michael Scrip
      119
    5. 5
      +Edouard
      91
  • Tell a friend

    Love Neowin? Tell a friend!