Is it DrawableGameComponent the wrong tool for creating buttons? - c#

I am making a game based on Game State Managment pattern. I made 20 button on a 2nd page that let the user pick the level.
LevelButton : TexturedDrawableGameComponent
Inside LoadContent() buttons are created and added to GameComponent collection. If I get this right the GameCompoenent is a sort of global collection, so when the user goes back from 4th page to 2nd app will add another 20 buttons.
I added a loop that clears all LevelButtons from the collection, but seems this is wrong.
Is it DrawableGameComponent the wrong tool for creating buttons?

Yes.
DrawableGameComponent and its friends are optional bits of the XNA framework. Consider them helpers. Simple example implementations.
If you need more or different functionality to what they provide, you must replace them with your own implementation!
In this case it seems that what you want is a "Button" object (like your LevelButton), probably with methods like Update and Draw.
And then you want some kind of "Button Container" object that will hold all these buttons, and keep them drawn and updated when they are on-screen. But not do anything with them while they are off-screen.
(You could create your own instance of GameComponentCollection to manage this, and keep using game components. But you still have to provide your own draw/update/load/unload/initialise logic. The logic for Game.Components is internal to Game and not reusable. I recommend just using a List - keep it simple!)
The alternative to creating multiple collections - which is to add and remove (or disable/enable) components from the global collection - is horrifically ugly and error-prone. Don't do it.

I think that it is not wrong if it runs.
Maybe you can approach this in several more elegant ways or improving the way you do it... but the matter is that it have to run.
Maybe you are trying to remove the button when you click it, this will give you problems because that button is being updated.
When the level selection screen becomes inactive or destroyed is the time to remove the buttons.
Of course, you can create a button collection class, that manages the list of buttons, and then you only have enable/disable or remove one DrawableGameComponent, and can be useful in other screens.

Related

Is there any limit on the number of WPF DispatchTimers that can be used

I have a WPF project where I have many small control "widgets" I display (these can add up to over a few thousand in some instances)
I now wish to add a small ongoing animation to scroll through one of the text fields to swap between various different values. So I need to add some sort of timer to trigger the animation (or change the value, which would then trigger an animation)
Since there are so many instances, I am not sure whether to do the neater and simpler way I adding a DispatchTimer to the control class (so there is an instance of DispatchTimer for each instance of the control), or, if there is some performance overhead of doing this, have some static, or injected single instance of the DispatchTimer that would then just call a method on each control.
I am always careful here, as in the Winform days, there were always limits on Handles, User objects etc.
Looking at the Source, I do see a HandleRef being created at line 2982..
SafeNativeMethods.SetTimer(
new HandleRef(this, _window.Value.Handle),
TIMERID_TIMERS,
delta);
however, HandlRef seems to be nothing more than a simple wrapper.
I haven;t examined all the source code, was wondering if anyone had any prior knowledge about this?
Thanks in advance for any help

Actioning a C# GUI from beneath

I have inherited a C# GUI that I need to make some changes to. The main thing I need to do is to action the view controllers to fill in forms and things automatically and without a user.
Being very new to C#, is it possible to initialise GUI elements and event handlers so that they are valid objects without actually displaying them on the screen? The end goal is to create a new API that effectively forms a command line style variant of what already exists.
I appreciate this question might come across a little confused, as I'm still dipping my toe in and trying to feel my way around the approach.
Being very new to C#, is it possible to initialise GUI elements and event handlers so that they are valid objects without actually displaying them on the screen?
Yes, you can set their visibility to false. And they won't be visible. Or alternatively you don't add them to any displayed control. If you go to designer.cs file of your form you will see this line.
this.Controls.Add
this is basically adding your control to the form. If you skip this line it won't be displayed.
If you are going to write a Command Line Interface for a GUI Application (not the usual approach), then how about instantiating and just not calling .Show() on it? You can control the individual elements from the .Controls -Property of the Form.

Creating A Counter In C# As A Repeatable Tool

I have a very basic program that's essentially a few counters with buttons to increase/decrease the count that is pictured below.
The problem is I had to create each of the items somewhat independently so for every button there's a function rather than reusing them.
Is there anyway to essentially save the entire counter so that the label and buttons automatically go together and I don't have to recode the same thing multiple times?
Make a UserControl called Counter, that provides whatever properties you need. I would suggest you just need two properties: Title and Count. You wire these up to the UI elements provided by your control. If using WPF, this is pretty easy to do through XAML if you use Dependency Properties.
I don't know C#, but I know that you could make a class that groups all of your UI elements together:
"A class is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events." From MSDN.
So each counter could have the label, count, buttons and surrounding panel(s), and handle setting all of them up and adding them to the interface each time the class is initialized.

List objects in a panel

i got a question regarding C#
I'm about making a program to hold all my daily tasks, and i need to show them in some kind of panel/list, Ive tryed with the gridview and it worked fine, but i don't want a "table" look, i rather want somekind of access database look, so it creates a new textbox/label maybe a panel with several informations - got any suggestions for this one? if it's possible in a easy way.
If you want just use WindowsForms, you can, for example, define a UserTaskControl:UserControl that holds unique set of controls you need for single entry.
Let's say you need for single entry to have Title, StartDate, EndDate, Description, so you can create a control with 4 TextBoxes or 2 TextBoxes and 2 Calendar controls (matter of design choice).
After define on main window TableLayoutPanel and at runtime add new instances of your UserTaskControl in the moment you need a new entry in your task list.
If you want to make things much better, consider of using WPF, as there you can use also UI Virtualization technique (just one example), which can make a difference in regard of WindowsForms, if you have too much entries in your list (too much is application specific measure, obviously). But in this case you need to learn WPF and learn to use it in a good way, which is a right thing to do IMHO, but depends on how much time you have.
Hope this helps.
A listview with checkboxes to check off when you've completed them? You can make the items editable or put in an "editing panel" to use to edit the values.
So you'd have:
[x] Get dressed
[x] Take out the garbage
[x] Make breakfast
[x] Ask ? on stackoverflow !
[ ] Implement solution
I did this one for work as a task tracker.

Delay drawing of a WindowsFormsHost?

Is there a way to delay the drawing of a WindowsFormsHost? It's hosted in WPF.
It hosts a COM object which is an ESRI ArcEngine AxTocControl. Visually it looks like a table of contents tab.
I'm removing a number of objects from the table of contents and adding a different objects in a single function.
When I remove an object, the TOC control blinks and removes and object, then I add an object, and it blinks and adds an object. Since I am adding and removing 20 objects, its blinking 20 times. I would like to delay the drawing -- do all my operations -- and then resume drawing -- hopefully i will only have 1 or two blinks / flashes.
So to summarize, is there a function or technique (I don't care how hacky it is) to delay the drawing of a WindowsFormHost - let some things execute - and then resume drawing?
Well, you can create your own WindowsFormsHost subclass for this specific scenario and override WndProc. From there, you can decide to silently ignore (by not calling base.WndProc) all drawing-related messages as long as a "suppress updates" condition holds.
The condition you can implement any way you like, e.g. set up a public PausePainting() / ResumePainting() method in your class that sets a boolean field.
You will also have to remember to repaint when e.g. ResumePainting() is called, because you don't want your control to never update.
In the end it's not going to be as simple as A-B-C, but the concept is sound so it should work out.
You can use Thread.Sleep(0) to introduce a delay.
You could try processing everything, adding them one by one to a list, then add the whole list at one time.
myWpfControl.Clear();
List<EsriModel> items = new List<EsriModel>();
// Code to populate your items
myWpfControl.AddRange(items.ToArray());
Could it be that simple? That should prevent your WPF Control from flickering as you add them one by one.

Categories

Resources