How make an area on win form scrollable? - c#

Items like below have to be accommodated on a specific area on WinForm.
"Order 1 ------------ Time Left: 00:20:00"
"Order 2 ------------ Time Left: 01:30:20"
We should be able to perform the following action on the each order:
Each item will occupy not more than one line. As the area specified
for it on the win form is limited, as more items comes in, I want to
make the area scrollable.
According to the time left the background color of the line should also change.
There is a DONE button next to it. So, if the item is selected and DONE is pressed, the item is moved off the list.
My question is what C# Form control can be used for it. I was thinking of labels, but how to make the area scrollable if there are many of them. If not labels, what else is suggested?

As suggested in comments, you can use grid, but in case it does not suits your requirements, this is like something what you can do -
Create a custom user control which will have a label control for your order detail and a Done button. Something like this (sure you will design it better!). The label will be empty initially and you will pass then from outside, using a public property for example, while creating the control.
Define an Event on this control, and raise this event when the Done button is clicked. This is required for your main form to know that when to remove this user control.
Add a FlowLayoutPanel to your main form. This will be the container for your user controls. Make sure to set the following so that the controls are created as desired
this.flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
this.flowLayoutPanel1.WrapContents = false;
this.flowLayoutPanel1.AutoScroll = true;
Now you can start adding your custom control to this FlowLayoutPanel, either by loop or the way you like. The control will added in linear way, one in each line, and you will also get scroll it it exceeds the given space.
Make sure to define event handler for the control, so that you know when to remove the control. And of course you can set other properties like back groung color etc. That's not going to be any problem.

Related

Add label to textbox custom usercontrol in WinForm

I am writing a windows forms application that has a lot of textboxes. I want to add a label or a caption to the textbox, so that I don’t have to drag a lot labels onto the form and deal with positioning etc. So far I have found 2 possible ways to do this.
Create a user control with the label and textbox. How do I get the
control, label and textbox to size appropriately depending on the
text entered since the control will be reusable and different text sizes will be entered. How to get all the
properties and events of the textbox to remain the same.
Extend a normal textbox and add a string property called label or
caption, and show this property at the left of the textbox. I know
this can be done in Web.UI with CSS but is it possible in a winform
and how?
Any suggestions on how to do either of these?
Thanks.
You can create a UserControl that contains a label and a textbox. When you add the user control to your form, both the label and the textbox within will be added simultaneously. You can expose properties of the label and textbox to assign values at design or run time.
Using this method, you can add multiples of the user control to standardize the layout. As far as resizing the controls based on the text, you'll have to subscribe to events and change the sizing manually.
For example, you can subscribe to the TextChanged event of the label and the textbox. When the event fires, you calculate the size of the string and then adjust the width and position of the controls accordingly.
If you get to the point where you have too many textboxes, I would suggest switching to a DataGridView. The GridView component is very well suited for what you're describing, but of course it requires you to accept a grid layout.
One of the bonuses involved in using a GridView is hard to appreciate until you see it in action: it creates only one HWINDOW at a time (two if you're in editing mode). If you create Labels and TextBoxes all over your form, each one is registered with the operating system as an HWINDOW object. All those HWINDOW objects take time to display! In .NET 1.0, WinForms was so slow that dialogs with more than about two dozen controls were unusable. Even though .NET 2.0 is much better in this regard, but you'll still get significantly better performance by using a single control that manages lots of data, as opposed to lots of controls that each manage one piece of data.
Oh, and another option, if you like: you can also try a PropertyGrid. It has the advantage that it will also show help and allow you to create complex editing controls for each element.

Should I be using a WPF user control? Or just a Grid?

So I have an application that is split into 2 parts - on the left there is a custom menu, on the right a grid that holds all the "content" (different screens).
It looks something like this:
Also, when the different buttons are hit, the menu on the left will fill up with different buttons (for example, if you hit the review button, the menu would become something along the lines):
Start Date
End Date
Employee
Project
...
I am pretty sure that I want each of the screens (to go on the right) to be their own user controls.
But my question is this: Should each of the menu's be user controls? This makes it a little harder to use them. Then I have to worry about having getters/setters so the main window can listen on the menu's buttons, etc.
The other option is to just programmatically add the buttons in the mainWindow, this way I can just add the listeners right in mainWindow.cs (into a grid)
Which is the better method? Or is there another method which is favoured?
The entire window can be done easily as a Grid.
The left side can just hold a StackPanel or other layout control with your buttons.
You can use a ContentPresenter to hold the content on the right. When you trigger your buttons, just change the bound content, and it will update with your appropriate user controls.
I think you might do it with a grid as Reed told. But if you will need to reuse this in another place, I think you should use a separated UserControl instead.
Maybe I'm misunderstanding you, but I would use a TabControl, with TabStripPlacement="Left".
If you need extra space on the left to display other stuff, you can override the default TabControl's ControlTemplate and add a bunch of margin space to the TabPanel inside the ControlTemplate. Alternatively, you could have the TabItem's header display differently when it is selected versus when it is not selected.

How to draw text at runtime

Basically, Im making a paint application very similar to MSPaint.
The idea is that, that the the user clicks anywhere on the form and should be able to write text in a control. And then following that, that text should be displayed in g.drawstring graphic method.
I don't want to do the whole thing for you, but here is a basic outline of one way to accomplish the goals you outline. This is not necessarily the best way, but it should get you started and will introduce you to a number of WinForms concepts.
Writing the text
Create a Form and add a TextBox control to it. Make sure it is hidden by default. Override the OnMouseClick method of your Form and add code that checks if the TextBox is visible and if not, shows it and puts focus to it for the user to enter their text. If the TextBox is already visible, the code should hide it and create a new UserControl in its place that shows the text (see below for details of that UserControl).
Also add an event handler to the TextBox so that if the user hits Esc, it cancels the edit and if they hit Enter, the text is accepted and the UserControl is created.
Displaying the text
Create a UserControl and make sure that the UserPaint and Opaque styles are set in its construction (see SetStyle - you may also want to consider OptimizedDoubleBuffer and AllPaintingInWmPaint as this can reduce flickering though it does require extra paint code).
Override the OnPaint method in your UserControl and implement the code for drawing the string (remember, you'll also need a way to set the text on the control).
Conclusion
If you hook all that up, you should have something that appears to meet your requirements. For further experimentation, consider how you could remove the need for the UserControl. Good luck and have fun!

What is the best way to embed controls in a list/grid

I have a table of about 450 rows that I would like to display in a graphical list for users to view or modify the line items. The users would be selection options from comboboxes and selecting check boxes etc.
I have found a listview class that extends the basic listview to allow for embeding objects but it seems kind of slugish when I load all the rows into it.
I have used a datagridview in the past for comboboxes and checkboxes, but there was a lot of time invested in getting that up and running...not a big fav of mine.
I am looking for sugestions how I can do this with minimal overhead.
thanks
c#, vs2008, .net 2.0, system.windows.forms
If you have a complicated set of controls for each row, this is the simplest way to do it. However, it won't act like a listbox; you won't be able to highlight your rows or navigate with the keyboard.
Create a usercontrol with public property to point to your row
Draw a panel on your form - you will add instances of your 'row' usercontrol at runtime to this panel.
Set the panel to autoscroll (also set property to make the active control scroll into view)
Set the panel's Anchor property so it sizes w/ the window
You can set the form's max/min size properties so the full usercontrol row always shows (have to do to prevent horiz. scroll bar in panel)
Have a routine to add the rows
In a loop, create new usercontrols, set their properties, including the row in the datatable
Also, set the .Top property to the panel's .controls(pnl.controls.count-1) for all but the first one you add
Very simple, allows complicated 'rows', gets the job done. There are better ways to do this if you want listbox-like functionality without coding it yourself, but you may not need that.

SplitContainer.Panel1 Selection to Changes SplitContainer.Panel2 selection

The MSDN documentation for the SplitContainer controls says:
With the SplitContainer control, you can create complex user interfaces;
often, a selection in one panel
determines what objects are shown in
the other panel.
So, I have a SplitContainer that is split horizontally having top and bottom panels.
What I would like to do is -- when a control within Panel1 (on top) receives focus, I need a control within Panel2 (bottom) to be focused. And then the focus must go back to the control in Panel1 again.
The problem is, the control in Panel2 returns false on CanFocus. I understand that for some kind of controls this can happen, and that every ancestor of the control must have focus for it to receive focus. I tried that too, but to no avail. The CanFocus still returns false.
Is there any alternative solution to this? Has anyone tried this. What I am trying to do is essentially the same as what Outlook does. When you select a message in your Inbox. The message body shows up in the bottom panel.
What you're describing doesn't mimic Outlook. If I select a message in the message list in Outlook, focus remains with the message list. Yes, the message body does appear below, but the focus doesn't move.
There shouldn't be anything preventing you from changing either the control or the content that is displayed in either panel. You shouldn't have to move focus to accomplish that.
Do you really need to set focus to Panel2?
I'm guessing your Panel2 contains something like a RichTextBox object, say called RTB1 for simplicity.
Say someone selects something in Panel1. For simplicity, let's say you have a ListView control called LV1.
Then, on the LV1.SelectedIndexChanged event, read that event and fill your data to RTB1.
You don't really need to give Panel2 focus, unless there is something I'm missing.

Categories