WPF WebBrowser in TabItem not showing - c#

I'm using WPF and need to populate a dynamically generated TabControl with a number of tabs.
I'm having trouble with the WebBrowser element, which seems to successfully navigate (I can see the mouse changes cursor as I hover through different elements) but the browser only displays white.
My code is below:
WebBrowser browser = new WebBrowser();
TabItem aTabItem = new TabItem() { Header = "My Tab", Width = 180, FontSize = 16, Content = (browser as WebBrowser) };
(Form.FindName("MyTabControl") as TabControl).Items.Add(aTabItem);
(Form.FindName("MyTabControl") as TabControl).SelectedItem = aTabItem;
browser.NavigateToString("http://www.google.com");
So basically I create the TabItem, add the WebBrowser to it, add the TabItem to the TabControl which will hold all tabs created.
When I try this the WebBrowser isn't displayed. If I swap it for a Label with some text, the Label will happily show up.
Could you give me some pointers?
Thanks

Found the issue. The window where I had the WebBrowser was set as AllowsTransparency="true". I should have known better...

This works for me,
WebBrowser browser = new WebBrowser();
TabItem TI = new TabItem() { Header = "My Tab", Width = 180, FontSize = 16, Content = (browser as WebBrowser) };
tabcontrol.Items.Add(TI);
browser.Source= new Uri("http://www.google.com");
I have used the Source of WebBrowser control. and i am creating the Uri.
NavigateToString(string text) works like below from MSDN
If the text parameter is null, WebBrowser navigates to a blank document ("about:blank").
If the text parameter is not in valid HTML format, it will be displayed as plain text.
After navigation, Source will be null.

Related

How to take the print of wpf user control using c#?

I have one user control in which i displayed the map and the data in the datagrid.
I have tried following code but it does not works.
PrintDialog printDialog = new PrintDialog();
// if user clicks on cancel button of print dialog box then no need to print the map control
if (!(bool)printDialog.ShowDialog())
{
return;
}
else // this means that user click on the print button
{
// do nothing
}
this.Measure(new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight));
this.Arrange(new Rect(new Point(20, 20), new Size(this.ActualWidth, this.ActualHeight)));
// print the map control
printDialog.PrintVisual(this, "Karte drucken");
Issue : When data grid has large number of records then user control gets scroll bar, but after printing the user control, only visible part of user control is printed and not the data present which we can see after scrolling down.I want to print whole content of user control.
Is there any solution for this, also how can we see print preview in wpf ?
Please check the following link, it should be helpful. Print Preview WPF
Code
PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
if (printDlg.ShowDialog() == true)
{
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
this.ActualHeight);
//Transform the Visual to scale
this.LayoutTransform = new ScaleTransform(scale, scale);
//get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.
this.Measure(sz);
this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
//now print the visual to printer to fit on the one page.
printDlg.PrintVisual(this, "First Fit to Page WPF Print");
}

Position of link in LinkLabel

I want a box of a fixed size to show some text and have a link in it that is clickable in the bottom right corner for editing. Clicking this edit link shows a set of fields to fill in.
I tried LinkLabel, which does the trick, but, when I change the text, the size of the box changes. I set autosize to false and longer text forces the link outside the box. Short text puts the link to far up.
I could get fancy and calculate the position of the link and insert it at the appropriate place (adding new lines if needed), but I'm wondering if there isn't an easier way to do this.
Is there a better control for doing this or another way of doing this?
EDIT:
The boxes that are filled in are concatenated and replace the text in the linklabel. The Edit link is currently appended to this and a LinkArea (of the last 4 characters) is set.
You need to build a composite layout, for instance using a Panel with Label/TextBox (Dock = Fill) and LinkLabel (Dock = Bottom, TextAlign = MiddleRight) inside, like this
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Samples
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var text = "I want a box of a fixed size to show some text and have a link in it that is clickable in the bottom right corner for editing.";
int textSize = 50;
var form = new Form { Padding = new Padding(8) };
var panel = new Panel { Parent = form, BorderStyle = BorderStyle.FixedSingle, Padding = new Padding(4) };
var label = new Label { Dock = DockStyle.Fill, Parent = panel, AutoSize = false, Text = text, Height = textSize };
var link = new LinkLabel { Dock = DockStyle.Bottom, Parent = panel, AutoSize = false, TextAlign = ContentAlignment.MiddleRight, Text = "Edit" };
panel.Location = form.DisplayRectangle.Location;
panel.Width = form.DisplayRectangle.Width;
panel.Height = panel.Padding.Vertical + link.Height + label.Height;
Application.Run(form);
}
}
}
Result:
I found another way.
I use a text box and position a linklabel in its corner.
textbox is readonly and disabled so it acts like a label and the backcolor is set to white to over-ride the disabled/readonly colour.
The edit link now stays put

HyperLink text not rendered after controls are added

I have a HyperLink control with text in its Text property.
With the following code:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Text = "Test";
link.Controls.Add(img);
When I do this, the image is rendered inside a a tag, but the text is not rendred.
Is there a way to render both the image and the text inside the Text property without throwing a third control in to the mix?
When you put any controls into the WebControl.Controls collection, it will ignore what you have inside Text. So if you want to render both text and other child controls, you should add the text into Controls:
var link = new HyperLink();
var img = new HtmlGenericControl("img");
img.Attributes.Add("src", "text.png");
link.Controls.Add(new Literal{ Text = "Test"}); // this line will add the text
link.Controls.Add(img);
I feel this should work out for you.
var link = new HyperLink();
var img = new HtmlGenericControl("img");
var lbl = new Label();
img.Attributes.Add("src", "text.png");
lbl.Text = "Test";
link.Controls.Add(img);
link.Controls.Add(lbl);
this.Controls.Add(link);
According to the MSDN article "The HyperLink control can be displayed as text or an image." So the answer is no, I'm afraid.

Any idea why my usercontrol isn't centering in my tabcontrol?

I'm a little confused as to why this isn't working, since I had it working in a prototype and the only big difference I think is that I use a custom TabItem and UserControl instead of the default ones. I'm trying to get the usercontrol that appears to be centered in the tab window, but it seems to be aligned left.
You hand this method the usercontrol you want to use and it formats it and sticks it in the tabcontrol. In a test solution I did of this earlier, setting scroll's horizontal and vertical alignment to stretch fixed this, but it's not working in this case. Is there some other setting or something else somewhere that would override this?
public void CreateNewTab(UserControlGeneric new_user_control, string tab_header)
{
//TabItem tab = new TabItem();
TabItemIndexed tab = new TabItemIndexed();
//The scrollviewer is created/setup to make sure the usercontrol gets scroll bars if the window if ever made smaller than the usercontrol
ScrollViewer scroll = new ScrollViewer();
//How you programatically set a scrollviewer's height and width to be "Auto"
scroll.Height = Double.NaN;
scroll.Width = Double.NaN;
scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
scroll.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
scroll.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
scroll.Content = new_user_control;
tab.Content = scroll;
tab.Header = tab_header;
//If there aren't any tabs, then hide the "No Workspaces Open" notice (Since we're adding a tab)
if (!tabControl_main.HasItems) label_no_workspaces_open.Visibility = System.Windows.Visibility.Hidden;
tabControl_main.Items.Add(tab);
tabControl_main.SelectedItem = tab;
}

How to create, in code behind, a Custom MessageBox with Images in WPF C#?

How to create, in code behind (with no XAML), a custom MessageBox (Dialog Boxes) in WPF C#?
I googled it and seems not to find a solution. I would want to have a MessageBox with Images and other Controls add to it.
You may use this solution:
string messageBoxText = "Do you want to save changes?";
string caption = "Your caption";
MessageBoxButton button = MessageBoxButton.YesNoCancel;
MessageBoxImage icon = MessageBoxImage.Warning;
MessageBox.Show(messageBoxText, caption, button, icon);
look over this article, you my recode all Xaml into pure c# in Custom Dialog Boxes paragraph if you want.
or you may create your own Window and use MyWindow.ShowDialog().
Like in this code:
Window wnd = new Window();
Grid grid = new Grid();
wnd.Height = 200;
wnd.Width = 150;
grid.RowDefinitions.Add(new RowDefinition {Height = new GridLength(100) });
grid.RowDefinitions.Add(new RowDefinition {Height = GridLength.Auto });
wnd.Content = grid;
Image img = new Image();
Button btn = new Button();
btn.Content = "OK";
btn.VerticalAlignment = VerticalAlignment.Bottom;
Grid.SetRow(img, 0);
Grid.SetRow(btn, 1);
grid.Children.Add(img);
grid.Children.Add(btn);
wnd.Owner = MyMainWindow;
wnd.ShowDialog();
Why don't you just create your custom Window in XAML and then use showDialog() in code-behind?
Everything from XAML can be rewrited in pure c#:
<Window>
<Grid>
<Grid.ColumnDefinition Width="50" />
<Grid.ColumnDefinition Width="*">
<Label Grid.Column="0" Content="Hello World!" />
</Grid>
</Window>
will looks like this:
public void MakeWin(DependencyObject owner)
{
MakeWin(Window.GetWindow(owner));
}
public void MakeWin(Window owner)
{
Window window = new Window();
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50) });
grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto});
window.Content = grid;
Label label = new Label { Content = "Hello World!" };
Grid.SetColumn(label, 0); // Depandency property
grid.Children.Add(label);
window.Owner = owner;
window.ShowDialog();
}
For images, there is source code (or a prebuilt one) for you in the WPF toolkit http://wpftoolkit.codeplex.com/wikipage?title=MessageBox&version=31
If you need more than an image, a line of text and a couple of buttons, then you should probably start looking at just using a new Window invoked with ShowDialog()
I've implemented a WPF MessageBox that has the exact same interface has the normal one and is also fully customizable via standard WPF control templates:
A Customizable WPF MessageBox
Features
The class WPFMessageBox has the exact same interface as the current WPF MessageBox class.
Implemented as a custom control, thus fully customizable via standard WPF control templates.
Has a default control template which looks like the standard MessageBox.
Supports all the common types of message boxes: Error, Warning, Question and Information.
Has the same “Beep” sounds as when opening a standard MessageBox.
Supports the same behavior when pressing the Escape button as the standard MessageBox.
Provides the same system menu as the standard MessageBox, including disabling the Close button when the message box is in Yes-No mode.
Handles right-aligned and right-to-left operating systems, same as the standard MessageBox.
Provides support for setting the owner window as a WinForms Form control.

Categories