Hi guys, I'm trying to create a game similar to the puzzle game 'Bridges', in which the user has to connect islands together with the right number of bridges. Rather than creating lots of preset lines for the bridges, I have decided to try and draw and erase specific LineShapes at runtime.
I know how to draw a LineShape with code, and from my research around this topic I have concluded that the best way would be to store these LineShapes in an array or List(Of T). However, I need to be able to call up specific LineShapes and erase them at the right time. This is where I have the problem.
I do not have any relevant code at the moment, but I would be grateful if you could suggest a method to solve this problem.
Many thanks,
Matt
6 Replies - 205 Views - Last Post: 07 October 2012 - 03:04 PM
#1
Code for deleting a specific line drawn in runtime?
Posted 07 October 2012 - 12:26 PM
Replies To: Code for deleting a specific line drawn in runtime?
#2
Re: Code for deleting a specific line drawn in runtime?
Posted 07 October 2012 - 12:46 PM
What exactly is your problem? Are you having problems adding items to list, and removing them after, or is there any other specific problem?
#3
Re: Code for deleting a specific line drawn in runtime?
Posted 07 October 2012 - 12:51 PM
I know how to add items to a list, but if they are LineShapes, does removing the items remove the shapes from the form? And if so, is there a way to search for a particular item in the list corresponding to, say, x and y positions, and remove it?
This post has been edited by hutchy4122: 07 October 2012 - 12:54 PM
#4
Re: Code for deleting a specific line drawn in runtime?
Posted 07 October 2012 - 02:07 PM
Could you add a handler of something like the click event and from that you can get the sender? From that you could take the sender object, cast it to lineshape and dispose.
#5
Re: Code for deleting a specific line drawn in runtime?
Posted 07 October 2012 - 02:21 PM
Sorry if I'm coming across as ignorant, but I'm a bit of a beginner in VB and to be fair, I have no idea what you're talking about. I have heard of the sender object, but what do you mean by 'cast it to lineshape and dispose'?
#6
Re: Code for deleting a specific line drawn in runtime?
Posted 07 October 2012 - 02:51 PM
Well, the sender is generic, meaning it is an object but doesn't have the specific properties of every object that can call the event. Therefore you have to convert it to its specific type at which point you have all the properties it contains.
For example let's say you add a line to your shape container
Keep in mind, you can run into problems if other types controls call this method so at that point you can use TypeOf to check for specific types and use their specific properties.
For example let's say you add a line to your shape container
Dim ln as new LineShape ln.x1 = 10 ln.x2 = 100 ln.y1 = 50 ln.y2 = 50 ln.Name = "MyShape1" Me.ShapeContainer1.Shapes.Add(ln) AddHandler ln.Click, AddressOf lineClick Private Sub LineClick (sender as Object, e as EventArgs) 'Here is where we take the object that is sender from the arguments and cast it to its specific control MessageBox.Show(CType(Sender, LineShape).Name ' I could just as easily use Ctype(sender, LineShape).Dispose ' And a click would remove the object. End Sub
Keep in mind, you can run into problems if other types controls call this method so at that point you can use TypeOf to check for specific types and use their specific properties.
#7
Re: Code for deleting a specific line drawn in runtime?
Posted 07 October 2012 - 03:04 PM
Thank you! I've found a way to use that in my code. I spent ages trawling through Google beforehand, and it seems so simple now you've explained it to me.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|