I have an old WinForm that combines 2 objects, but almost all metadata of the result is lost after the combining.
To solve this I created a UserControl WPF and I am using it as an element host inside the mainwindow Form.
I would like to import these 2 objects to UC and after the user selected the metadata it should return the output to the mainwindow where the metadata and the data should be integrated.
One other problem I have is to Hide and Show the UserControl.
From the MainWindow Form:
private Ch FirstCh = new Ch();
private Ch SecondCh = new Ch();
public WinForm()
{
InitializeComponent();
elementHost1.SendToBack();
elementHost1.Hide();
}
private void UserCWPFButton_Click(object sender, EventArgs e)
{
elementHost1.Show();
elementHost1.BringToFront();
Ch ThirdCh = new Ch();
//Would like to use the ThirdCh from UserCWPF here
}
From the UserControl:
public partial class UserCWPF : UserControl
{
public UserCWPF()
{
InitializeComponent();
//Would like to use FirstCh and SecondCh here
}
private void CloseBtn_Click(object sender, RoutedEventArgs e)
{
this.Visibility = Visibility.Collapsed;
this.Visibility = Visibility.Hidden;
//Both of these leave me with a Gray square in front of the form!
}
}
So far I can only properly hide the UCwpf with .SendToBack, but I don't know how to do it within the UserCWPF.
Also, Does it make sense to use a User Control WPF in an old WinForm or it should just be another form?
Related
I have implemented a Singleton userControl in order to interact with another classes during the application is running. It works well in background but I want to show (set visibility = true) designed userControl when clicking on a button! When Application is running and I click the button it shows nothing in the designed form. here is the code that I set the visibility to true in Form.cs file:
private void ButtonSearch_Click(object sender, EventArgs e)
{
panelYellow.Height = buttonSearch.Height;
panelYellow.Top = buttonSearch.Top;
searchPanel.getInstance().Visible = true;
}
And here is part of my singleton userControl class definition:
public partial class searchPanel : UserControl
{
private searchPanel()
{
InitializeComponent();
}
private static searchPanel sp = null;
public static searchPanel getInstance()
{
if (sp == null)
{
sp = new searchPanel();
}
return sp;
}
}
It's been a while since I didn't touch winforms, but from what I can remember you cannot really put a private constructor for a UserControl / form.
But another thing that could work is that you can try adding it dynamically in its parent form by setting it with Parent property. Because from what I see, if you didn't put it in the designer, it will never show up.
Anyway the question would be:
- Why do you want to use a singleton for a userControl ?
You must add your searchPanel to form as a control.
private void ButtonSearch_Click(object sender, EventArgs e)
{
panelYellow.Height = buttonSearch.Height;
panelYellow.Top = buttonSearch.Top;
searchPanel.getInstance().Visible = true;
this.Controls.Add(searchPanel.getInstance()); // add this line
}
Also your implementation of Singleton is not wrong but can be improved.
public searchPanel()
{
InitializeComponent();
// write your Singleton code here
}
Can someone please illustrate for me how to set up a logic like this:
I have a WPF Control. When a button is pressed it does one of the two possible things.
A. It checks if a different WPF Window has been loaded. If it was, it triggers that window's Print method.
B. It checks if a different WPF Window has been loaded. If it was not, it instantiates that window and then triggers its Print method.
I struggle to understand the events system between two WPF Controls/Windows. It's a relatively new thing for me, so I would appreciate if someone walked me through this.
Ps. This is not a homework assignment, but rather a new hobby of mine. If its a totally noob question then just point me to a resource so I can educate myself.
Cheers!
First of all, what is the way by which you will check if new Window opened is what you need it to be ?
You might do this by comparing their Handle or their Type (public class MyWindowWithPrintMethod : Window).
There can be multiple ways of doing this.
I suggest my simple way, focusing on the WPF way, to solve your purpose in easiest way possible.
MyWindowWithPrintMethod obj_MyWindowWithPrintMethod;
private void btnNewWindow_Click(object sender, RoutedEventArgs e)
{
obj_MyWindowWithPrintMethod = new MyWindowWithPrintMethod();
obj_MyWindowWithPrintMethod.Show();
}
private void btnCheckNewWindow_Click(object sender, RoutedEventArgs e)
{
WindowInteropHelper tgtWindow = new WindowInteropHelper(obj_MyWindowWithPrintMethod);
foreach (Window w in Application.Current.Windows)
{
// Compare Handle
WindowInteropHelper wih = new WindowInteropHelper(w);
if (wih.Handle == tgtWindow.Handle)
{
((MyWindowWithPrintMethod)w).Print();
}
// Compare Type
if (w.GetType() == typeof(MyWindowWithPrintMethod))
{
((MyWindowWithPrintMethod)w).Print();
}
}
}
MyWindowWithPrintMethod.cs
public class MyWindowWithPrintMethod : Window
{
public void Print()
{
MessageBox.Show("Print invoked !");
}
}
This answer from this question about events from 2 windows may help:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Child childWindow = new Child();
childWindow.MyEvent += new EventHandler(childWindow_MyEvent);
childWindow.ShowDialog();
}
void childWindow_MyEvent(object sender, EventArgs e)
{
// handle event
MessageBox.Show("Handle");
}
}
Child window
public partial class Child : Window
{
// define event
public event EventHandler MyEvent;
protected void OnMyEvent()
{
if (this.MyEvent != null)
this.MyEvent(this, EventArgs.Empty);
}
public Child()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Child_Loaded);
}
void Child_Loaded(object sender, RoutedEventArgs e)
{
// call event
this.OnMyEvent();
}
}
The above code shows how to set up an event from one window to another. But, you might want to simply call a method in that other window instead. For example:
public void AddNewUser()
{
Window2 window = new Window2();
if (window.ShowDialog() == true)
{
// Update DataGrid
RefreshDataGrid();
}
}
If you are determined to stick with events, then you should read up on WPF routed events.
Ok, am going to try and ask this without sounding dumb. I have a user form that I create dynamic textboxes inside a panel. I want to reference these textboxes or the panel from another form to send the texbox data to excel. I need to know how I can reference these controls. Thank you in advance!!
Create public properties on the user control, one for each element you want to access.
public class MyControl : UserControl
{
public string Name
{
get { return textBoxName.Text; }
}
public string Address
{
get { return textBoxAddress.Text; }
}
...
Then use those from the parent control that hosts the user control.
string name = myControl1.Name;
string address = myControl.Address;
Assume you have Form1 that dynamically adds a TextBox to panel panel1 during the load event like this:
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.Add(new TextBox());
}
And you have Form2 from which you want to access the data on panel1. On Form2 you can add a public field to store the reference to panel1, but you can call it whatever you want. In this case, I use sourcePanel:
public partial class Form2 : Form
{
public Panel sourcePanel;
public Form2()
{
InitializeComponent();
}
}
Then you can pass the panel1 reference to any new Form2 instance when the new instance is created, for example from a button click event on Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.sourcePanel = panel1;
f2.Show();
}
Then on Form2, you can access all the values in the TextBoxes with code like this:
private void Form2_Load(object sender, EventArgs e)
{
foreach (TextBox txt in sourcePanel.Controls.OfType<TextBox>())
{
System.Diagnostics.Debug.WriteLine(txt.Text);
}
}
I'm new to wpf and c#. I have two windows like MainWindow and MainWindow2.
I have a button in MainWindow that opens the MainWindow2 and a GroupBox with a StackPanel.
In the MainWindow2 I have a button that I want to add a RadioButton to the StackPanel in the MainWindow.
Here's the MainWindow code
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
RadioButton butãoRadial = new RadioButton();
stackPanel1.Children.Add(butãoRadial);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow2 obj = new MainWindow2();
obj.Show();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
}
}
Here's the MainWindow2 Code:
public partial class MainWindow2 : Window
{
public MainWindow2()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MainWindow window = new MainWindow();
}
}
Sorry for my bad english ^^
Please do not do it that way.... every time you add controls directly to a window from another window a kitten dies. If you're not into kittens then substitute something else you hold dear into that sentence.
A more appropriate way to do this is to bind an ItemsControl (or a derivative like a ListView) to an ObservableCollection in your ViewModel, then use an DataTemplate defined in the ItemTemplate property to represent each item in the ObservableCollection on the screen. Then all you have to do is add the appropriate data item to the ObservableCollection (which can be passed to the second window).
Pleeeeeeeeaaaassssseee don't be adding controls from another window!
I am tinkering around with User Controls and I've found myself stumped. My form has 2 User Controls, a TitleScreen and a MainScreen. I need for the TitleScreen to be visible when the program runs and then invisible when the "new game" button is clicked, at which point the MainScreen needs to be visible. Unfortunately, I can't figure out how to do this.
FormMain.cs
#region Property Region
public UserControls.MainScreen ControlMainScreen
{
get { return controlMainScreen; }
}
#endregion
#region Constructor Region
public FormMain()
{
InitializeComponent();
controlMainScreen.Visible = false;
}
#endregion
TitleScreen.cs
public void btnNewGame_Click(object sender, EventArgs e)
{
this.Visible = false;
FormMain.ControlMainScreen.Visible = true;
}
Obviously it doesn't work, but I've tried everything that I know how to do. I also have no clue how to make controlMainScreen static since I didn't actually make the declaration (VS did). Any help would be appreciated.
You could use ParentForm which is a property on UserControl to get a reference to the parent form.
((YourFormsExactType)(this.ParentForm)).ControlMainScreen.Visible = false;
Add a user control on the Win project and then dynamically add to the Form.
Here is the code:
public Show_hideControl()
{
InitializeComponent();
//Adding First User control
Main _main = new Main();
panel1.Controls.Add(_main);
}
private void button1_Click(object sender, EventArgs e)
{
//removing already added user control
panel1.Controls.Clear();
//Adding second user control
Alternative _alter = new Alternative();
panel1.Controls.Add(_alter);
}