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.
Related
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.
I am currently developing a chat application in C# and I would like to know which form control allows to add text retaining control to each specific message to modify it later on when is required. I want this in order to be able to add a double tick when the message is received in the other side of the communication, pretty much like in "Whatsapp".
I've thought about an approach consisting on each message object firing events (like "sent", "received"..) when it changes that are listened by the corresponding form control that serves as the view, adding the above mentioned tick.
Any advice on how to achieve this goal? I've tried TextBox but Lines property force to have control os indexes and I want it to be completely event driven. Currently I stuck with DataGridView, however I've made little to no progress.
Thanks!
No one ready made Control I can think of will do the job, I'm afraid.
I would use a FlowLayoutPanel and add a Label for each chunk of text that gets added to the chat.
You can use MeasureString with a given width to get the height of the Label. (AutoSize should be off.)
The Labels would get the Width of the FLP and you could keep a List<> of the Labels with maybe a few meta data, like user, time etc..
Sounds like a good candidate for a ChatDisplay class to bundle the whole functionality!
Of course as the Labels are Controls you can add events to them as you like to communicate with the ChatDisplay or even with an outside communications object.. And the ChatDisplay class is free to implement whatever you need anyway. If necessary you can wrap the Labels in a ChatItem class, too.
Much more extensible than digging into a DGV to force it into doing things it was not meant to do..
I am working on a WinForm based applications(Yes I don't know WPF) and want's a dashboard like panels in it. Picture given below
Each panel will have a title and records from Database and some action controls. How could this be achieved? I don't want to use GridControl as I don't want to show Excel like spreadsheet here. How could this be achieved?
It sounds like you want to make a UserControl, possibly coupled with an automatic layout panel like FlowLayoutPanel.
Simply speaking, you would create a UserControl with whatever properties and events you require (i.e. in your example you might have a Title property and a Data property), and any events you need to respond to (e.g. you might have a button that you provide a wrapper event for). Then you can add the control to your existing form as you would any other standard control.
As far as displaying data in list form goes, one suggestion is to use a Panel and dynamically add Labels to it. Another idea could be just a simple Label with line breaks in the Text.
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!
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.