I want to add a WPF Input Box in my Project in C#. I got a WinForm one from InputBox in C# but it has Winform look and feel. So i was recreating it in WPF. I have created all the controls (Label, Button, Textbox) but i am unable to add them to my window.
static Window winInputDialog
The Window is showing through ShowDialog but without controls.
There are two ways to get controls in your window:
Do the whole designing stuff in the Designer of VisualStudio
Add the controls by code. Here is a short, simple sample of creating a window and putting controls in it:
var window = new Window();
var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
stackPanel.Children.Add(new Label { Content = "Label" });
stackPanel.Children.Add(new Button { Content = "Button" });
window.Content = stackPanel;
Related
How to add user control to tab control in winforms and how to open entire tab control panel on single button click?
Before the question was edited:
Use System.Windows.Forms.Integration.Elementhost.
MSDN.
// Create the ElementHost control to host the WPF UserControl.
ElementHost WPFHost = new ElementHost();
WPFHost.Dock = DockStyle.Fill;
// Create the WPF UserControl.
HostingWpfUserControlInWf.UserControl1 uc = new HostingWpfUserControlInWf.UserControl1();
// Add the WPF UserControl to the Host.
WPFHost.Child = uc;
// Add the ElementHost to the form.
this.Controls.Add(WPFHost);
Or you can use XAML to design Winform Project GUI
I'm working on a scorekeeping app in WPF for Darts, and part of it is to display the score, which the player has thrown. I'm trying to output it on the window using dynamically created labels like this:
Label throwLabel = new Label();
throwLabel.Content = score.ToString();
throwLabel.Style = Resources["BoardStats"] as Style;
throws.Children.Add(throwLabel);
Where throws is a Stack Panel and BoardStats is a style defined in app XAML. The problem is that when the label appears on the window, it doesn't have the style I have given it in the code above. How can I fix this?
I'm having a difficult time trying to work with tooltips.
I'm trying to add a tooltip with different text to each dynamically created tab in a tabcontrol.
It's important to note that this tab is created from a form that contains a docked form where the tabcontrol is in.
This is, a main Form with a docking area, in where I have docked a results form, which contains an - initially empty - tabcontrol.
When you start the application this results form doesnt exists, I also create it dynamically whenever the user press certains parts of the main form, each one created as a new tab in the results form tabcontrol.
This is how I generate the tabs:
generateResultForm();
TabPage newtp = new TabPage("Nuevo paciente")
_result.TabControl.TabPages.Add(newtp);
newtp.Name = setTabName("np");
Now, I've tried putting a tooltip in the results form, then tried to first generate the tooltip by adding below something like _result.ResultsTooltip.SetToolTip(newtp, "Creación de un nuevo paciente.");, which didn't work. Then, since once the tab is created, it becomes selected, I tried to add it in the results class by something like WorkareaTooltip.SetToolTip(tabControl.SelectedTab, "Cosas"); in the selectedindexchange event from the tabcontrol.
I don't think it would have been a great solution, but I don't know what else to try.
Of course the tabcontrol has its ShowToolTips property set to true.
If anyone could help me that'll be great.
Thanks for reading, and sorry if there are any language mistakes :)
//EDITED
This is the code i'm actually using (and doesn't work)
TabPage newtp = new TabPage("Nuevo paciente");
_workareaform.TabControl.TabPages.Add(newtp);
newtp.Name = "np";
var tooltip = new ToolTip();
tooltip.SetToolTip(newtp, "Creación de un nuevo paciente.");
Now, it doesn't work, might be because of the whole configuration.
Just to be clear, this tab is in a TabControl which is in a Form docked into a dockContainer in another Form.
Here is an image if it.
http://i.imgur.com/fVz6e06.png
As you can see, no tooltip at all.
Have you tried setting ToolTipText property as shown below?. It worked for me.
_result.TabControl.ShowToolTips = true;
TabPage newtp = new TabPage("Nuevo paciente");
_result.TabControl.TabPages.Add(newtp);
newtp.ToolTipText = "this is tooltip";
If you're working with another form you need to reference the other form TabControl
In this sample I create a Form1 instance (with TabControl in it) from my Form2 and then add page and tooltip.
private void Form2_Load(object sender, EventArgs e)
{
//instantiate another form
var f1 = new Form1();
//find tabcontrol on new form
var tc = (TabControl) f1.Controls["tabControl1"];
//create page
TabPage newtp = new TabPage("Nuevo paciente");
newtp.Name = "Paciente1";
tc.TabPages.Add(newtp);
//add tooltip
var tt1 = new ToolTip();
tt1.SetToolTip(newtp, "paciente 1 tooltip");
//show other form
f1.Show();
}
When clicked show button I show win form inside tabcontrol which elements of win form, but I can not add wpf form inside this tabcontrol;
Faturalar() is the win forms;
Fatura.Faturalar ftr = new Fatura.Faturalar();
ftr.TopLevel = false;
ftr.Visible = true;
ftr.FormBorderStyle = FormBorderStyle.None;
ftr.Dock = DockStyle.Fill;
tabControl1.TabPages[0].Controls.Add(ftr);
it works but;
MainWindow is wpf forms;
MenuYonetimi.MainWindow mny = new MenuYonetimi.MainWindow();
tabControl1.TabPages[2].Controls.Add(mny);
it doesn't allow to do
Top-level Windows cannot be contained inside other Windows or "form"s if you want to call it that. If you need WPF content inside a winforms app you will need to place an ElementHost inside the form and place your WPF UI (probably in a WPF UserControl). But there's no way that any form or window will contain another Window.
In Windows Forms, I have created a Twitter application that gets the latest tweets from the Timeline using TweetSharp. I have used a UserControl and a Panel to display these. The code I've used to do this looks like this:
IEnumerable<TwitterStatus> tweets = service.ListTweetsOnHomeTimeline();
foreach (var tweet in tweets)
{
TweetBox t = new TweetBox();
t.username.Text = tweet.User.ScreenName; // Label
t.display.ImageLocation = tweet.User.ProfileImageUrl; // PictureBox
t.tweet.Text = tweet.Text; // Label
t.info.Text = tweet.CreatedDate.ToString();
t.Dock = DockStyle.Top;
HomeTimeline.Controls.Add(t); // Add to HomeTimeline Panel
}
I'm now re-making this application in WPF. The UserControl is in the same format, but I'm clueless as of how to add this to the panel and dock it to the top - or the equivalent in WPF. How do I do this?
A WinForms UserControl are not the same as a WPF UserControl.
You'll have to make the UserControl again, in WPF.
Of course, you could wrap the WinForms UserControl in a WindowsFormsHost element, but that's not good practice.