Adding controls to TabItems dynamically at runtime in C# - c#

So, here is my issue, im working in a Wpf application, so i dont have all of the standard windows form controls.. so im attempting to use a windowsformshost, to hold a webbrowser inside of a tabitem. So here is what i have:
Tab t = new Tab();
Browser newbrowse = new Browser(t);
WindowsFormsHost host = new WindowsFormsHost();
Grid g = new Grid();
host.Child = newbrowse;
newbrowse.Dock = DockStyle.Fill;
g.Children.Add(host);
t.Header = "New Tab";
t.Content = g;
tabControl1.Items.Add(t);
now, Tab, and Browser, are just my custom implementations of the controls, and they both are tested and working. So that is not the issue. Now as far as i can see, that code should work. But I'm left staring at a blank tab page. Note that this will need to be in the codebehind and cant be included in the WPF itself.
Thanks for any input! :) Cheers
EDIT: Note that i have also tried the operation with the standard, controls.tabitem, and forms.webbrowser to the same effect

I just tried this, and apparently what breaks your plan is the line:
newbrowse.Dock = DockStyle.Fill;
Comment it out, and watch how it suddenly works!

Related

TabControl (SelectedTab) does not work, how can I fix this?

I am working on a Project in the Moment and have a Problem with the selection of a Tab in TabControll. I tried to select a Tab with TabControl.SelectedTab, but then an error appears that says, that SelectedTab does not exists. Is there anything I am missing? Down below is the Code how I tried to select a Tab.
TabControl tabControl1 = new TabControl();
tabControl1.SelectedTab = tabControl1.TabPages["tb_config"];
Thx for any help :)
Ok I finally got the solution:
System.Windows.Forms.TabControl tabControl1 = new System.Windows.Forms.TabControl();
this.tabControl1.SelectedIndex = 2;
If I use this Code, it works without Problems :)

How to show a WinForms Window in a WPF Grid or Viewbox

In xml file:
<Viewbox name="viewbox1"></Viewbox>
In C# code-behind:
windowformClass window1 = new windowformClass(); // this is the WinForms object, not WPF
viewbox1.Child = window1;
When I assign window1 to viewbox1 it shows an error that cannot convert window form to System.Window.ElementUI
What's the right way to do that?
You should use WPF's interop host control for WinForms called WindowsFormsHost.
The code below was extracted and adapted from MSDN
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create your control.
windowformClass window1 = new windowformClass();
// Assign the control as the host control's child.
host.Child = window1;
// Set the interop host control as the ViewBox child.
viewbox1.Child = host;
A user reported a problem with hosting the WindowsFormHost inside a ViewBox. You should check that out in case you have problems with that too.
As you are (apparently) a newcomer, I'd like to give you a little tip:
There are plenty of resources on this subject on both MSDN and SO that could be found through a simple search on your favourite search engine.
Next time, try to find a solution before posting a question, as to keep only the best answers on the topic and save the moderators the work of closing questions as duplicates.
For more on that, check the help from SO

LongListSelector interaction breaks when a popup is open in WP8

To dynamically place some content on a page in a Windows Phone 8 project, we use a popup with a grid to host the content.
When this page contains a LongListSelector control, the Tap interaction to stop scrolling no longer works. Swiping up and down works as expected.
The issue can be reproduced very easily by starting with a new Databound app and adding this piece of code in the page constructor:
private Popup p;
p = new Popup();
Grid grid = new Grid();
grid.Width = Application.Current.Host.Content.ActualWidth;
grid.Height = Application.Current.Host.Content.ActualHeight;
p.Child = grid;
p.IsOpen = true;
Using this code you can make the LongListSelector scrolling but a Tap does no longer work to stop the scrolling.
Has anyone seen this issue and found a solution or might this be a known issue with the LongListSelector?
The invisible Grid that you're putting over the LongListSelector (actually the whole page) is capturing the tap event and because the popup is not part of the visual tree the event doesn't bubble as you're expecting.
The anomaly here is that you can actually interact with the LLS at all.
The real question here isn't why this happens but why you'd do this? Obviously your reproduction is very simple but it's to a point that makes no sense.
What are you ultimately trying to achieve?
There are almost certainly more appropriate alternative ways to achieve your end goal.

WP7 - ContextMenu doesn't show on Hyperlink

I'm trying to programmatically add a ContextMenu to a Hyperlink. I've searched in the documentation and in forums, and seems that the correct solution should be this:
var link = new Hyperlink();
link.Inlines.Add(new Run() { Text = linkText });
link.FontWeight = FontWeights.Bold;
link.TargetName = linkText;
link.Click += new RoutedEventHandler(link_Click);
ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Click += new RoutedEventHandler(CopyLink);
item.Header = "copy link";
item.Tag = linkText;
menu.Items.Add(item);
ContextMenuService.SetContextMenu(link, menu);
This code compiles and does not throw any exception. I've tested and the ContextMenu is indeed added to the Hyperlink. The problem is that it will not show anytime. I can tap & hold the link all the time of the world and the menu will not appear. Also tried to add a listener with GestureService and GestureListener, but the Hold event does not fire at all.
Anyone can help me? Thanks.
You can't do ContextMenus on Hyperlinks. You can do it on a HyperlinkButton, though. I'm not exactly sure of the reason, but it does work. Depending on what you're trying to do, HyperlinkButton may have been what you wanted anyway (Hyperlink is usually only used inside documents of text).
I just found the answer while reading the Windows Phone 7.5 Unleashed book by Daniel Vaughan. My problem was that I needed to add a hyperlink in a RichTextBox, and I can only use Inlines to show text. Hyperlink is an Inline, but does not support ContextMenu. As Tim suggested, I needed to use HyperlinkButton. The solution is that there is a class called InlineUIContainer. So, this
var inline = new InlineUIContainer { Child = SomeHyperlinkButton }
made the trick for me.

C# dynamically add text to tabcontrol

Having a slight problem on C#, still quite new to the language but hoping you can help. I have a program which dynamically creates tab forms and then I'm trying to add controls to the tabform (text boxes and labels), but no matter what I try it just doesn't seem to want to work. Here's the code I'm currently using (just to get one textbox in each form):
int i = dogresults;
while (i > 0)
{
i--;
DataRow dogrow = ds1.Tables["confirmdogs"].Rows[i];
string dogname = dogrow.ItemArray.GetValue(3).ToString();
TabPage newpage = new TabPage(dogname);
tcNewCustomer.TabPages.Add(dogname);
TextBox tb1 = new TextBox();
tb1.Location = new Point(20, 10);
newpage.Controls.Add(tb1);
tb1.Name = "txtDogNo" + i;
}
Any help would be greatly appreciated!
EDIT: Doh! Got it!
You're not adding the new TabPage you're creating. This line:
tcNewCustomer.TabPages.Add(dogname);
should be like this:
tcNewCustomer.TabPages.Add(newpage);
(A small test app shows the tab pages being created without any textboxes with the first version, but with the second version working fine.)
That looks okay at a glance (although I haven't tried it - a short but complete demo program would help). When you say it "just doesn't seem to want to work" - what exactly is happening?
Have you tried moving the location down a bit? I know some controls are odd in terms of where their logical "top" is (i.e. it's not the first visible pixel).
What about setting the text in the textbox? Currently you're just setting the name...
Although I'd still expect you to see a border on the box + background colour assuming that differs from the tabpage background.

Categories