hi,

i've to set some conditions to my program. user has to enter a string of 9 letters + numbers eg. x1234567y OR 12 numbers eg. 111111111111 before hitting the ok button (this ok button in my 1st form will direct me to my 2nd form if i keyed in correctly). the system will do a pop-up box telling u if u had keyed in an invalid no, that is not 9 letters + numbers nor 12 numbers.

can someone teach me to link the forms by adding in the needed codes with some explanation if u can, to my codes given below, in a way as simple as possible?

here's part of my codes for the 1st form:


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace WindowsApplication2

{
///
/// Summary description for MedicalApplication.
///
public class Form1 : System.Windows.Forms.Form
{
.
.
.
.
.
.
private void button8_Click(object sender, System.EventArgs e)
{
<<button8 will link to form 2
}


here's form 2:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication3
{
///
/// Summary description for ToCreatePatientFileInDatabase.
///
public class ToCreatePatientFileInDatabase : System.Windows.Forms.Form
{
.
.
.
.
.
.


Thanks! i really appreciate your help..

Dani AI

Generated

Short summary and fixes based on the thread (posts by , and ): the flow is correct—validate the text box, show an error on invalid input, open the second form on success—but the replies lack a robust validation rule, a safe form-opening pattern and a note about the different namespaces seen in the posted code (WindowsApplication2 vs WindowsApplication3). The validation below enforces either: 1 letter + 7 digits + 1 letter (9 chars total, e.g. x1234567y) OR exactly 12 digits (e.g. 111111111111). It also shows a simple, safe way to open the second form.

Include using System.Text.RegularExpressions; at the top of the file.

private static readonly Regex KeyPattern = new Regex(@"^(?:[A-Za-z]\d{7}[A-Za-z]|\d{12})$");

private void okButton_Click(object sender, EventArgs e)
{
    string input = keyTextBox.Text.Trim();
    if (!KeyPattern.IsMatch(input))
    {
        MessageBox.Show("Invalid key: enter 1 letter + 7 digits + 1 letter (9 chars) or 12 digits.",
                        "Validation error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        keyTextBox.Focus();
        return;
    }

    // If Form2 lives in a different namespace use the full name (or add a using).
    WindowsApplication3.ToCreatePatientFileInDatabase form2 =
        new WindowsApplication3.ToCreatePatientFileInDatabase();

    // Choice: use ShowDialog() for a modal secondary form, or Show() + Hide() to keep the app running.
    form2.Show();
    this.Hide();
}

Practical tips and troubleshooting: wire the click handler to the OK button in the designer (or via +=), set the form's AcceptButton to allow Enter to trigger validation, and consider enabling/disabling the OK button in the TextChanged event using the same KeyPattern for immediate feedback. If forms are in separate projects, add a project reference (Project → Add Reference → Projects) or move the class into the same namespace; otherwise a compile error will occur. Modal (ShowDialog) blocks the caller until the second form closes; non-modal (Show) requires deciding which form owns application lifetime (hiding the main form keeps the message pump alive).

Recommended Answers

All 5 Replies

try this im not sure what your asking but i think you want this

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace WindowsApplication2

{
/// 
/// Summary description for MedicalApplication.
/// 
public class Form1 : System.Windows.Forms.Form
{
.
.
. 
.
.
.
private void button8_Click(object sender, System.EventArgs e)
{
 frmAbout frm = new frmAbout();
            frm.ShowDialog(this);
            frm.Dispose();

// just change the form baout so it reads form 2 then when  you press the button it will take you to form 2
}

here's form 2:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication3
{
/// 
/// Summary description for ToCreatePatientFileInDatabase.
/// 
public 

try this im not sure what your asking but i think you want this

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace WindowsApplication2

{
/// 
/// Summary description for MedicalApplication.
/// 
public class Form1 : System.Windows.Forms.Form
{
.
.
. 
.
.
.
private void button8_Click(object sender, System.EventArgs e)
{
 frmAbout frm = new frmAbout();
            frm.ShowDialog(this);
            frm.Dispose();

// just change the form baout so it reads form 2 then when  you press the button it will take you to form 2
}


here's form 2:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication3
{
/// 
/// Summary description for ToCreatePatientFileInDatabase.
/// 
public 

tyspen READ, :rolleyes: he want to create a event that means it would have to be like this

 private void button8_Click(object sender, System.EventArgs e)
 {
string text = textBox1.Text;

if (text == "//your text//")

{
frmAbout frm = new frmAbout();
             frm.ShowDialog(this);
             frm.Dispose();
 }

else

{
MessageBox.Show("error : The key you typed is incorrect");
}


 // just change the form baout so it reads form 2 then when  you press the button it will take you to form 2
 }

tyspen READ, :rolleyes: he want to create a event that means it would have to be like this

private void button8_Click(object sender, System.EventArgs e)
 {
string text = textBox1.Text;

if (text == "//your text//")

{
frmAbout frm = new frmAbout();
             frm.ShowDialog(this);
             frm.Dispose();
 }

 // just change the form baout so it reads form 2 then when  you press the button it will take you to form 2
 }

My bad....:)

No problem tyspen dont,if you have a idea for project please tell me cause this days i dont have ideas for new projects

EDIT: (sry didnt mean to post this. Mods please delete)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.