Click here to Skip to main content
15,745,746 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
See more:
i have a dialogbox that has only text box and receive only
integer i have done all validation except the Following ...
when user press Enter it should Check whether Textbox is empty or not...
if not then do something otherwise this.close();
my below code doesn't work when user Press a Key the dialogueBOx Close() it self
C#
if (Convert.ToInt32(e.KeyChar) == 13 && txtsearhkey.Text.Length>0)
            {
                frmviewpemaish vp = new frmviewpemaish();
                vp.psearchkey = psearch;
                vp.MdiParent = this.ParentForm;
                
                vp.Show();
                vp.loadclient(txtsearhkey.Text);
                this.Close();
               }
                else
                 { 
                  this.Close(); 
                 }
               }
Posted
Updated 10-Feb-14 6:13am
v2
Comments
joshrduncan2012 6-Feb-14 13:10pm    
Why doesn't it work? What error are you seeing?
abdul manan 7 6-Feb-14 13:14pm    
when i press a key(any key) the dialog box close()

Enter is not character; and there is no such characters. And this is always a bad idea to convert characters to integer; remember, this is Unicode. And keys are generally not character, they are handled before characters are created. To handle key hits, you need to handle events like KeyDown or KeyUp. For example:
C#
this.KeyDown += (sender, eventArgs) => {
    if (eventArgs.KeyCode == Keys.Enter)
        DoSomething();
    //...
}


Another bad idea is using MDI, ever. Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA
 
Share this answer
 
Comments
abdul manan 7 6-Feb-14 13:27pm    
thanks aloti hate MDI but Friend told me to use it ...it group every thing well
please write the code again i didn't get
Sergey Alexandrovich Kryukov 6-Feb-14 17:43pm    
Why would we need enemies if we have such friends? :-)
If you want to avoid trouble and get hated by the users, you won't ever use MDI. Even Microsoft works hard to phase it out.
No, who needs to write code again? What didn't you get? I answered your question on the key in full. Any questions?
—SA
abdul manan 7 8-Feb-14 0:26am    
i learn alot about the MDI
good links u sent
thank you Sir
Sergey Alexandrovich Kryukov 8-Feb-14 1:35am    
You are very welcome.
Good luck, call again.
—SA
i solved my self Thanks for Replay
C#
if (Convert.ToInt32(e.KeyChar) == 13)
            {
                if (txtsearhkey.Text.Length > 0)
                {
                    frmviewpemaish vp = new frmviewpemaish();
                    vp.psearchkey = psearch;
                    vp.MdiParent = this.ParentForm;

                    vp.Show();
                    vp.loadclient(txtsearhkey.Text);
                    this.Close();
                }
                else
                {
                    this.Close();
                }
 
Share this answer
 
v2
Comments
BillWoodruff 7-Feb-14 3:55am    
How are you making sure your TextBox only has integers ? If you want to see a different way of handling what you describe here, just ask, and I'll post some code I use.
abdul manan 7 8-Feb-14 0:25am    
yes tell me a different way that's y i posted here
i am new to C#
BillWoodruff 9-Feb-14 9:34am    
Before I post some code, let me ask you if you want to allow the minus-sign and decimal point in what is entered in the TextBox.
As promised to OP: some sample code (adapted from code in actual use) to create a UserControl with a TextBox that accepts only integers, a minus-sign as the first character, and that auto-hides when the 'Enter Key is pressed:
C#
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class ucIntegerOnly : UserControl
    {
        public ucIntegersOnly()
        {
            InitializeComponent();
        }

        // only this nullable Int64 property is exposed
        public Int64? EnteredValue { private set; get; }

        private Int32 maxInt64Length = Int64.MaxValue.ToString().Length;

        private void UserControl1_Load(object sender, EventArgs e)
        {
            EnteredValue = null;
            tbxIntegerEntry.Clear();
        }

        private void tbxIntegerEntry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string mTxt = tbxIntegerEntry.Text;

                EnteredValue = null;

                // don't evaluate if Text is empty or only minus sign
                if (mTxt != String.Empty && mTxt != "-")
                {
                    EnteredValue = Convert.ToInt64(mTxt);
                    this.Hide();
                }
            }

            e.SuppressKeyPress =
            !(
                // backspace allowed
                e.KeyCode == Keys.Back
                ||
                // must not exceed length limit
                tbxIntegerEntry.Text.Length <= maxInt64Length
                &&
                // digits allowed
                char.IsDigit(Convert.ToChar(e.KeyCode))
                ||
                // minus sign allowed at position #0
                (tbxIntegerEntry.Text.Length == 0) && (e.KeyCode == Keys.OemMinus)
            );
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900