Results 1 to 8 of 8

Thread: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce?

    To explain further, i'm creating lots of buttons and want to be able to have each perform different actions.

    R = 1
    C = 1
    For i as Integer = 1 to 2500
    Button(R, C) = New Button
    Button(R, C).Parent = Me
    ...
    If C = 50 Then
    R += 1
    C = 1
    Else
    C += 1
    End If
    next

    This is creating a grid of buttons from (1,1) to (50,50), and i want each button to change R and C to it's own number, ie

    Addhandler Button(30, 48).Click, Addressof R30C48

    Private Sub R30C48
    R = 30
    C = 48
    End Sub

    etc etc...

    I made a seperate bit of software to write it all for me, but when i click one of the buttons it comes up with an error message saying that i'm using too much memory. I can only assume that there is a lesser memory consuming method of doing the same thing but in a much easier way, kind of like the For Loop i used to create the buttons.

    Thank you in advance.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    You only need a single method to handle ALL of those button clicks, so they can all add just that one method. I mention this because your example shows a method that certainly suggests that you are adding a different method to handle every single button, which would be a nightmare to maintain when only one method is necessary.

    Still, that's not the issue. The issue must be adding 2500 buttons to the form. I haven't tested such a thing, but it seems likely that it would kill performance. The first thing to do is to comment out the AddHandler line. Without that line, you would just be testing the creation of all those buttons. If that fails, then it is the number of buttons that is causing the problem. Since you can't be doing all that much with that many buttons on the screen, then finding out that all those buttons is the cause of the problem would be really significant, since there are other ways to get the same result while using only one control (though you would have to draw it yourself, which might be an issue). For instance, you could put up a single Panel or Picturebox, draw all the rectangles where the buttons would be, then handle the MouseDown method to figure out which one was clicked (and change that part of the drawing to show that it was clicked).

    So, if the creation of all those buttons is the issue, then there are viable alternatives.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    I tried it with the buttons first, and that runs fine, i can even have it change the background color of all the buttons in miliseconds. But its just having that many addhandlers that is creating the problem. I want it to use one method, just like the for loop, but i dont know what that method is.

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    Do they need to be buttons? I'm thinking a single custom control that can draw a grid on itself, that can handle a click and from the coordinates of the click on the control determine the coordinates of the 'button' clicked and then raise an Event with the coordinates in the event args.

    What is the application that you're trying to build here?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    To be honest, i'm not using buttons, i'm using Labels, but the same problem exists, i'm using this as a part of a game, and these will be a part of a map, so the labels will be diferent colors. As the user presses left, right, up or down, the colors will change. If the user click a Label, i'm wanting it to "zoom in", in other word, create a new grid over the top that is of equal size but has less labels, and uses the colors of the labels around the label they clicked. But to do this i'll need it to give a reference point when cicked.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    To be honest, i'm not using buttons, i'm using Labels, but the same problem exists, i'm using this as a part of a game, and these will be a part of a map, so the labels will be diferent colors. As the user presses left, right, up or down, the colors will change. If the user click a Label, i'm wanting it to "zoom in", in other word, create a new grid over the top that is of equal size but has less labels, and uses the colors of the labels around the label they clicked. But to do this i'll need it to give a reference point when cicked.

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    It really doesn't make a lot of sense to create a grid this way especially as the DataGridView control has just about every piece of functionality you'll require built in. Going from 2500 labels to one DataGridView is certainly gonna solve any memory problems!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    Those points aside, you did confirm that adding the buttons (or labels, or whatever) is not the issue.

    Take note of the .Tag property of the control. This is a very useful little property that holds absolutely anything. That's what it was added for: To hold whatever stuff you wanted to associate with that control. For example, you have a location for each control, so you might put a Point structure into the .Tag property. Alternatively, you might want even more stuff, so you could create some custom class and assign one to each control.

    Now you only need a single method to handle everything because the control has all the information it needs right there in the .Tag property. Furthermore, the control is the first argument passed to the handler method. It is the 'sender' argument, though 'sender' is type object, so you'd have to cast it back to whatever you wanted. If you had a Point in the .Tag property, then you could do this:

    R = DirectCast(DirectCast(sender,Control).Tag,Drawing.Point).X
    C = DirectCast(DirectCast(sender,Control).Tag,Drawing.Point).Y

    which would mean that all your controls would need only that one method.

    So, if you did that and still got the problem, that would surprise me, because the only alternative would be that the AddHandler call itself is causing the out of memory error, and AddHandler should be pretty trivial in memory usage. It makes me think that the error is actually elsewhere, though I can't say where.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2012
    Posts
    18

    Re: vs2010, Button(1) - Button(2500) "Addhandlers" too much for memory, how to reduce

    I was faffing about for a bit last night and i managed to create a formula that used the mouseposition to define which button was being pressed, so that allowed me to get the reference points i wanted, and it doesnt give me any errors. Althouh i will have a play with your idea and see if it helps me with future projects. Thanks for all the replies, and thanks for all the advice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width