Is it possible to autosize WebBrowser control? - winforms

I need to display some portion of html in my windows forms application. It's necessary that this html will be displayed without any scrollbars.
I tried to use WebBrowser control for my task, but it lacks of AutoSize property. Is it possible to determine minimal height necessary to display all contents without scrolling somehow?

Set ScrollBarsEnabled to false. Define the size based on your target minimum width. Add a handler to the webbrowser documentcompleted event as follows:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Size = WebBrowser1.Document.Body.ScrollRectangle.Size
End Sub

Here's a link to a C# WebBrowser wrapper which may do what you need:
http://www.codeproject.com/KB/miscctrl/csEXWB.aspx?msg=2526724
To do what you need, I think you would access the document on the page and then the element you're displaying, and get its height and width properties, and then adjust your WebBrowser control to be a few pixels larger than that. I think the WebBrowser wrapper control in the link can do the first part of this task (get the element's height and width).

Related

WPF contentcontrol extend beyond the bounds of its parent control

This is a sort of extension of this question I asked yesterday (the question gave me a contentcontrol that can overlay the current control). I now have a contentcontrol that can be overlayed on the current control via bindings (a modal type window). This works well and I am happy with this. One great feature would be if I could get the overlay to go over its parent.
currently the overlay will go into "My Control" control. What I would like is if I can still define it in that control (as that is were it is needed), but when it is displayed it can cover the whole main content area and / or the main window.
is this even possible?
Thanks
Sure it's possible, just wrap it into a Popup! :) You might have to manually stretch it though, but Popup is the control which will let you go outside the bounds of the parent view.
Another way is to host the MainContent in a grid and add a collapsed content control after the MainContent.
To show the popup: put it in the collapsed content control and make it visible.
To hide the popup: collapse the contentn control and remove the popup.

Can we overlay a WPF element by another?

I've this situation:
A label placed in Footer Cell of a RadGridView doesn't have sufficient width available in its container for displaying its full text.
Is it possible by any ways to have the label cross its container boundaries and show full text?
Thanks!
The space given to any element depends on the parent control or Panel that contains it, as well as its Width, Height, Horizontal and VerticalAlignment, and Margin. Normally you can manipulate some combination of these directly to change overlapping behavior but by using a DataGrid control you've given up a lot of that control since things like ColumnSpan are set up by the control internally. You could try setting negative Margin values and changing the Panel.ZIndex but I doubt those will help.
The best solution I can recommend without more detail is to use TextWrapping or TextTrimming to avoid ugly clipping, maybe in combination with a ToolTip showing the full text.
You can overlay any WPF element by another anytime. Only place where it fails is the WebBrowser control .The WPF WebBrowser has not been improved a single bit from WinForms WebBrowser. It is still the same simple activex control. However, you can bypass even that with a tooltip control.
I believe you can either set ClipToBounds=False to allow it to expand outside its area, or set TextWrapping=Wrap to allow the text to wrap.
EDIT: Forgot ClipToBounds is only honored in the Canvas control, so wrap your label in a Canvas and set ClipToBounds=False and it should work.

Is it possible for WinForm controls (Panel,etc.) to grow like a webform control?

I'm converting an app from ASP.NET WebForms to WinForms. There is one asp.net page which contains a ListView/Repeater that contains several custom controls, which in turn contain a ListView with other custom controls. Basically the layout looks like a TreeView, but on each node/leaf there are few controls like comboboxes, etc.
When this is in ASP.NET, the page automatically lays itself out, so it is several screens tall - if I add 20 buttons into a Panel, it will grow and the browser will get scrollbars.
I'd like to do the same thing in a WinForms application - so I'll have a user control that will contain a lot of controls in a some variation of Panel (Flow, Table layout), and the controls might have another controls inside them, etc.
The problem is, that when I make winforms app, each control has specific height in the design time. I'd like some user controls to be able to grow with their contents - so they'll add up. In the main Form, there should be a vertical scrollbar, just like in the web browser when the generated page is taller than the screen.
I'd just like to get some general pointers in the right direction. Thanks.
Use Anchor and Dock container properties.
Yes, to expound on Anchor and Dock...try this
-Place a Panel on an empty form, and set its dock property to Top
-place a textbox in the panel, and Dock it to Full...it should fill the whole top panel
-Place a splitter on the form, and if not already docked correctly, set its dock to top
-place another panel below the splitter, and set its Dock to Fill
-place another textbox inside the lower panel and fill it as as well
Now you have a form with two resiable textboxes and will resize when the form does.
*you may have to set the textbox MultiLine property to true but not sure.
Hope this helps.
Anchor the controls to the parent. Anchoring all four sides will cause it to stretch.
If the Anchoring and Docking answers don't work for you, there is another option. It's not pretty, but you can access a control's properties and change them dynamically during runtime. You'd do something like: if(listBox.Items.Count > [yourVal]) listBox.height = [yourFormula] or something.
It's been a while since I've done a Win Form (and I don't have my IDE fired up at the moment) but I'm pretty sure there's even a ScrollPanel or other scrolling control that you can set on your form.
That said, when you're working with WinForms, the less scrolling you can make your users do, the better.

Redraw and flicker issues

I have an Outlook style app. So basically I have a sidebar on the left and on the right I have a panel control (pnlMainBody) that hosts content.
The content is typically a user control that I add to the panel when user clicks appropriate button in the side bar. The way I add the user control to the panel is as follows:
// _pnlEmails is the User Control that I am adding to the panel
_pnlEmails = new pnlEmails();
_pnlEmails.Dock = DockStyle.Fill;
this.pnlMainBody.Controls.Add(_pnlEmails);
Some of the user controls that I add to the main panel are quite complex UI-wise. So when this.pnlMainBody.Controls.Add(_pnlEmails); fires, I see the control appear on the screen, then it resizes itself to fill the body of the panel control.
It's quite ugly actually, so I was wondering whether there is a way to not show the resizing until it's actually done resizing?
I've tried setting the user control's .Visible to false. I've tried doing .SuspendLayout, all to no avail.
Is there a way to do this so the screen transitions are smooth?
First try to turn on double buffer painting in the parent form by setting:
this.DoubleBuffered = true;
Do that in your load handler or some such place to see if the flicker goes away.
If that doesn't work you can also try to set the DoubleBuffered property to true for the child controls if they are .NET Control-derived entities. Here's some code that I used recently to get controls that did not expose the double buffer property to paint nicely: (vb version. Do you need C#?)
Private Sub ForceDoubleBuffering(ByVal o As Object)
Dim ctrl As Control
Dim method As Reflection.MethodInfo
Dim flags As Reflection.BindingFlags
ctrl = TryCast(o, Control)
flags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
method = ctrl.GetType().GetMethod("SetStyle", flags)
method.Invoke(ctrl, New Object() {ControlStyles.OptimizedDoubleBuffer, True})
End Sub
I figured out the trick to solving the issue. As I long as I set up the Dock.Fill property after adding the control to the main panel, there is no flicker.
this.pnlMainBody.Controls.Add(_pnlEmails);
_pnlEmails.Dock = DockStyle.Fill;

Binding a Popup to another control's relative screen position

I'm writing an XBAP with a complex Popup (Canvas Z-index of 99 with a grid on it...) that I would like to "attach" to the button that opens it and follow that button around wherever it goes on the screen. For example, if the button is in a ListBox or an XamDataGrid I would like the popup to follow the button as it scrolls through. If it is beneath an Expander I want it to stay attached to the button when the expander forces it to move, etc.
Any Ideas?
When using a Popup, neither PlacementTarget nor CustomPopupPlacementCallback is used after the popup has originally appeared. So any use of these properties will not allow the popup to track the button as it moves.
Several ways occur to me of achieving what you desire:
Attach a custom Adorner to the button and put the popup inside it. Disadvantage: Popup is not connected to Button or surrounding elements, so it won't inherit properties & DataContext from them.
Attach a custom Adorner to the button. This adorner will get measure and arrange calls when the button moves relative to the AdornerLayer, allowing you to manually update the Popup position. As long as your AdornerDecorator doesn't move relative to your Window (eg if it is the direct child of the Window), you can easily detect the AdornerLayer being moved by monitoring changes to Window size. Disadvantage: Complex to code & get right.
Don't use a Popup at all. Instead wrap the button in a <Grid> alongside a <Canvas> with zero width and height and the desired position. Inside the <Canvas> add the UserControl for the popup with an appropriate ZIndex. It will extend past the edge f the Canvas, which is just fine in WPF. Instead of using a Popup control just control the visibility of the UserControl. Disadvantage: Will not really be totally on top of all other objects, can't extend off edge of window (may not be an issue for XBAP, though).
I'm not sure if it will auto-update for you or not, but the PlacementTarget property allows you to specify a control to position the popup relative to. If that doesn't work, then maybe CustomPopupPlacementCallback will do the trick?

Resources