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..
Related
My program will prompt the user for a number, i.e. 25. The program will then start the "main form" with 25 controls (textbox). The 25 (or whatever number) of textboxes (or whatever control) will need to be formatted evenly. I will also need to be able to retrieve the text (or another property if I use another control) in order, from left to right and up to down. What is the best method of approaching this?
Using WPF MVVM. In a .XAML file, create a DataTemplate with the DataType of a ViewModel that will provide the binding for your TextBoxs, lets call this the TextboxViewModel. Then using a ItemsControl element with an ItemsSource of TextboxViewModel. You'll be able to instantiate as many TextBoxs as you want and be able to get the result by browsing through your list of TextboxViewModel.
Supposing you are using Windows Forms here.
Dynamically create the X controls and add them to the Controls collection of your form. To ease the access to them you can store their reference in a List and set some event handlers too, depending on your needs. You just need to calculate their positions while you add them.
If WinForms, this is exactly what the FlowLayoutPanel is for. Just add the controls to it and they will arrange themselves automatically, wrapping down to the next row as needed. As Mihai already suggested, you could also keep reference to those controls in a List.
Another option would be to use a TableLayoutPanel. It's a little more difficult to learn and use, but is much more flexible and powerful.
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.
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.
I'm trying to learn WPF and was thinking about creating a simple IRC client. The most complicated part is to create the chat log. I want it to look more or less like the one in mIRC:
or irssi:
The important parts are that the text should be selectable, lines should wrap and it should be able to handle quite large logs.
The alternatives that I can come up with are:
StackPanel inside a ScrollViewer where each line is a row
ListView, since that seems more suitable for dynamic content/data binding.
Create an own control that does the rendering on its own.
Is there any WPF guru out there that has some ideas on which direction to take and where to start?
I suggest you start with a good object model independent of the UI, and then try a multi-line TextBox or a RichTextBox.
Whether these will suffice will depend on exactly how long you want the log to be able to get. If you run into performance issues, you may need to look at virtualization.
First of all, you should consider if you want to select only entire row (like in a listbox), or if you want to select certain characters from a row (like in a textbox).
In the first case, I think a ListView or even a ListBox should be enough, both of them support virtualization when bound to collection and there should be no problem with huge amounts of data. A stack panel inside a ScrollViewer is a little bit like reinventing the wheel for this case and creating a new control is not a very inspired approach in my opinion (as the functionality you want can be achieved with the existing controls, in WPF).
In the second case, if you want to select some text inside of a line, or if you want word wrapping for your longest lines in the log and want to select individual parts of the wrapped lines, then you need to use a control more oriented on displaying text. Kent already suggested a RichTextBox, I would add AvalonEdit control or even the WebBrowser control in which you directly modify its HTMLDocument.
I would suggest to use RichTextBox too, and store items in a log file or database, if you run into performance issues.
Another solution is to use the WPF WebBrowser control and modifiy its HTML content with:
webBrowser.NavigateToString("<HTML><H2><B>This page comes using String</B><P></P></H2></HTML>");
More information about using WebBrowser control
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!