Controlling the main form

Hi,

Can anybody tell me how to control the main form. I want to make the main form appear depending on a check that i am going to make. For example when running the exe, i need to check first if the username and password is already present in the Registry. If it is present then i wont show the login. If the login is not correct, i shouldn't get the main form but the login again.In which event of the main form should i do this check so as to make the main form appear depending on the result of this check?

Comments

  • Hi Rowin,
    sorry for replying so late. That's why I post a quick-and-dirty way:


    Add the bool member variable [b]passwordGiven[/b] to TForm1, then:

    [code]
    __fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)[b], passwordGiven(false)[/b]
    {

    }
    //---------------------------------------------------------------------------
    [b]void __fastcall TForm1::FormPaint(TObject *Sender)
    {
    if (passwordGiven==false)
    {
    while(1)
    {
    Application->HandleMessage();
    this->Visible = false;
    Form2->Visible = true;
    if (Form2->MaskEdit1->Text=="Password") break;
    }
    passwordGiven = true;
    this->Visible = true;
    Form2->Visible = false;

    return;
    }

    }[/b]
    [/code]

    There will be more elegant solutions if you make a hidden Form called FormControl, that dynamically creates Form1 and Form2.

    See ya,
    bilderbikkel

  • : Hi Rowin,
    : sorry for replying so late. That's why I post a quick-and-dirty way:
    :
    :
    : Add the bool member variable [b]passwordGiven[/b] to TForm1, then:
    :
    : [code]
    : __fastcall TForm1::TForm1(TComponent* Owner)
    : : TForm(Owner)[b], passwordGiven(false)[/b]
    : {
    :
    : }
    : //---------------------------------------------------------------------------
    : [b]void __fastcall TForm1::FormPaint(TObject *Sender)
    : {
    : if (passwordGiven==false)
    : {
    : while(1)
    : {
    : Application->HandleMessage();
    : this->Visible = false;
    : Form2->Visible = true;
    : if (Form2->MaskEdit1->Text=="Password") break;
    : }
    : passwordGiven = true;
    : this->Visible = true;
    : Form2->Visible = false;
    :
    : return;
    : }
    :
    : }[/b]
    : [/code]
    :
    : There will be more elegant solutions if you make a hidden Form called FormControl, that dynamically creates Form1 and Form2.
    :
    : See ya,
    : bilderbikkel
    :
    :
    Hi,
    thanks for the help.
    Actually I have done it in another way.
    Do you want me to post it?

    Rowin

  • : Hi,
    : thanks for the help.
    : Actually I have done it in another way.
    : Do you want me to post it?
    Sure! Might be a more elegant solution.

    See ya,
    bilderbikkel

  • : : Hi,
    : : thanks for the help.
    : : Actually I have done it in another way.
    : : Do you want me to post it?
    : Sure! Might be a more elegant solution.
    :
    : See ya,
    : bilderbikkel
    :
    :
    In the project.cpp,
    Application->ShowMainForm=false;
    MainForm->start();
    Application->Run();

    start() is a function that i have added in my main.
    Now in the start():

    if(Application->ShowMainForm==false) {

    AnsiString temp1;
    AnsiString temp2;
    TRegistry *Reg = new TRegistry();
    Reg->RootKey = HKEY_LOCAL_MACHINE;
    if(Reg->KeyExists("SOFTWARE\Rowin"))
    {

    if(Reg->OpenKey("SOFTWARE\Rowin",FALSE))
    {
    temp1=Reg->ReadString("UserName");
    temp2=Reg->ReadString("Password");
    DataModule1->Database1->Params->Values["User Name"]=temp1;
    DataModule1->Database1->Params->Values["Password"]=temp2;
    Reg->CloseKey();
    LoginPrompt->Edit1->Text=temp2;
    LoginPrompt->Edit2->Text=temp1;
    // Application->ShowMainForm=true;
    try{
    OKBottomDlgSearch->DBLookupComboBoxDeveloper->ListSource->DataSet->Active=true;
    }
    catch(...)
    {
    //ShowMessage("Connection could not be made to the server. Please enter your credentials" );
    //MessageBox("Connection could not be made to the server. Please enter your credentials",MB_OK | MB_ICONWARNING);
    MessageDlg("Connection could not be made to the server. Please enter your credentials",mtWarning,TMsgDlgButtons() << mbOK, 0);
    LoginPrompt->Edit1->Text="";
    LoginPrompt->Edit2->Text="";
    goto L1;

    }
    }
    else
    {
    ShowMessage("Can not open program serial key .");
    Reg->CloseKey();
    }
    }
    else
    {
    //ShowMessage("Connection could not be made to the server. Please enter your credentials");
    MessageDlg("Connection could not be made to the server. Please enter your credentials",mtWarning,TMsgDlgButtons() << mbOK, 0);
    L1: if(LoginPrompt->ShowModal()==mrOk)
    {
    LoginPrompt->Edit2->SetFocus();
    DataModule1->Database1->Params->Values["User Name"]=LoginPrompt->Edit2->Text;
    DataModule1->Database1->Params->Values["Password"]=LoginPrompt->Edit1->Text;

    try{
    OKBottomDlgSearch->DBLookupComboBoxDeveloper->ListSource->DataSet->Active=true;
    }
    catch(...)
    {
    //ShowMessage("Unknown user name or password. Login failed for user " +LoginPrompt->Edit2->Text);
    MessageDlg("Unknown user name or password. Login failed for user " +LoginPrompt->Edit2->Text,mtError,TMsgDlgButtons() << mbOK, 0);
    LoginPrompt->Edit1->Text="";
    LoginPrompt->Edit2->Text="";
    goto L1;

    }

    }
    else
    {
    Application->Terminate();
    }
    delete Reg;

    }
    if(DataModule1->Database1->Connected==true)
    {
    Application->ShowMainForm=true;
    AnsiString gSerial1;
    AnsiString gSerial2;
    gSerial1=LoginPrompt->Edit1->Text;
    gSerial2=LoginPrompt->Edit2->Text;
    TRegistry *Reg = new TRegistry();
    AnsiString temp;

    Reg->RootKey = HKEY_LOCAL_MACHINE;

    if((!Reg->KeyExists("SOFTWARE\Rowin")) || (Reg->KeyExists("SOFTWARE\Rowin")))
    {
    if(!Reg->CreateKey("SOFTWARE\Rowin"))
    {
    Application->MessageBox
    ("Can not Make Serial Key","Error",MB_OK);
    return;
    }


    if(Reg->OpenKey("SOFTWARE\Rowin",FALSE))
    {
    Reg->WriteString("UserName",gSerial2);
    Reg->WriteString("Password",gSerial1);
    Reg->CloseKey();
    }
    else ShowMessage("Registry RootDir error");
    Reg->CloseKey();

    }
    delete Reg;
    }

    }

    Do you understand everything?
    Rowin
  • : Do you understand everything?
    : Rowin
    :
    Sure! Thanks for the post Rowin!

    See ya,

    bilderbikkel

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories