i have a Normal Panel in my windows form Application and i want to add a stack panel which is there in WPF inside it.
Stack Panel should Consists of 2 Expander Controls inside it.
i am using Element Host to Have the stack panel and expander Control in the windows form.
i am using the Following Code:
ElementHost WPF_Container = new ElementHost();
WPF_Container.AutoSize = true;
ElementHost WPF_Container1 = new ElementHost();
WPF_Container1 .AutoSize = true;
ElementHost StackPanelHost = new ElementHost();
Expander expander1 = new Expander();
expander1.Header = "Pipes";
Expander Expander2 = new Expander();
Expander2.Header = "Mountings";
uc1 uc1 = new uc1(); // Object Creation for User Control-1
uc2 uc2 = new uc2(); // Object Creation for User Control-2
StackPanel Stackpanel = new StackPanel();
Stackpanel.Orientation = System.Windows.Controls.Orientation.Vertical;
WindowsFormsHost host = new WindowsFormsHost();
host.Child = uc1 ;
WindowsFormsHost host2 = new WindowsFormsHost();
host2.Child = uc2;
expander1.Content = host;
Expander2.Content = host2;
Stackpanel.Children.Add(expander1);
Stackpanel.Children.Add(Expander2);
// WPF_Container.Child = expander1;
// WPF_Container1.Child = Expander2;
StackPanelHost.Child = Stackpanel;
panel1.Controls.Add(StackPanelHost); // Adding Stack Panel to my Winform Panel.
by using the above Code during Form Load Event no controls are getting added to my Windows form Panel.
How to Achieve the functionality
Here is an example of how you do it:
ElementHost host = new ElementHost();
StackPanel wpfPanel = new StackPanel();
host.Child = wpfPanel;
//panel1 is Windows Forms Panel
panel1.Controls.Add(host);
I recomend that you create the UI you need in a WPF user control and then add it to the ElementHost.
UPDATE:
ElementHost host = new ElementHost();
StackPanel wpfPanel = new StackPanel();
host.Child = wpfPanel;
//panel1 is Windows Forms Panel
panel1.Controls.Add(host);
var testButton = new System.Windows.Controls.Button(){Content = "test"};
wpfPanel.Children.Add(testButton);
Pay attention you are using the correct namespace as windows forms as also a Button Class
Related
So I have a Form that is a scale A4 page that allows users to drag and drop controls on the form for printing.
IE where ever the control is on the form its location is used to print the controls data, eg: File name or image, to that point of an A4 page.
However I have created a number of templates for the form that sets the controls in certain locations and adds in any missing controls. When the templates are selected any extra controls don't show on the form even though I call the Invalidate() method.
Here is my code for the method that adds the controls to the Form:
private void standardIDToolStripMenuItem_Click(object sender, EventArgs e)
{
selectedID = true;
selectedInvoice = false;
selectedLetter = false;
lblName.Visible = true;
lblDOB.Visible = true;
lblUID.Visible = true;
lblName.Location = new Point(200, 100);
lblDOB.Location = new Point(200, 125);
lblUID.Location = new Point(200, 150);
lblName2.Text = lblName.Text;
lblName2.Location = new Point(60, 750);
lblName2.Enabled = true;
lblName2.Visible = true;
lblDOB2.Text = lblDOB.Text;
lblDOB2.Location = new Point(60, 775);
lblDOB2.Enabled = true;
lblDOB2.Visible = true;
lblUID2.Text = lblUID.Text;
lblUID2.Location = new Point(60,800);
lblUID2.Enabled = true;
lblUID2.Visible = true;
hidden1.Location = new Point(300, 100);
DOBHidden.Location = new Point(300, 125);
UIDHidden.Location = new Point(300, 150);
#region ID Background placeholder
PictureBox backPic = new PictureBox();
backPic.Location = new Point(24, 48);
backPic.ForeColor = System.Drawing.Color.PaleGreen;
backPic.Size = new Size(504, 176);
backPic.Visible = true;
backPic.Show();
backPic.SendToBack();
this.Invalidate();
#endregion
}
Why will the new controls not appear on the form when I have called the Invalidate() method to force it to repaint?
It seems that you don't add them to Controls:
please try this on every control after you have specified location and the rest of the control initialization:
this.Controls.Add(lblName)
Mong Zhu seems to be right and I also suggest you to catch a glimpse of some kind of report designer if you can use some third-part libaries ( I'm not sure if winforms privides something like DevExpress reports for example)
I guess that it will be helpful with stuff you are doing in your project.
I am having TabControl in which there are multiple tabs. I need to have a functionality in which if I 'drag a tab' or 'right click on tab -> float' then that tab comes out from tabs list and becomes free float window(or a winform) which can be moved anywhere on the screen. And vice Versa.
Here is the code for the TabControl-
TabControl tabControl = new TabControl();
TabPage tabPage1 = new TabPage();
tabPage1.Text = "Tab Page 1";
TabPage tabPage2 = new TabPage();
tabPage2.Text = "Tab Page 2";
tabControl.Controls(tabPage1);
tabControl.Controls(tabPage2);
Please use below code for swaping Tab-Page controls to Form and vise-versa. Please note that instead of adding controls to Tab-Page, you need to first add controls in a panel and then that panel need to be added to the Tab_Page:
private void Button1_Click(object sender, EventArgs e)
{
TabPage tabPage1 = (TabPage) sender;
Form frm = new Form();
frm.Text = tabPage1.Text;
Panel panel = (Panel) tabPage1.Controls[0];
tabPage1.Controls.RemoveAt(0);
tabControl.TabPages.Remove(tabPage1);
frm.Controls.Add(panel);
frm.Show();
}
private void Form1_Click(object sender, EventArgs e)
{
Form frm = (Form) sender;
TabPage tabPage1 = new TabPage();
tabPage1.Text = frm.Text;
Panel panel = (Panel)frm.Controls[0];
frm.Controls.RemoveAt(0);
tabControl.TabPages.Add(tabPage1);
frm.Controls.RemoveAt(0);
frm.Hide();
}
When I do this in form load
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
this.Controls.Add(tb1);
this.Controls.Add(tb2);
It puts one textbox over another (not vertically or horizontally, but covering each other), which is not what I want.
I could manually try to position them programmatically, but is there a way where I can have each control appear adjacently when I add them?
You can use a FlowLayoutPanel.
Here a short example code that you can test using Linqpad
Form f = new Form();
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Dock = DockStyle.Fill;
flp.FlowDirection = FlowDirection.LeftToRight;
f.Controls.Add(flp);
TextBox t1 = new TextBox();
flp.Controls.Add(t1);
TextBox t2 = new TextBox();
flp.Controls.Add(t2);
f.Show();
My problem is that I want to use WPF expander object to host some winforms control. And the position that I'm going to use this is in my application's setting form. But, what I couldn't find is to add more than one control to it.
After a lot of searching for solution to my problem I just found this simple code that only add one control to the WPF expander object (I require more than one control to be added):
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Controls.Expander expander = new System.Windows.Controls.Expander();
expander.Header = "Sample";
WPFHost = new ElementHost();
WPFHost.Dock = DockStyle.Fill;
WindowsFormsHost host = new WindowsFormsHost();
host.Child = new DateTimePicker();
expander.Content = host;
WPFHost.Child = expander;
this.Controls.Add(WPFHost);
}
In this code the expander only hosts one control.
How should I customize it to host more than one control ?
Please help
Using a System.Windows.Forms.Panel as a container will help:
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Controls.Expander expander = new System.Windows.Controls.Expander();
System.Windows.Controls.Grid grid = new System.Windows.Controls.Grid();
expander.Header = "Sample";
ElementHost WPFHost = new ElementHost();
WPFHost.Dock = DockStyle.Fill;
Panel panel1 = new Panel();
DateTimePicker dtPicker1 = new DateTimePicker();
Label label1 = new Label();
// Initialize the Label and TextBox controls.
label1.Location = new System.Drawing.Point(16, 16);
label1.Text = "Select a date:";
label1.Size = new System.Drawing.Size(104, 16);
dtPicker1.Location = new System.Drawing.Point(16, 32);
dtPicker1.Text = "";
dtPicker1.Size = new System.Drawing.Size(152, 20);
// Add the Panel control to the form.
this.Controls.Add(panel1);
// Add the Label and TextBox controls to the Panel.
panel1.Controls.Add(label1);
panel1.Controls.Add(dtPicker1);
WindowsFormsHost host = new WindowsFormsHost();
host.Child = panel1;
expander.Content = host;
WPFHost.Child = expander;
this.Controls.Add(WPFHost);
}
I want to recreate a simple browser with tab capability. A new tab has to be created every time the user clicks on "button_addTab" or when the selected web site tries to open a new window.
Here is my code:
Add a new tab by pressing the dedicated button:
private void button_addTab_Click(object sender, EventArgs e)
{
TabPage addedTabPage = new TabPage("tab title"); //create the new tab
tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage, //add the new webBrowser to the new tab
Dock = DockStyle.Fill
}
addedWebBrowser.Navigate("www.google.com");
Prevent a site from opening new windows (if you know every webBrowsers name) :
private void specificWebBrowser_NewWindow(object sender, CancelEventArgs e)
{
WebBrowser thisWebBrowser = (WebBrowser)sender;
e.Cancel = true;
TabPage addedTabPage = new TabPage("tab title");
tabControl_webBrowsers.TabPages.Add(addedTabPage);
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage,
Dock = DockStyle.Fill
}
addedWebBrowser.Navigate(thisWebBrowser.StatusText.ToString());
}
Now my question is:
How can I modify the second branch of code in order to match all the web browsers created with no names when the first code is called (at one time)?
Try to modify the first function to attach an event handler to newly created browser:
private void button_addTab_Click(object sender, EventArgs e)
{
TabPage addedTabPage = new TabPage("tab title"); //create the new tab
tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the tab to the TabControl
WebBrowser addedWebBrowser = new WebBrowser()
{
Parent = addedTabPage, //add the new webBrowser to the new tab
Dock = DockStyle.Fill
};
addedWebBrowser.NewWindow += specificWebBrowser_NewWindow;
addedWebBrowser.Navigate("www.google.com");
}