Focus on field if Cancel on child form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a child form frmDataEntry call up another child form frmDealerSearch.
If the user clicks on cancel on frmDealerSearch, I want to close
frmDealerSearch and put the focus on txtDealerNum on frmDataEntry.

Here is my code.

public class frmDataEntry : System.Windows.Forms.Form
{
private void txtDealerNum_Leave(object sender, System.EventArgs e)
{
frmDealerSearch f = new frmDealerSearch();
f.ShowDialog();
}

}


public class frmDealerSearch : System.Windows.Forms.Form
{
private void cmdCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
 
Use DialogResult -

public class frmDataEntry : System.Windows.Forms.Form
{
private void txtDealerNum_Leave(object sender, System.EventArgs e)
{
frmDealerSearch f = new frmDealerSearch();
if (f.ShowDialog() == DialogResult.Cancel)
{
//set focus here
}
}

}


public class frmDealerSearch : System.Windows.Forms.Form
{
private void cmdCancel_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
 
On frmDealerSearch, make sure that the CancelButton property is set to the
forms Cancel Button. In addition, set the DialogResult property of this
button to Cancel. Also, you remove the this.Close() from cmdCancel_Click as
it will occur automatically.

Next, you’d want to be aware of the return value from f.ShowDialog(), and
based on it act to set the focus to txtDealerNum, much like:

if( f.ShowDialog() == DialogResult.Cancel )
{
txtDealerNum.Focus();
}

Brendan
 
Doesn't work, f.ShowDialog() comes back as <overload> and DialogResult.Cancel
comes back as None.
 
Doesn't work, f.ShowDialog() comes back as <overload> and DialogResult.Cancel
comes back as None.
 
Mike L said:
Doesn't work, f.ShowDialog() comes back as <overload> and
DialogResult.Cancel
comes back as None.

I'm not sure exactly what you mean by that.
Show the code you are using now, just so we can make it is what we intended.
 
I did have the DialogResult property of my cancel button set to "Cancel".

Here is the code.

namespace LicenseDealerSales
{
/// <summary>
/// Summary description for frmDataEntry.
/// </summary>
public class frmDataEntry : System.Windows.Forms.Form
{
private void txtDealerNum_Leave(object sender,
System.EventArgs e)
{
if (txtDealerNum.Text.Length != 6)
{
MessageBox.Show("Dealer Number requires 6
digits.", "Invaild Data",MessageBoxButtons.OK);
txtDealerNum.Focus();
}
else
{

frmDealerSearch f = new frmDealerSearch();
if( f.ShowDialog() == DialogResult.Cancel )
{
txtDealerNum.Focus();
}
}
}
}
}


namespace LicenseDealerSales
{
/// <summary>
/// Summary description for frmDealerSearch.
/// </summary>
public class frmDealerSearch : System.Windows.Forms.Form
{
private void cmdCancel_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;

}
}
}
 
I did have the DialogResult property of my cancel button set to "Cancel".

Here is the code.

namespace LicenseDealerSales
{
/// <summary>
/// Summary description for frmDataEntry.
/// </summary>
public class frmDataEntry : System.Windows.Forms.Form
{
private void txtDealerNum_Leave(object sender,
System.EventArgs e)
{
if (txtDealerNum.Text.Length != 6)
{
MessageBox.Show("Dealer Number requires 6
digits.", "Invaild Data",MessageBoxButtons.OK);
txtDealerNum.Focus();
}
else
{

frmDealerSearch f = new frmDealerSearch();
if( f.ShowDialog() == DialogResult.Cancel )
{
txtDealerNum.Focus();
}
}
}
}
}


namespace LicenseDealerSales
{
/// <summary>
/// Summary description for frmDealerSearch.
/// </summary>
public class frmDealerSearch : System.Windows.Forms.Form
{
private void cmdCancel_Click(object sender, System.EventArgs e)
{
this.DialogResult = DialogResult.Cancel;

}
}
}
 
Hi,

I tried your code on my machine and it works fine. Can you put these code
into a new project to see if it works? Also you can put a breakpoint on
line txtDealerNum.Focus(); to see if dialog result is Cancel.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
I figured it out. I had to add the code this.DialogResult = DialogResult.OK;
to the OK button.
 
I figured it out. I had to add the code this.DialogResult = DialogResult.OK;
to the OK button.
 
Nice to hear that you have got the issue resolved.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top