简体   繁体   中英

Adding custom properties to a button control c#

I'm building a chess game at the moment that's probably 75% complete, it was an impulse thing with zero planning and hence it is very messy.

I have three multi-dimensional arrays, one contains an enum Colour , another an enum ChessPiece and the last contains the coordinates.

I want to clean this up and there are two things I can think of.

1) Object oriented programming. I could build a class with properties Colour, Coordinates, and Piece, and then instantiate each piece at the start of the game, and refer to each instance.

2) A far easier way would be if I could add a property to a button control, similar to that of a class. ie button1.Piece = Pawn where button1 refers to the actual button.

Is there a way to do this? Thanks.

It could be as simple as:

public class ChessButton : Button
{
    public ChessPiece { get; set; }
}

Obviously you'd need to extend ChessButton (horrible name) to do what you want.

Well, if you take your first approach (to do things within an oo paradigm) I could imagine you could come up with quite a neat model. I could envisage a base class Piece, which may be inherited by classes Prawn, Rook, Bishop etc. which would likely implement the "rules" for moving the piece. Similarly I could imagine a class Tile and another class (containing a 2-D array of tiles) called Board. etc. etc.

So you can picture this ultimately as being quite neat/pretty. You'd have to think about all these classes, work out what they do and how they interact with other classes. So there's an element of design in it, and this is a potential downside - having to do up-front thought without any tangible gain - you're almost building up an infrastructure for your app to live in. But if you gave the code to someone they would hopefully be able to work out what is going on quite easily. So its maintainable. And there's the big benefit of an oo approach.

Your second approach (extending ui classes). Well, as you say it seems far easier. And maybe if the app is simple enough, that's the way to go.

But you also say that you've done things on an ad-hoc basis so far and its all very messy. And what you've come across is typical of ui development - when you build things with an unstructured approach, things become messy. Over the years people have invested lots of time and money in trying to add structure into front ends to try and increase maintainability. You tag this question c# so maybe you've heard of Microsofty things like MVC, or MVVM, or MVP. You don't necessarily need to go and look at these approaches in detail, you just need to understand why they exist - to try and compartmentalise UI code to make it all more manageable.

Since you also say one of your motivations here is to clean your code up, I would urge you to put some thought into a structured approach, rather than taking the "far easier" path by default.

If you think about it and decide to just extend a Button, fine. The reason you'll choose it over a structured approach is because the structured approach would require too much more effort. But at least you thought about it. In the future you'll come up against much more complex ui requirements and this kind of up-front thought will pay dividends.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM