Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, okay so i have this layout/ a button and 2 textfields, my plan is when both text fields are empty and you click the button a error message opens up, my problem is that when i click the button i get the message , when i click ok the next form still opens. my code"
if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text)))
                MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            Files mf = new Files();
            Hide();
            mf.ShowDialog();
            Close(); 

thanks,
Posted
Updated 16-Jul-15 23:00pm
v2

//reffer below code......
C#
private void btn_Click(object sender, EventArgs e)
        {
            Files mf = new Files(); //form object
            if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text)))
            {
                MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                mf.ShowDialog(); //show dialog
            }
        }
 
Share this answer
 
Comments
jamesmc1535 17-Jul-15 5:02am    
aah thanks man
[no name] 17-Jul-15 5:04am    
you are most welcome please mark answer as solution..!
:)
jamesmc1535 17-Jul-15 6:06am    
sorry uhm i used that code it works perfect but ive seen now that it diesnt close the 1st form, how do i make it close the form when i click the button?
Simple - add an else clause:
C#
if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text)))
    MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK, 
else
    {
    MessageBoxIcon.Warning);
    Files mf = new Files();
    Hide();
    mf.ShowDialog();
    Close(); 
    }
BTW: it's a good idea (especially when you are just starting out) to always add the curly brackets, even if it's just one line:
C#
if (string.IsNullOrEmpty(txtPassword.Text) || (string.IsNullOrEmpty(textbox8.Text)))
    {
    MessageBox.Show("field empty.", "Error", MessageBoxButtons.OK,
    }
else
    {
    MessageBoxIcon.Warning);
    Files mf = new Files();
    Hide();
    mf.ShowDialog();
    Close();
    }
That way, it's immediately obvious what is expected to happen.
And please, do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
Comments
jamesmc1535 17-Jul-15 5:04am    
thanks man . ill keep that in mind. i did change the name though but i changed it back,dont ask me why XD

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