I am new to C#. I am using windows forms.
As shown in screenshot, I have form1 with 4 buttons and 4 user controls.
click show UserControl1 and UserControl1 shows up.
click show UserControl2 and UserControl2 shows up.
click show UserControl3 and UserControl3 shows up.
click show UserControl4 and UserControl4 shows up.
what I want to do is: when I click Show Previous UC (on User Control4) , the previous User Control shows up.
for example:
click show UserControl2 and UserControl2 shows up.
click show UserControl4 and UserControl4 shows up.
and now I want when I click Show Previous UC, UserControl2 shows up (the previous user control).
How Can I show the previous User Control ( the last one which was shown before the current one)?
Please help me, Thank you.
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
UserControl2 UC2 = new UserControl2();
UserControl3 UC3 = new UserControl3();
UserControl4 UC4 = new UserControl4();
public Form1()
{
InitializeComponent();
Controls.Add(UC1);
Controls.Add(UC2);
Controls.Add(UC3);
Controls.Add(UC4);
}
private void ShowUserControl1_Click(object sender, EventArgs e)
{
UC1.Visible = true;
UC2.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl2_Click(object sender, EventArgs e)
{
UC2.Visible = true;
UC1.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl3_Click(object sender, EventArgs e)
{
UC3.Visible = true;
UC1.Visible = false;
UC2.Visible = false;
UC4.Visible = false;
}
private void ShowUserControl4_Click(object sender, EventArgs e)
{
UC4.Visible = true;
UC3.Visible = false;
UC2.Visible = false;
UC1.Visible = false;
}
}
You need to keep track of which user control was the previous in the page (and possible also the current, unless you want to iterate the UCs to find out). Use fields for this in the page.
public partial class Form1 : Form
{
UserControl[] userControls = new []{
new UserControl1(),
new UserControl2(),
new UserControl3(),
new UserControl4()
};
UserControl previous;
UserControl current;
public Form1()
{
InitializeComponent();
foreach(var uc in UserControls)
{
uc.Click += ShowPrevControl_Click;
Controls.Add(uc);
}
}
In the event (you only need one)
private void ShowUserControl_Click(object sender, EventArgs e)
{
foreach(UserControl uc in UserControls)
{
if(uc.Name == (string)((Control)sender).Tag)
{
previous = current;
uc.Visible = true;
current = uc;
}
else
{
uc.Visible = false;
}
}
}
private void ShowPrevControl_Click(object sender, EventArgs e)
{
if (previous != null)
{
foreach(var uc in UserControls)
{
uc.Visible = false;
}
var temp = current;
previous.Visible = true;
current = previous;
previous = temp;
}
}
Set the Tag property for each button to hold the name of the UserControl it should control (UserControl1, UserControl2, UserControl3 or UserControl4).
Let all the buttons click events be handled by ShowUserControl_Click.
Create a new public event in your user controls (if all should handle clicks), that that page can handle using your ShowPrevControl_Clickmethod:
public UserControlx : UserControl
{
public event EventHandler Click;
public UserControlx()
{
Button.Click += Button_Click;
}
private void Button_Click(object sender, EventArgs e)
{
if(Click != null)
Click(this, EventArgs.Empty);
}
}
An easy solution is to add a member variable to your class.
So your class would look like:
public partial class Form1 : Form
{
UserControl prevControl;
// Etc...
So when you click the button for, say, UC2, you can set prevControl to UC2.
private void ShowUserControl2_Click(object sender, EventArgs e)
{
UC2.Visible = true;
prevControl = UC2;
UC1.Visible = false;
UC3.Visible = false;
UC4.Visible = false;
}
And on an event handler for the "Show previous control" button:
private void ShowPrevControl_Click(object sender, EventArgs e)
{
if (prevControl != null) prevControl.Visible = true;
}
UserControl is a reference in C#, which makes this possible.
Related
I want to display message that button is unclickable (I have used Enabled option, button1.Enabled = false/true; ). Does anyone know how to detect if button is clicked, when it is unclickable in order to display error message "Button is unclickable...".
Windows forms, C#
Whenever a standard control doesn't behave in exactly the way that need it to, all we usually have to do is make our own version that inherits the standard control so that we can make it do whatever we want. Your question offers a great reason for doing that because a normally a disabled Button is not going to fire a Click or a MouseDown event.
Here is a guideline example for a custom Button that:
Intercepts the Enabled property by declaring it new.
Leaves the base class button always responsive because it's always enabled.
Paints the control as dimmed if disabled.
Suppresses the firing of the Click event if disabled, and fires DisabledClick instead.
Look in the Title Bar to see when the button is clicked.
ButtonWithDisabledOption class
class ButtonWithDisableOption : Button
{
bool _enabled = true;
public new bool Enabled
{
get => _enabled;
set
{
if (!Equals(_enabled, value))
{
_enabled = value;
OnEnabledChanged(EventArgs.Empty);
}
}
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
if(Enabled)
{
ForeColor = SystemColors.ControlText;
BackColor = SystemColors.Control;
FlatStyle = FlatStyle.Standard;
}
else
{
ForeColor = Color.FromArgb(191, 191, 191);
BackColor = Color.FromArgb(204, 204, 204);
FlatStyle = FlatStyle.Flat;
}
}
protected override void OnClick(EventArgs e)
{
if (Enabled)
{
base.OnClick(e);
}
else
{
DisabledClick?.Invoke(this, EventArgs.Empty);
}
}
public event EventHandler DisabledClick;
}
MainForm.Designer.cs
Be sure to replace Button references with ButtonWithDisabledOption.
private void InitializeComponent()
{
// this.buttonWithDisableOption = new System.Windows.Forms.Button();
this.buttonWithDisableOption = new button_with_disabled_option.ButtonWithDisableOption();
...
}
// private System.Windows.Forms.Button button1;
private button_with_disabled_option.ButtonWithDisableOption buttonWithDisableOption;
TEST
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
buttonWithDisableOption.Enabled = checkBoxButtonEnabled.Checked = true;
// Button events
buttonWithDisableOption.Click += buttonWithDisableOption_Click;
buttonWithDisableOption.DisabledClick += buttonWithDisableOption_DisabledClick;
// CheckBox events
checkBoxButtonEnabled.CheckedChanged += checkBoxButtonEnabled_CheckedChanged;
}
private void checkBoxButtonEnabled_CheckedChanged(object sender, EventArgs e)
{
buttonWithDisableOption.Enabled = checkBoxButtonEnabled.Checked;
}
private void buttonWithDisableOption_Click(object sender, EventArgs e)
{
// Title bar
Text = $"Button Click {_tstcount++} (Enabled)";
}
private void buttonWithDisableOption_DisabledClick(object sender, EventArgs e)
{
// Title bar
Text = $"Button Click {_tstcount++} (Disabled)";
MessageBox.Show("Button is unclickable");
}
int _tstcount = 1;
}
I have this method(below) that works well First time it's called:
public void showForm(Form _form, Form _main)
{
if (_main != null)
{
foreach (Form otherForms in this.MdiChildren)
{
otherForms.Close();
}
}
_form.MdiParent = this;
_form.Dock = DockStyle.Fill;
_form.TopLevel = false;
_form.TopMost = true;
_form.FormBorderStyle = FormBorderStyle.None;
this.MDIChildPanel.Controls.Add(_form);
_form.BringToFront();
_form.Show();
}
on Form_Load event for MDI Parent Form i call the method above as shown below and it works well:
private void MDIRental_Load(object sender, EventArgs e)
{
showForm(new Login(), null);
}
I then try to call the method above from another event handler and it just doesnt work as show below: help
private void manageUsersToolStripMenuItem_Click(object sender, EventArgs e)
{
showForm(new Users(), null);
}
I have a usercontrol2 (custom) on a form. I have defined a tooltip on the user control. The usercontrol2 has some child controls on it.
I want the tooltip to be shown when i click on a specific child control on usercontrol2. The tooltip should hide only if i click outside the child control.
The tooltip is shown when i click on the child control but is hidden once i move the mouse position. Also, when i click outside the child control and i click on the child control again, the tooltip is not shown.
The child control is named usercontrol1.
public partial class UserControl2 : UserControl
{
private Boolean IsToolTipShown;
public UserControl2()
{
InitializeComponent();
WireControl(userControl11);
InitiliseToolTip();
}
private void WireControl(Control cont)
{
cont.MouseClick += UserControlMouseClick;
foreach (Control ctl in cont.Controls)
{
ctl.MouseClick += UserControlMouseClick;
if (ctl.HasChildren) WireControl(ctl);
}
}
private void UserControlMouseClick(object sender, MouseEventArgs e)
{
if (!IsToolTipShown)
{
string s = null;
for (int i = 0; i < 5; i++)
{
s += string.Concat(i, Environment.NewLine);
}
toolTip1.Show(s, userControl11);
IsToolTipShown = true;
}
}
private void InitiliseToolTip()
{
IsToolTipShown = false;
toolTip1.AutomaticDelay = 0;
toolTip1.BackColor = Color.Chartreuse;
toolTip1.StripAmpersands = true;
toolTip1.ToolTipIcon = ToolTipIcon.Info;
toolTip1.UseFading = false;
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
if (IsToolTipShown)
{
toolTip1.Hide(userControl11);
IsToolTipShown = false;
}
}
}
I have two windows forms in my application. First one is Main form and the second one is lookup form. I'm trying to open lookup form from the main form in a text box key leave event and then I'm opening the lookup form. My lookup form has a data grid view and I' loading it in the form load event of the lookup form. I'm reading my selected value on the grid view of the lookup window to an object. I want to close the lookup window as soon as I read the values of the selected row to the object and I want to pass it to the main form? How can I do that?
This is what I have done.
In the main form.
LookupModelType="";
if (e.KeyCode.Equals(Keys.F3))
{
foreach (Form frm in Application.OpenForms)
{
if (frm is FormControllers.Lookup)
{
if (frm.WindowState == FormWindowState.Minimized)
{
frm.WindowState = FormWindowState.Normal;
frm.Focus();
return;
}
}
}
LookupModelType = "Product";
FormControllers.Lookup newLookUp = new FormControllers.Lookup(LookupModelType);
newLookUp.ShowDialog(this);
}
In the lookup window
private string GridType = "";
public Lookup(String LookupModelType)
{
InitializeComponent();
this.GridType = LookupModelType;
}
private void Lookup_Load(object sender, EventArgs e)
{
if (GridType == "Product")
{
using(DataControllers.RIT_Allocation_Entities RAEntity = new DataControllers.RIT_Allocation_Entities())
{
dgvLookup.DataSource = RAEntity.TBLM_PRODUCT.ToList<DataControllers.TBLM_PRODUCT>();
}
}
dgvLookup.ReadOnly = true;
}
private void dgvLookup_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
{
return;
}
int index = e.RowIndex;
dgvLookup.Rows[index].Selected = true;
}
you can do it like blow :
in the Main form :
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F3)
{
LookupForm look = new LookupForm();
var result = look.ShowDialog();
if(result == DialogResult.OK)
{
MessageBox.Show(look.data.ToString());
}
}
}
and in the look up form you have to declare 1 variable and fill whenever cell clicked
public partial class LookupForm : Form
{
public object data = new object();
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
data = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
of course, for better performance, you can declare the variable in specific type
To share data between Parent Child forms using events, here are the things needed:
A public custom event args class to share data.
Child form to have a event.
In your parent form whenever you create an instance of child, you
need to register eventhandlers
Please note that the code below is just a demo code and you will need to add null checks etc. to make it "robust".
Custom event args below
public class ValueSelectedEventArgs : EventArgs
{
public object Value { get; set; }
}
Your lookup form should have the following event declared:
public event EventHandler ValueSelected;
protected virtual void OnValueSelected(ValueSelectedEventArgs e)
{
EventHandler handler = ValueSelected;
if (handler != null)
{
handler(this, e);
}
// if you are using recent version of c# you can simplyfy the code to ValueSelected?.Invoke(this, e);
}
In my case I am firing the event on listbox selected index change and closing the form as well. Code for it:
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var i = this.checkedListBox1.SelectedIndex;
ValueSelectedEventArgs args = new ValueSelectedEventArgs();
args.Value = i;
OnValueSelected(args);
this.Close();
}
Finally in the parent form you have to register for the eventhandler
private void textBox1_Leave(object sender, EventArgs e)
{
lookup myLookup = new lookup();
myLookup.ValueSelected += MyLookup_ValueSelected;
myLookup.Show();
}
private void MyLookup_ValueSelected(object sender, EventArgs e)
{
textBox2.Text = (e as ValueSelectedEventArgs).Value.ToString();
}
I personal like to add dynamically the lookup window, and I do something like this:
//examble object
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
// the value which you want to get from datagridview
private Person _selectedValue;
// the datagridview datasource, which you neet to set
private IEnumerable<Person> _gridDataSource =
new List<Person>()
{
new Person {FirstName="Bob",LastName="Smith" },
new Person {FirstName="Joe",LastName="Doe"}
};
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode== Keys.F3)
{
var btnOk = new Button() { Text = "Ok", Anchor= AnchorStyles.None };
var btnCancel = new Button() { Text = "Cancel",Anchor= AnchorStyles.Right };
var dg = new DataGridView();
var bs = new BindingSource();
bs.DataSource = _gridDataSource;
dg.DataSource = bs;
dg.Dock = DockStyle.Fill;
dg.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//setup a layout wich will nicely fit to the window
var layout = new TableLayoutPanel();
layout.Controls.Add(dg, 0, 0);
layout.SetColumnSpan(dg, 2);
layout.Controls.Add(btnCancel, 0, 1);
layout.Controls.Add(btnOk, 1, 1);
layout.RowStyles.Add(new RowStyle(SizeType.Percent));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
layout.Dock = DockStyle.Fill;
//create a new window and add the cotnrols
var window = new Form();
window.StartPosition = FormStartPosition.CenterScreen;
window.Controls.Add(layout);
// set the ok and cancel buttons of the window
window.AcceptButton = btnOk;
window.CancelButton = btnCancel;
btnOk.Click += (s, ev) => { window.DialogResult = DialogResult.OK; };
btnCancel.Click += (s, ev) => { window.DialogResult = DialogResult.Cancel; };
//here we show the window as a dialog
if (window.ShowDialog() == DialogResult.OK)
{
_selectedValue =(Person) bs.Current;
MessageBox.Show(_selectedValue.FirstName);
}
}
}
what is this error :
Form that was specified to be the MdiParent for this form is not an MdiContainer.
Parameter name: value
here is the code
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void Form1_MdiChildActivate(object sender, EventArgs e)
{
if (this.ActiveMdiChild == null)
tabForms.Visible = false; // If no any child form, hide tabControl
else
{
this.ActiveMdiChild.WindowState = FormWindowState.Maximized; // Child form always maximized
// If child form is new and no has tabPage, create new tabPage
if (this.ActiveMdiChild.Tag == null)
{
// Add a tabPage to tabControl with child form caption
TabPage tp = new TabPage(this.ActiveMdiChild.Text);
tp.Tag = this.ActiveMdiChild;
tp.Parent = tabForms;
tabForms.SelectedTab = tp;
this.ActiveMdiChild.Tag = tp;
this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);
}
if (!tabForms.Visible) tabForms.Visible = true;
}
}
// If child form closed, remove tabPage
private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
{
((sender as Form).Tag as TabPage).Dispose();
}
private void tabForms_SelectedIndexChanged(object sender, EventArgs e)
{
if ((tabForms.SelectedTab != null) && (tabForms.SelectedTab.Tag != null))
(tabForms.SelectedTab.Tag as Form).Select();
}
private void projectsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form7 f7 = new Form7();
f7.MdiParent = this;
f7.Show();
}
}
The problem is probably that in your Form5 class you are not specifying that the Form is a MdiContainer.
Try setting the IsMdiContainer property to true or set the property manualy after you call InitializeComponent in the constructor.