Creating A Counter In C# As A Repeatable Tool - c#

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.

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

Binary Search Tree with Forms

I am working on an assignment and I need to implement a Binary Search Tree with Forms. Now, I know how to create a BST with not much problems, But to use it with forms and create a GUI is confusing to me.
So, what I would like to ask is how do I implement a BST to a form?
I think you're approaching this with the mindset that the UI and the Binary Tree have to be intertwined so closely that you'll need to modify your BST class. Instead, think of it like this.
Your BST class will take an input. Where you get this input from doesn't, and shouldn't matter, to your BST. So you'd create an instance of your tree, as normal, and pass in the input from a TextBox for example. So the user clicks a button (you handle the button click event). Within this event, you read the TextBox data and then send that to your BST class. The class will return values which you now have in a collection, for example.
The final step is to work with that collection. As a basic example, you could bind that collection a ListBox so after your method returns the values, you set the ItemsSource property of the ListBox to the collection that was returned.
The idea is that you need to think of your BST class being logically separate from your UI. This makes it easier to break down what you want to achieve into smaller steps.
What effort did you put in learning windows forms so far? Your question is to general.
If it's your first contact with winforms, you could start on msdna - those are pretty interesting examples, that'll introduce you the basics.

Is it DrawableGameComponent the wrong tool for creating buttons?

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.

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.

C# How to implement a form which controls are dependant of some properties?

In my application, different controls are only used dependent of the values of properties from a particular object. The forms constructor accept this object as a parameter.
The form has always some basic functionality, no matter what properties are set of the particular object.
Now I have something like this:
if(myObject.SomeProperty)
{
myControl.Visible = true;
myOtherControl.Visible = false;
// and so on
}
At this time, the controls that are dependant of SomeProperty are buttons and tab items. However, I can imagine that in the future other controls are added to the form and are also dependant of SomeProperty.
As you might guess, I want to set this up the right way. But I don't know exactly how. How would you implement this?
There are multiple ways I can think of solving this, depending on your situation you could select the best suited to you.
1. Databinding is one elegant solution when managing the state (visibilit or other properties) of multiple control's depend on a different object. Additional details in this question
2. You could write different functions if the combination of the states is only limited to couple of cases to at most 4-5 cases. That ways you can still reason about the methods which set the state depending on the object you are depending on. Ex: Basic_Editing, Advaced_Editing, Custom_Editiong etc.
3. If the number of cases are limited you could create multiple forms (User controls) and load them on demand based on the state of the dependent property (or object you are talking about).
Just having a bunch of if else's makes your code harder to maintain, or comprehend, logically group the states so that 1. You could reason about it later, 2.Someone else understands the reason/logic 3.When there is a change required it can be localized to one of these modular methods (techniques) reducing the time to fix, and test.
I would do it like this in form constructor:
myControl.Visible = myObject.SomeProperty && !myObject.SomeOtherProperty;
myOtherControl.Visible = !myObject.SomeProperty;
....
Is it the less code and its rapidly changing.
OR
You can create separate functions that will generate controls dynamically at runtime for each form view based on object properties.
First i can see you are setting visibility on/off it means you have already controls on the form every time.. , so that not a good practice, instead create controls only when needed.
As for your scenario you can have an function like Initialize() which contains all the code for checking if showing a particular control should be shown or not and then create it and add it to Forms control collection. If any new control come to be added later you have one function to update.
A more precise answer can be given if you can provide more detail to you scenario

Categories

Resources