problem with control array

setep

New member
Joined
Feb 24, 2009
Messages
2
Programming Experience
Beginner
hello, i am making a program that loads a grayscale image and allows the user to add color based on the grayscale value of a pixel. i have it working and am trying to do it better or different, instead of having all the buttons at design time im wanting to create a button array. each button representing the grayscale value that can be changed to color.

VB.NET:
Private Sub showbuttons()
        Dim bm As Bitmap = picbox1.Image
        Dim RRc, GGc, BBc As Integer
        
        Dim lowval, highval As Integer

        Dim gsbuttonarray As buttonarray
        Dim gsarray As Object

        Dim topval, leftval As Integer


        Dim eYY, eXX As Integer
        Dim xmax As Integer
        Dim ymax As Integer
        xmax = bm.Width - 1
        ymax = bm.Height - 1
        progbar.Visible = True
        Label8.Visible = True
        progbar.Maximum = ymax + 1
        lowval = 255
        highval = 0
        topval = 1
        leftval = 1


        With bm
            For eYY = 0 To ymax
                For eXX = 0 To xmax
                    RRc = .GetPixel(eXX, eYY).R
                    GGc = .GetPixel(eXX, eYY).G
                    BBc = .GetPixel(eXX, eYY).B
                    If RRc < lowval Then
                        lowval = RRc
                        lowvalnum.Text = lowval
                    End If
                    If RRc > highval Then
                        highval = RRc
                        highvalnum.Text = highval
                    End If
                    gsbuttonarray = New buttonarray(testform, RRc, topval, leftval)
                    gsarray = gsbuttonarray(RRc)

                    [COLOR="RoyalBlue"]If IsNothing(testform.Controls.Find(gsarray.ToString, False)) Then
                        MsgBox("I have control " & gsarray.ToString)
                    Else
                        ' Call the AddNewButton method of MyControlArray.
                        gsbuttonarray.AddNewButton(RRc, topval, leftval)
                        ' Change the BackColor property of the Button 0. 
                        gsbuttonarray(RRc).BackColor = Color.FromArgb(RRc, GGc, BBc)
                        ' add array number to text
                        gsbuttonarray(RRc).Text = "GreyScale =" & RRc.ToString
                        gsbuttonarray(RRc).Height = 20
                        gsbuttonarray(RRc).Width = 92
                    End If[/COLOR]
                    
                    topval = topval + 27
                    If topval >= testform.Height - 27 Then
                        topval = 1
                        leftval = leftval + 156

                    End If

                Next eXX
                progbar.Value = progbar.Value + 1
                Application.DoEvents()

                percentage()

            Next eYY
        End With
        progbar.Visible = False
        Label8.Visible = False
        progbar.Value = 0
    End Sub

VB.NET:
Public Class buttonarray
    Inherits System.Collections.CollectionBase

    Private ReadOnly HostForm As System.Windows.Forms.Form
    Public Function AddNewButton(ByVal aindex As Integer, ByVal tloc As Integer, ByVal lloc As Integer) As System.Windows.Forms.Button
        ' Create a new instance of the Button class.
        Dim aButton As New System.Windows.Forms.Button()
        AddHandler aButton.Click, AddressOf ClickHandler
        ' Add the button to the collection's internal list.
        Me.List.Add(aButton)
        ' Add the button to the controls collection of the form 
        ' referenced by the HostForm field.
        HostForm.Controls.Add(aButton)
        ' Set intial properties for the button object.
        aButton.Top = tloc
        aButton.Left = lloc
        aButton.Tag = Me.Count
        Return aButton
    End Function
    Public Sub New(ByVal host As System.Windows.Forms.Form, ByVal aindex As Integer, ByVal tloc As Integer, ByVal lloc As Integer)
        HostForm = host
        Me.AddNewButton(aindex, tloc, lloc)
    End Sub

    Default Public ReadOnly Property Item(ByVal Index As Integer) As  _
       System.Windows.Forms.Button
        Get
            Return CType(Me.List.Item(0), System.Windows.Forms.Button)
        End Get
    End Property
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As  _
   System.EventArgs)

        colorform.Color1.ShowDialog()
        sender.BackColor = colorform.Color1.Color

        MessageBox.Show("you have clicked button " & sender.text)

    End Sub
End Class

the problem im having is that 2 buttons are created on the form at the same location, i dont want the second button created, i cant see how its created and it doesnt have the properties i defined for the array i created(it has a different size,color,text).

im an amateur, so the approach maybe all wrong, any extra corrections are welcome as well.

also, i havent looked into it yet, so if u dont want to reply about something i havent tried to fix thats understandable, The code in blue was my attempt to NOT create buttons of the same grayscale value--its a failure-- im getting buttons for each pixel in the image.

i wasnt sure how much code to show here as the program is quite large, but this is all thats used for creating the array.
 
fixed

i got it worked out-
i had to move the: gsbuttonarray = New buttonarray(testform, RRc, topval, leftval) line out of the FOR loops and add some variables to the calls and functions to add button text(essentially for all properties)

thx tho :)
 
Back
Top