Binary Search Tree with Forms - c#

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.

Related

How do I make controls extend passed a control bound or overlap with others?

I have a wpf grid setup where I have two custom buttons that are next to each other. First picture is how the design window looks like, however, functionally, it looks like the second picture. I want them to function like the grid doesn't block them (closely resembling the first picture.)
The current xml I have is based on this MSDM which is very basic.
I do not know how to proceed. Do I have to use a different control panel/container or is there a setting to allowed them to extend passed the grid if the other button isn't above it (like zpanel?)
E: I couldn't find any other questions for this, so please link to any searches/posts with information on it.
Turns out I can use canvas and just do a bit more xml to keep the design the same. I would still like to know if it is possible to overlap them in any way for future use.

Passing data from a handler to another. very basic issue about OOP

This may be a really basic question to experienced programmers but I started on VB6, and now I'm trying to accomplish same stuff on C# which is object oriented.
Suppose I have a class with a method to add two numbers on textboxes and I run that in the click handler of a button (Doesn't matter if it is static or not), then I have the result and I display it on the screen (maybe in another textbox), the user click another button, how do I recover the result on the other button handler?, what's the best practice?, I know I can read the result on the textbox, but if the result was displayed on a Messagebox.Show or in console?.
What's the best practice to save results instead of using helper textboxes or global variables?
On VB6 I use invisible textboxes, so my forms looks really messy, but thats the way to there.
Using C# + XAML + WPF
Usually that is what a model is for. It is the data-state of what is shown (and more, as some information may not be displayed at all times or just used as utility). The view often has a reference to the model which you then can access in the handlers and manipulate.
I'd suggest reading up on design patterns like Model-View-Controller and for WPF specifially Model-View-ViewModel.
Also, WPF has a few powerful mechanics like data binding, which makes synchronizing your data with the view a lot easier, do not treat your view as a model.

How to a implement ListBox ScrollIntoView functionality in a ViewModel

I have a screen with a TextBox in which the user can type a 2-character state code. Below the TextBox is a ListBox containing all 50 state codes. The TextBox is bound to property in the VM, and the SelectedItem is bound to a property in the VM. That all works fine.
The way I want the UI to work is when the user selects a state from the ListBox, the TextBox is automatically filled in, and this works fine.
Where it gets messy is when the user types in the state in the TextBox. When I get the first character, what I want to do is reposition the list box at the first matching state code for that letter, so for instance, if the ListBox is sitting at "AK" (Alaska) and the user is going to type "ID" for Idaho, when I get the "I" I want to position the ListBox so you can see the first "I" state, which is "IA" (Iowa).
If I use code behind and point SelectionChanged=BringSelectionIntoView with this method coded as follows, it works great:
private void BringSelectionIntoView(object sender, SelectionChangedEventArgs e)
{
ListBox lb = (ListBox)sender;
lb.ScrollIntoView(lb.SelectedItem);
}
All I have to do is scan the list of state codes until the first letter matches, then update the Index property to which SelectedIndex is bound, and poof, the BringSelectionIntoView() method gets invoked and I have exactly the UI behavior I want.
Trying to do this in a purest MVVM methodology, however, has proved quite frustrating. I'm not using MVVMLight or ExpressionBlend--I want a simple way to do this in MVVM. I understand the purest's mindset of not putting any UI code in the view, but the framework is insanely cumbersome to enact this kind of behavior. There's a point of diminishing returns when you have to create such obtuse plumbing to force yourself to adhere to a pattern when it's far more practical to put in the method with 2 lines of code that works perfectly.
So my question is this: am I doing something wrong and is there a simple way to make this work without violating MVVM? It's disappointing if the solution requires additional SDKs or someone's framework. That would suggest that MVVM doesn't have particularly good legs to stand on in a generic OOP sense.
Does someone see an error in what I'm trying to do, or do you see a simplistic solution here? Thanks!
MVVM is not about not having any code behind.
What you're talking about here is VIEW behavior. Which fits perfectly in the code behind, as long as you're not messing with the DATA in the event handlers.
You're using a VIEW event handler to manipulate a VIEW aspect.
That doesn't break MVVM.
Keep it that way. Keep it Simple.
You should still have a ViewModel and a Model to hold the DATA that the UI shows.
This is a perfect use case for an attached behavior. You can write this behavior once and use it for all listboxes without ever having to write any additional code. If you would like me to elaborate, ask and I'll post more information.

WPF Binding and historical values

I'm currently writing a WPF progress bar that includes a rate (see Windows 8 - Fancy Progress Bars API?).
The screenshot below shows what I've got so far (left) and a badly done all in code as part of my learning exercise (right). I'm now trying to convert the code version to use as much XAML as possible.
I've got most of the way there by creating a new class called RateBase and implementing it in a similar way to RangeBase. I've then added a new instance and provided a template file RangeGraph. I'm attempting to do this as by the book as possible, but I'm not sure how to tackle the final stage.
I now wish to add a graph, this graph is to display the rate as it has changed historically as the progress has progressed. I have 'Rate' as a value I can bind to, but I believe somwhere I need a Double[] containing my historical rate values. My question is where should this be placed (I don't really want to pollute RateBase) and how do I bind to it from my template (I don't believe I can bind to RangeGraph.cs if I add properties on there or am I wrong?)
You are right, you will need historical Data. In my opinion, whenever the bound Dependency Property Rate changes, you should move the old value into an IEnumerable that's defined on the graph control itself (The same place that has the DP) and use that to draw the lines. I personally would create a class named MyControlData and add an instance of that to the control.
You might also want to add a Timer and move the current Rate to the IEnumerable when it Elapses, so longer streaks of the same rate will appear as multiple bars. Depends on how you actually determine progress. You might get into the following dilemma here : The Rate changes at a different interval than the Percentage in most cases - what floats your boat?
Keeping the history in your control's scope leaves your application agnostic to the history of your Rate, but lets your control display it as required.
To use DataBinding in a UserControl, edit the <UserControl x:Name="myControl"> node in Control.xaml and add a name like shown here. Wherever you want to bind, refer to ElementName=myControl. Please note that you will have to implement INotifyPropertyChanged on the Control (or on MyControlData) if you want to achieve this - or, and that would be advisable, directly implement it as a dependency property.
And BTW, if you have no idea how to achieve what you intend to have a look into ItemsControl. I think what you want to do can easily be achieved by means as simple as using ItemsControl and ItemsTemplate, where the ItemsSource is your historical data and the ItemTemplate depicts your current rate in comparison to your MaxRate. MaxRate is another property you can set from the DependencyProperty Rate's changed handler.

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.

Categories

Resources