Windows Forms TextImageRelation Layout Math - c#

I'm trying to create a custom Windows Forms Label control with a much needed TextImageRelation property, just like Buttons have. However, I don't understand how Buttons are dividing their internal space between the Text and Image for the various TextAlign and ImageAlign combinations. I've spent far too much time playing with the designer trying to reverse engineer the logic, and I just don't get it. Can someone please explain to me how this layout is being calculated?

Related

How to use Visual Studio's C# Windows Forms Design View for a Scalable/Resizable Window?

I am attempting to create a Windows Forms Application that will have components that scale when the window is being resized. I am running into issues with the Form when attempting to resize it when the application is running. First of all, this is what it looks like in the editor at its Minimum Size:
Then I stretch it out at run time and it is even on both sides (after tinkering with the Fixed-Splitter position:
I run into more problems when attempting to put List Boxes in the blue and red panels. In design:
Stretched:
I want the list boxes to nicely fill most of the width of each side, but when I attempt to use the Anchor tags it gets messed up.
So to sum up: Why is the designer all asymmetrical compared to the finished product and how do I make the List Boxes fit and scale in width when the window is resized?
I am using VS17 if that helps! Thanks!
The anchors was always (I don't know why) littlebit broken. Use the composition of nested Panels and use the Dock and Padding properties instead of anchros.

Creating a picturebox/mapview type control with clickable rectangles and animated icons

I'm looking to add a 'mapview' type control to my project.
It must have a 'main map' image with clickable transparent rectangles with borders and icons/images that can be animated when an event occurs.
What would be the best way of achieving this using windows forms in C#?
My first thought was to use a picture box with other items on top of it but I might run into problems with transparency etc.
Are there any libraries or anything out there that would be able to achieve this?
No need for a library, really:
I would go for a regular doublebuffered Panel subclass or even a PictureBox subclass for the board/map along with a movable Label or Panel subclass fpr the rectangles/items.
Important: Make sure the Labels are not just 'put on top' of the PictureBox but really nested!! (lbl.Parent = pbox). Then transparency will work just fine..
Since PictueBox is not a 'container', to nest a control in it you need code. But since you probably want to create them dynamically this is not an issue anyway.
This assumes that the rectangles are not overlapping! For overlapping controls transparency in winforms will not work.
The clearer you understand the 'animate when event' part the easier the rest of the code will be..
Since you mention 'animation', a word of warning: Simple animation, especially in reponse to a user action is doable; for more classy animation you may run into the limits of winforms.

WPF Scrollable Timeline Control

I'm trying to build a timeline-like control for a project, but I have limited experience with WPF, and don't really know where to start. The requirements are fairly simple: I have an ObservableCollection of objects that have a timestamp property, and I need to be able to select one or more of them and drag them back and forth on a "timeline". All of the objects will be rendered the same size since there's no concept of a start/end time. Here is a mockup of what I'm trying to build:
I've spent hours searching for examples or tutorials, but when I search for "timeline" controls most of the results address a completely different problem:
Rather than going with the classic override of the Thumb control, I threw together a quick framework based off your mockup:
ZIP: http://www.mediafire.com/file/fidg8ea88ofoki4/TimelineFramework.zip
VirusTotal: https://www.virustotal.com/en/url/7579b365749d07eb743643ab118de71c7dd09cb03df7a8b28fbf3cec816ff4cc/analysis/1484802709/ and https://www.virustotal.com/en/file/4899aa96234e1e69c4e935f7d692e46789d8b4b7a5afd4c354937ed921986b20/analysis/1484802463/
It's pretty basic in terms of WPF as it's mostly C# with little XAML, so you can probably figure it out real fast and then be on your way to adapting it for your specific needs.
In the demo app, it looks like this:
The blue bands are draggable, with the thick center line representing their true placement and the faded side blue making for a bigger grip to click on. Hovering over a band shows its placement in the Tooltip. Of course, you can restyle and adjust all the visuals to your needs.

Converting a WinForms form to WPF

I've written a media player application in C#, and I was wondering if there was any way to use WPF to change the border of the form without converting the whole project over to WPF. I'm asking this because I only have 2 and a half days to get the border changed, and I don't think converting the whole project over would be possible in that amount of time (I don't think 2 days would be long enough to learn the amount required for a port to WPF). Would it be simpler to try and change the border in C#?
First, remove the border of the form.
form.FormBorderStyle = FormBorderStyle.None;
Then, change the background of the form to an image you create that has a custom border in the image.
form.BackgroundImage = image;
This will get you where you need to be, in the short term. In the long run, you are better off biting the bullet and switching to WPF if this is a project you will be maintaining and adding features to.
Use a WindowsFormsHost to host your WinForms form in a WPF window/application. Then you can have a WPF border for the WindowsFormsHost control.
You will only need to know WPF at a hello-world level and all of the application logic can stay inside the WinForms form.

Docking buttons in the wpf panels

How can it be done? If there are, for example, four groups of buttons in menu-like panel. How would you dock them to their initial location if the window is resized?
I am trying this using DockPanel and HorizontalAlign, but it seems to only be work for the last button on the right when the window is resized. But how do you dock(anchor) a group of buttons? Maybe put them in border object and use HorizontalAlign for it? Is there more elegant way to do this?
To summarize the comments: I don't know your background but it seems you are used to another way of UI design where you do not explicitely have to specify grouping etc in code. While that might seem more elegant, it is not: the designer generated code is awful and the whole system is not as flexible nor srtaightforward as what WPF gives you..
With WPF you get a clear one-to-one relationship between your intent (treating buttons as a group within a layout) and the actual code (put the buttons in a stackpanel/grid/...). Or draw a border around buttons and organize them vertically within the border vs in xaml use a border with a stackpanel inside. It won't get any more elegant than that.
Read up on WPF layouts and once you'll get a grip of it you will quickly see that it is rather powerful and beatiful at the same time. I found this tutorial pretty helpful when just starting with layouts. And google provides lots and lots and lots more information, as usual.
Like stijn said, put the buttons in a Grid or a Stackpanel and you'll be fine.
You may not think it's beautifull, but it's the best solution for your problem.

Categories

Resources