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);
}
Related
I have a main form that has buttons, the role of one of them in disabling all controls of the form in general.
I also have a child form that inherits the buttons and the actions of them, as I could prevent the events of the parent form at a certain point from running that is, temporarily remove the event, for example if the user has a text and does not fill it out and gives the button I do not want anything to be disabled
I think there is a way in c# that like the "eventname"=- I don't remember well
This is an example of my base or parent form, with a button that does actions
Parent Form
namespace WindowsFormsApp1
{
public partial class BaseMantenimiento1 : DevExpress.XtraEditors.XtraForm
{
public BaseMantenimiento1()
{
InitializeComponent();
}
private void BaseMantenimiento1_Load(object sender, EventArgs e)
{
Guardar.Enabled = false;
Cancelar.Enabled = false;
controles.habilitarcontroles(false, xtraTabControl1);
}
private void Nuevo_Click(object sender, EventArgs e)
{
Guardar.Enabled = true;
Cancelar.Enabled = true;
Editar.Enabled = false;
Buscar.Enabled = false;
Nuevo.Enabled = false;
controles.habilitarcontroles(true, xtraTabControl1);
}
This is my child form with its basic code
child form
public partial class Mantenimiento_Empresas : BaseMantenimiento1
{
public Mantenimiento_Empresas()
{
InitializeComponent();
}
What I wanted was that if in the child form I gave the button save, but they did not fill the textbox well, then the event that this inherits from its parent form is not executed, so I took the event and then below if everything is fine I trigger the event of the parent with
base.Save_Click(null, null);
I found what I was looking for, in the same space where the components are initialized I must place the following
public Mantenimineto_Bicicletas()
{
InitializeComponent();
Save.Click -= base.Save_Click;
}
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?
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
}
The project I am working on needs a couple buttons on multiple forms, instead of doing the code shown below I was hoping it could be made global.
This is only one part of the project, all the code does is enlarge the button picture when the user hovers over it.
I've tried looking at classes, tags and attributes. I know classes can be made to use across multiple forms but I cant find out if they work with events.
private void btnEnter_MouseEnter(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Size = new Size(299, 102);
}
private void btnLeave_MouseLeave(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.Size = new Size(289, 92);
}
You can create an inherited button. Add a new class then make sure you put : Button after the class name.
using System.Drawing;
using System.Windows.Forms;
namespace InheritedButton
{
public class ExpandButton : Button
{
public Size EnterSize { get; set; }
private Size _LeaveSize;
public Size LeaveSize
{
get
{
return (_LeaveSize);
}
set
{
_LeaveSize = value;
this.Size = LeaveSize;
}
}
public ExpandButton() : base()
{
}
protected override void OnMouseEnter(EventArgs e)
{
this.Size = EnterSize;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
this.Size = LeaveSize;
base.OnMouseLeave(e);
}
}
}
Build your project and the new button will appear in the toolbox. Drop it onto a form/control and make sure you set the EnterSize and LeaveSize. EnterSize determines the size of the button when you mouse over and LeaveSize sets the initial size and sets the size of the button when you mouse out. You don't need to set the Size property, just set LeaveSize.
Any time you want to use the expanding/contracting button just use the inherited one instead.
Reuirement :
Need to show all child pages in a master layout. Without opening as new window i mean it should not be visible as separate window from main window
Solution i figured :
I used content presenter in main page.
Create all other pages as User controls.
On click of menu ViewWindow.Content = new SalesEntry();
By using that i am showing that.
Problem :
To close that user control i used a button click (button present inside the user control)
to preform this.Visibility = Visibility.Hidden;
But every time when user request this page the page is initialized and shown.
So, whats the best approach for this to overcome or any other way to solve this.
(I was told not to use any framework as a project requirement)
I am very new WPF..
Please help me in this..
What you are doing is fine, I don't really understand the problem here but I will tell you how I would do it.
You will have a parent view, a Window. You will have many childs, UserControl's.
Inside your window, you should have a way of selecting which child to show. This can be done using buttons or a menu.
When you select a child, you instantiate it as an object and subscribe to its exit event. When this event is fired by the child, you remove that child from your childs in the parent window.
// This one defines the signature of your exit event handler
public delegate void OnExitHandler(UserControl sender);
// This is your child, UserControl
public partial class MyChild : UserControl
{
public event OnExitHandler OnExit;
public MyChild()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.OnExit(this);
}
}
// This is your parent, Window
public partial class MainWindow : Window
{
private MyChild _control; // You can have a List<UserControl> for multiple
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_control = new MyChild();
_control.OnExit += _control_OnExit; // Subscribe to event so you can remove the child when it exits
_content.Content = _control; // _content is a ContentControl defined in Window.xaml
}
private void _control_OnExit(UserControl sender)
{
if(sender == _control)
{
// Or if you have a collection remove the sender like
// _controls.Remove(sender);
_control = null;
_content.Content = null;
}
}
}
If your problem is something else, please comment.