开发者

Set Cancel button for POPup window!

开发者 https://www.devze.com 2023-03-18 10:05 出处:网络
开发者_StackOverflow社区I am implementing a PopUp window which takes inputs.So it contains an OK button(submit button) and also cancel button.So,when i press the OK button it takes input values,if i p

开发者_StackOverflow社区I am implementing a PopUp window which takes inputs.So it contains an OK button(submit button) and also cancel button.So,when i press the OK button it takes input values,if i press the cancel button it should not take input but should make a value decrease by 1.I will explain you clearly why so. here is the code of popup form.

public partial class PopUp : Form {

    public PopUp()
    {
        InitializeComponent();
    }

    private void OK_Click(object sender, EventArgs e)
    {
        ((Scrollbar.Form1)this.Owner).OK_Click(textBox1.Text,textBox2.Text,textBox3.Text,textBox4.Text);
        this.Close();
    }

    private void Cancel_Click(object sender, EventArgs e)
    {

        ((Scrollbar.Form1)this.Owner).Cancel_Click();
        this.Close();
    }
}

and the OK_click and cancel_click functions in my main form.

public void OK_Click(string tbox1, string tbox2, string tbox3, string tbox4)
{
    g[b] = Int32.Parse(tbox1);
    h[b] = Int32.Parse(tbox2);
    i[b] = Int32.Parse(tbox3);
    j[b] = Int32.Parse(tbox4);
    b++;
}

public void Cancel_Click()
{

}


Form ChildForm = null;
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (a < 2)
    {
        ChildForm = new PopUp();
        ChildForm.ShowDialog(this); 
        l[a] = e.X;
        m[a] = e.Y;
        a++;
    }

}

the Mouseup event should occur only two times,every time i press the OK button the value of 'a' increments by one ,so when i press the cancel button even though 'a' increments by one ,in that case i can enter the values only once.so when i press the cancel button value of 'a' should decrease by one . please propose me way how i can implement the mouse up event only two times even after clicking the cancel button.thanks in Advance.!


You are implementing the popup dialog all wrong. The base Form object has a DialogResult property. You should use that to determine your logic in your MainForm.

First, MyPopup:

// Set properties on your buttons either in VS Designer or programmatically
this.button1.DialogResult = System.Windows.Forms.DialogResult.OK;
// other properties

this.button2.DialogResult = System.Windows.Forms.DialogResult.Cancel;
// other properties

Then, in your MainForm:

// Display popup expecting a DialogResult.OK or DialogResult.Cancel
void ShowPopup ( )
{
    MyPopup popup = new MyPopup ( );

    if (popup.ShowDialog() == DialogResult.OK)
    {
        //  process popup textbox text values
    }
    else
    {
        //  process popup cancel action
    }
}


Your popup window is derived from Form, as much as I see. The simpliest solutionto prevent the form from closing on CANCEL click first time and to make something else, if I understand right your problem, is to override Form's OnClosing event, like this:

    protected override void OnClosing(CancelEventArgs e)
    {
        // some decisional logic here 
        if(..condition...) 
            e.Cancel =true; //this will prevent the form from closing
        base.OnClosing(e);
    }

Do not invent double events or somethign like that as much as it possible, as this is the first pass to mess.

Use "natural" possibilities of framework you use. I think you will need to rearrange a little bit your code for support this.

If this is not what you were asking for, please explain better. Regards.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号