I am making a custom Tic Tac Toe game where there are three Tic Tac Toe frames in the form.
Instead of creating three times a 3×3 square of buttons, i thought it would be better to make a custom control that will contain nine buttons in the formation I want, and so I can put it three times in the form and also access each and every button in that formation.
I am pretty new to inheritance and custom controls so i would like your help with examples or instructions.
In this case, you can use a
UserControl. This is easier than creating a custom control, which requires to derive a control from an existing control and to enhance it. In VS right click on your project and choose “Add” > “New Item…”. In the Windows Forms section select “User Control”. Give it the name “TicTacToeUserControl”. You can design the user control much like designing a form. It will then automatically appear in to Toolbox of the current project and be ready to be dropped on a form.UPDATE
Here some more explanations. Place a
TableLayoutPanelon theUserControl. ChangeDocktoFill. Add a row and a column in order to have three of both and change their size mode toPercentand change these values to 33.33. Add a button to each table field from left to right and then top down in order to have buttons names “button1”, “button2” etc. in reading order. Save the user control (my VS had a glitch at this point and I had to start all over).Create this class that we will use as event argument for our button click event
Change the code of the user control to make it look like this
Select the event handler “button_Click” for the click event of all your buttons in the properties window switched to Event (the flash symbol). Don’t create a new one for each button. Hit F6 (compile)
Your control is ready and can be dropped onto a form from the tools window. Resize it as desired. Double click on it. Because of the
DefaultEventAttributethat we specified for the form, VS will automatically create this event handlerAdd this code line to it in order to test the user control
NOTE: This does not actually create a new control, it just creates a template