I have 2 forms, Main Form & ModalBox.
In Main Form I have a button that when clicked draws a shape depending on the selected index of the comboBox.
// Create Rectangle or Ellipse
public void buttonAdd_Click(object sender, EventArgs e)
{
SolidBrush sb = new SolidBrush(Color.Red);
Graphics g = panel1.CreateGraphics();
if (shapeSizeControl1.comboBoxShapeSelection.SelectedIndex == 0)
{
g.FillRectangle(sb, Convert.ToSingle(shape.X), Convert.ToSingle(shape.Y), Convert.ToSingle(shape.Width), Convert.ToSingle(shape.Height));
}
else if (shapeSizeControl1.comboBoxShapeSelection.SelectedIndex == 1)
{
g.FillEllipse(sb, Convert.ToSingle(shape.X), Convert.ToSingle(shape.Y), Convert.ToSingle(shape.Width), Convert.ToSingle(shape.Height));
}
}
In the ModalBox form I have an Ok button which should do the same thing as the button in Main Form but it doesn't because I have no idea how to program it to do so.
What i've tried..
Copying the same code from Main Form buttonAdd_Click into ModalBox buttonOk_Click. Bad idea because I have to instantiate a new Main form to get the panel1 variable. If I do that nothing happens. Why? I'm not sure an explanation would be great.
Creating a custom event so when the ModalBox Form opens, once I press the ok button since its subscribed It will draw the shape. 1 problem I have no idea where to call the event and check for null because the button is on the second form so If I call the custom event in the first form I have no where to call the event and do null check.
Goal
My goal is to figure out how to add shapes in the Main Form from the ModalBox Form using the Ok button in the ModalBox Form.
Just create a method DrawShape() in the main form that draws your shapes and call it from the button click.
// Main Form
private void button1_Click(object sender, EventArgs e)
{
DrawShape();
}
Now create the modal form and set the owner
// Main form
private void button2_Click(object sender, EventArgs e)
{
var dlg = new ModalBox();
if (dlg.ShowDialog(this)==DialogResult.OK)
{
// do things
}
}
Finally in the modal form call the DrawShape() function
// Modal form
private void button1_Click(object sender, EventArgs e)
{
var parent = this.Owner as MainForm;
parent.DrawShape();
}
Related
I want to know if there is any event when you click on the rest of the screen and not the Windows Form the Form closes. Is it because of the ShowDialog()?
My Main Form is just with a notifyIcon and when I click it I call Form1.ShowDialog();
Main.cs:
private void ShowForm1(object sender, EventArgs e)
{
form1.Left = Cursor.Position.X;
form1.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form1.Height;
form1.ShowDialog();
}
Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Test";
}
You need to run the dialog box non-modally, not modally. Think about it, when you run it modally, the dialog box takes over the UI and plays games with mouse-clicks elsewhere, preventing you from running. You don't want it to be modal anyway.
I created a simple Windows form with a button that includes this handler to open a small AutoCloseDialog form I created (and populated with a few controls):
private void button1_Click(object sender, EventArgs e)
{
var dlg = new AutoCloseDialog();
dlg.Show(this); //NOTE: Show(), not ShowDialog()
}
Then, in the AutoCloseDialog form, I wired up the Deactivate event. I did it in the designer (where this code is generated):
this.Deactivate += new System.EventHandler(this.AutoCloseDialog_Deactivate);
Finally, here is the handler for the Deactivate event.
private void AutoCloseDialog_Deactivate(object sender, EventArgs e)
{
this.Close();
}
I think this does what you are asking.
Yes, you can create a similar function like this, which closes the Form if the Form lost focus (in Form1.cs)
public void Form_LostFocus(object sender, EventArgs e)
{
Close();
}
and then you add the LoseFocus EventHandler (in Form1.Designer.cs):
this.LostFocus += new EventHandler(Form_LostFocus);
I am stuck on using a PerformClick method. I have a main form which is called mymainform, and I have some subforms. When loading main form, I am creating subforms and hide them with Form Visibility and access some elements on subforms.
My problem is clicking a button on subform1 from MainForm. I have written the below code and it didn't work. Normally DayModeButton changes the boolen; isDay = !isDay
After clicking the button1 on mainform it doesn't change the isDay boolen.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
}
If I write this code it works, but I don't want to show and hide the form because it is not a good view for users.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.Visible=true;
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
mysubform1.Visible=false;
}
Can anyone help me in performing a Click event in invisible forms?
Thanks
Make sure your DayModeButton is Public and not Private.
I have a form. If someone presses a button, I want to show a second form "attached" to the original form, meaning that its left side is at the right side of the original form and they have the same height. In other words: they touch each other.
An answer seems to be Open Form next to Parent Form
However, there is a gap between the images. I want them to be exactly next to each other
main form:
private void ShowOtherForm()
{
using (var form = new OtherForm())
{
var dlgResult = form.ShowDialog(this);
ProcessDlgResult(dlgResult);
}
}
Other form, event handler Load
private void FormLoad(object sender, EventArgs e)
{
// show this form attached to the right side of my owner:
this.Location = new Point(this.Owner.Right, this.Owner.Top);
this.Height = this.Owner.Height;
}
Try to use ClientSize and Location
private void Form2_Load(object sender, EventArgs e)
{
var owner = this.Owner;
Location = new Point(owner.Location.X + owner.ClientSize.Width, owner.Location.Y);
Height = owner.Height;
}
I'm making like a add contact form with two buttons, ADD CONTACT and EDIT CONTACT. When a user clicks add contact, it pops up another form where the user can add contact information.
I want to give them the option to edit that info by clicking the EDIT CONTACT button which should pop up the SAME form.
However its not letting me call the object of the form twice, saying that I cannot press the edit button after the add button.
How do I call a form object twice?
//instatiating an object of the form
FormContact contactForm = new FormContact();
public FormManager()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
//displaying it when the user clicks add button
contactForm.Show();
}
private void btnEdit_Click(object sender, EventArgs e)
{
//trying to display it again but gives this exception
///System.ObjectDisposedException: 'Cannot access a disposed
///object.
///Object name: 'FormContact'.'
contactForm.Show();
}
Error:
///System.ObjectDisposedException: 'Cannot access a disposed
///object.
///Object name: 'FormContact'.'
The problem is you're closing the form after you've shown it.
You click the Show button
Your only instance of the form is shown
You close it with the X in the top corner
The runtime destroys the form (disposes it) after it is closed
You try to show it again but this this it's gone, trashed, doesn't exist any more, waiting to be garbage collected
Either make a new form each time you open it (to reduce code clutter, assign this same event handler to both button clicks, or copy paste it out twice if you want them to be coded differently eventually):
private void btnAddOrEdit_Click(object sender, EventArgs e)
{
new FormContact().Show();
}
Or intercept the FormClosing event of the FormContact form and cancel the closing, and perform a Hide() instead so that instead of being destroyed your form is made invisible. It will then still exist and can be Show()n the next time. To handle the event, open the FormContact designer, click anywhere in the form background, click lightning bolt in properties grid, double click the FormClosing entry:
private void FormClosing(object sender, FormClosingEventArgs e){
e.Cancel = true;
this.Hide();
}
Which method you choose depends how you want your program to behave:
If you make a new form each time, and you Show instead of ShowDialog your user could click Add twice and see two forms. Then could click Add 10 times and see 10 forms. Using ShowDialog means that the main window won't accept any more clicks until the FormContact is closed. You might or might not want this either
If you Hide (not close; hiding is different from closing) and Show the same form rather than making a new one then the user can click Add 10 times but they still only see one form
FormContact contactForm = new FormContact(); is a Member variable, it's scope is Private and is visible to the entire class.
After you first Show the form:
contactForm.Show();
The form is already showing. Therefore if you call Show again, it won't do anything as the instance of the form/class is already showing.
If you want to show two instances of the form, you will need to instantiate two instances eg:
private void btnAdd_Click(object sender, EventArgs e)
{
FormContact contactForm = new FormContact();
contactForm.Show();
}
private void btnEdit_Click(object sender, EventArgs e)
{
FormContact contactForm = new FormContact();
contactForm.Show();
}
Or make two instances of it:
FormContact contactForm1 = new FormContact();
FormContact contactForm2 = new FormContact();
private void btnAdd_Click(object sender, EventArgs e)
{
contactForm1.Show();
}
private void btnEdit_Click(object sender, EventArgs e)
{
contactForm2.Show();
}
Or add a argument in the parameter of the Constructor to indicate Add or Edit, eg:
public class FormContact
{
public FormContact(int id)
{
if (id > 0)
{
//Load contact for Editing
}
else
{
//Clear all fields for Adding
foreach(var ctrl in this.Controls)
{
if (ctrl Is TextBoxBase) ctrl.Text = string.Empty
//TODO other controls types... if (ctrl Is ....
}
}
}
}
Then you can call it passing a contactID to edit or 0 to add:
FormContact contactForm = new FormContact(contactID);
contactForm.Show();
I've got another answer, in both methods simply show your form modally:
contactForm.ShowModal();
I have a Split Container that have Panel 1 (left, with menu) and Panel 2 (right, where I load the child forms), in the menu I have a button that opens a new child form. It only allows one instance of the window at a time, not duplicates.
The last form that I open always stays in front of the Panel 2, even when the focus is on the form in back.
How do I bring the back from to front when I focus a control in it?
Function that open the form
private void AddFormInPanel(object formChild)
{
Form fh = formChild as Form;
Form existe = Application.OpenForms.OfType<Form>().Where(pre => pre.Name == fh.Name.ToString()).SingleOrDefault<Form>();
if (existe == null)
{
fh.TopLevel = false;
this.splitContainer.Panel2.Controls.Add(fh);
this.splitContainer.Panel2.Tag = fh;
fh.BringToFront();
fh.Show();
}
}
Call to the function
private void buttonForm1_Click(object sender, EventArgs e)
{
AddFormInPanel(new Form1());
}
private void buttonForm2_Click(object sender, EventArgs e)
{
AddFormInPanel(new Form2());
}
In this example, i Have focused the textbox Name, but the form still in back.
Example: https://imgur.com/6jsWIgi