Avatar of Imen BELTAIEF
Imen BELTAIEF

asked on 

Hiding/displaying two edit controls by button click

Hello , I want to display two edit controls by clicking on button .
in te first one , the two edit control are hiding . just by clicking a button , those two edit controls will be shown .
Any help please !
C++* MFC

Avatar of undefined
Last Comment
sarabande
SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Imen BELTAIEF
Imen BELTAIEF

ASKER

Thank you for your quick reply . I will try with it .
Another question PLease , In the same dialog box , I want each time , I clicked in button , Some controls displayed .
My scenario for my GUI , is by clicking button"connect" ,two edit controls displayed , then by validating this step , a short menu will be appear in the left of dialog box .By clicking in one menu ,a description and a controls should be displayed in the right of the dialog box .
Is there any method to devise the dialog box into two child dialog box , by pressing in the menu of the dialog box in left , data and controls will be shown in the left of the dialog box.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Imen BELTAIEF
Imen BELTAIEF

ASKER

I want to show the Tree after validating the authentication step by "enter key "click.
First I have created a function "OnActivate"
My code is as bellow :
void CIntAccDlg::OnActivate() // valider l'étape d'authentification pour aller au menu
{
      CWnd* pwndCtrl = GetFocus();
      int ctrl_ID1 = pwndCtrl->GetDlgCtrlID();
      switch (ctrl_ID1) {
      case IDC_PSW:
            UpdateData(TRUE);
            if (m_Login_Compteur == "admin" && m_PSW_Compteur == "admin") // password
            {
                  m_bVisible1 = !m_bVisible1;
                  OnShowHideTreeview();
                  ShowWindow(SW_SHOW);
            }
            UpdateData(FALSE);
      case IDOK: // activer le key board "entrer"
            break;
      }

      EndDialog(0);

}
int CIntAccDlg::OnShowHideTreeview() // pour afficher l'arbre de controle
{
      {
            GetDlgItem(IDC_TreeView)->ShowWindow(!m_bVisible1 ? SW_HIDE : SW_SHOW);
      }
      return 0;
}
 Also  I add a class CTreeView to the Tree Control.


I compile my application  , I enter the two edit controls data but no data(Tree view) is dispalyed after clicking the "enter key "button , however ,the dialog box is exited .
Any help please .
Avatar of sarabande
sarabande
Flag of Luxembourg image

GetDlgItem(IDC_TreeView)->ShowWindow(!m_bVisible1 ? SW_HIDE : SW_SHOW);

don't use member variables like m_bVisible1 for toggling. those members have the bad property that they always have to be initialized rightly, or you would hide the controls if they are already hidden and vice versa.

instead check whether the control is visible or not:

CWnd * pTree = GetDlgItem(IDC_TreeView);
if (pTree)
{
       pTree->ShowWindow(pTree->IsWindowVisible()? SW_HIDE : SW_SHOW);
}

Open in new window


generally, you might better find a design where the controls are enabled/disabled rater than shown/hidden.  that makes the form much better understandable especially if enabling/disabling was triggered by radio buttons or checkbox buttons rather than by push buttons.

Sara
Avatar of Zoppo
Zoppo
Flag of Germany image

Hm - I'm not really sure what you want with OnActivate() here - this function is called when a window get's activated, so i.e. for a top-level window this is called in case the user uses ALT-TAB to toggle between your application and others - see https://msdn.microsoft.com/de-de/library/1f31c334.aspx

I think what you need is to implement a command handler for IDC_PSW (I guess that's the button you mentioned above, if so it can be done from resource editor, right click the buttonm select 'Add event handler ...' from context menu) and to override CDialog::OnOK to handle the ENTER key.

About using a flag or not (as Sara suggested) you can do as you want - I myself usually prefer a flag, at least if there's some more logic behind the scene than just showing/hiding controls except in trivial cases  - i.e. if any other functionality in the dialog needs to know whether the user has pressed a button to show the controls before, it IMO has a 'bad smell' when the only way to find out, that the user activeted them, by again checking if the controls are visible using IsWindowVisible.

But that's a matter of personal preference ...

ZOPPO
Avatar of sarabande
sarabande
Flag of Luxembourg image

OnActivate() here - this function is called when a window get's activated, so i.e. for a top-level window this is called in case the user uses ALT-TAB to toggle between your application and others

actually, there are two messages WM_ACTIVATE and WM_ACTIVATEAPP. the second will be sent when toggling between applications. the first when a top-level window turns from inactive to active what might happen as well when toggling between apps but als can happen with windows of the same application (process).

Sara
Avatar of sarabande
sarabande
Flag of Luxembourg image

About using a flag or not (as Sara suggested) you can do as you want

hmm. as told, member flags have the quirk property to needing always attention. if you prematurely return from a function which would change the flag, you always have to consider whether you have to do the change nevertheless or not. some years later you may not know the exact meaning of the flag and go wrong. or any other will oversee it. so, whenever you have a chance to find out the current status of a control or object without a flag you should choose this as the better alternative. note, this advice only applies for member flags which were changed in different functions and were not bound directly to the window or object. if for example you would store the flag by using SetWindowLong function of WINAPI, you safely could read it again in any function that has access to the window (control). that is similar to calling IsWindowVisible or IsWindowEnabled as i did in my sample code.

Sara
Avatar of Imen BELTAIEF
Imen BELTAIEF

ASKER

I create the function "activate" , I mean that by validating the authentication step , the other controls and view will be activated.It is just a nomination , no more .
Avatar of Imen BELTAIEF
Imen BELTAIEF

ASKER

Hi Mr Zappo ,
For event handler , I don't know  which the right event to add to the edit control "password " to activate this step .

also the code  you give me :

CWnd * pTree = GetDlgItem(IDC_TreeView);
if (pTree)
{
       pTree->ShowWindow(pTree->IsWindowVisible()? SW_HIDE : SW_SHOW);
}
where should I put it !
should I also change the function "OnActivate" to another function , if yes , please any idea about the right function which help me to validate the authentication step and display a tree control .
Avatar of Zoppo
Zoppo
Flag of Germany image

@Sara: >> some years later you may not know the exact meaning of the flag and go wrong. or any other will oversee it

I think that's more likely to happen if you don't use flags in a central function. If you later analyze the code you have to understand what happens in every single function which changes control states, there may be cases where different event handlers change control states of the same controls, so the functional logic is spread over multiple event handlers functions. With meaningful flag-names used in a central function this IMO becomes much easier to understand.

I'm working a lot with large dialogs (up to 50 controls and more, with quite a lot state-dependencies between controls) with none-trivial functional logic, and I'm sure it's much better to implement this via some flags instead of having a codes like i.e. 'if ( my_checkbox->IsVisible() && my_checkbox->IsEnabled() && my_checkbox->IsChecked() { my_textbox->Show() }' scattered in a bunch of event handlers .

For complicated cases (like when change selection in this listcontrol show/hide and/or disable/enable and/or change values of a bunch of controls) IMO flags are better.

When I implement a new dialog the first thing I do is to implement a function (i.e. UpdateControlStates) which I call in DoDataExchange when UpdateData( FALSE ) is called. Then for each control where I have to handle different states (visible/enabled) I add a flag, initialize it to the value I want (this on-the-fly avoids change of behavior in case someone accidentally changes such a state in the dialog resource) and add needed ShowWindow/EnableWindow to set the new control's states according to the flag.

Thus the only thing to take care with this approach is to ensure that each functionality, which changes such a flag, calls either UpdateData( FALSE ) or my UpdateControlStates() explicitly. Everything else is simple and IMO safe, there's only one central function where controls change their states, the flags which controls these states can be set from anywhere without taking care of any dependencies, because these even are handled in that central function.

And I'm used to do the same with simple dialogs, because I want to keep a conistent way of implementing things, I don't like it to implement similar functionalities in completeley different ways. Further sometimes it can happen that a former trivial case becomes a none-trivial case - then it's a lot of effort to change the existing functionality.

But, as told, it's a matter of personal taste :o)
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi Imen Beltaief,

I'm sorry to say this, but I have the impression that you don't have much experience with MFC, right?

It's hard to give you specific help as long as you don't understand the basics of GUI programming with the MFC framework. I think the best thing I can do now is advise you to read or watch some tutorial about this, i.e. here you can find one about GUI controls and how to use Visual Studios Tools/Wizards to create values and eventhandlers for those controls: https://www.tutorialspoint.com/mfc/mfc_controls_management.htm

Next I'm not really understand what you want to acchieve - first you asked about showing two text-boxes when a button is clicked, now it seems it's about showing a tree-control when two string member values are 'admin' - when should this be checked? On another button-click? Or should this be checked on the fly while the user is typing?

And yes, I'd recommend to rename your 'OnActivate', at least because there alreay exists a function with the same name in the base class (CWnd::OnActivate) which is used for a (if I understand the above correctly) completely unrelated purpose (as told above it's about de-/activating a window). I'd suggest to Just give it a name which somehow describes either what the function does (usually without an 'On'-prefix) or, in case of event handlers, the event which is handled (usually with 'On'-Prefix) - and try to avoid using names which are already used in MFC except in case you want i.e. to override a virtual function or for functions which are automatically created by VisualStudio tools/ClassWizard.

ZOPPO

ZOPPO
Avatar of Imen BELTAIEF
Imen BELTAIEF

ASKER

Hi Zappo , Yes I'm new in using MFC , I try with many tutorials and documents ,but it seems a little to statisfy my need ,especialy I'm lack of time.
My scénario for my GUI is as bellow :
In first step ,I will show two edits controls from button click . Those two edits controls are a (login and PSW) .
Second  ,by validating the "authentication" step(fill in the two edit controls) ,  tree view showing my items will be  shown .
I hope you understand my scenario , and thank you again for your help.
SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Imen BELTAIEF
Imen BELTAIEF

ASKER

Hi Zappo ,
Thank your for your help ,
For the last code , after typing the login and PSW correctly ,directly the tree is shown without clicking "enter Key" .
 Try to add  "IDOK" to the condition :
   if (m_Login_Compteur == "admin" && m_PSW_Compteur == "admin"  &&  IDOK)
But it keeps the same state .
Avatar of Zoppo
Zoppo
Flag of Germany image

hm - this was how I understood 'Second  ,by validating the "authentication" step(fill in the two edit controls) ,  tree view showing my items will be  shown ', and I wasn't aware there's an "enter Key".

But it's easy to solve this: Add a 'BN_CLICKED' handler for the "enter Key", in it do the same as you did in the EN_CHANGE-handlers and remove the EN_CHANGE handlers.

ZOPPO

PS: It's not important, but it's really ZOPPO, not Zappo :o)
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Imen BELTAIEF

ASKER

Hi Sara , Yeah I use tree control (class CTreeCtrl) : my code is runned with success , I can display the tree since validating the authentication step .
For my code , I create the TreeCtrl using the "OnInitDialog()".
Then I call it using OnShowHideTreeview() function :
int CIntAccDlg::OnShowHideTreeview() // pour afficher l'arbre de controle
{
      CWnd * pTree = GetDlgItem(IDC_TREE1); // pointeur vers l'arbre de controle
      if (pTree)
      {
            pTree->ShowWindow(pTree->IsWindowVisible() ? SW_HIDE : SW_SHOW);
      }
      return 0;
}

Now ,I have a difficulty how to manage my User interface . attached is my GUI .
As it showed , there are a menu in the left of the dialog box ,then in left there are an area to display data.
I want to display data related to each menu just by clicking in the menu , for example , when I click in "Metrologie Legale" , the date related to this menu are displayed in the right of interface .
Which method  should I use to do this scenerio . For data , should I create a FormView ,  then call it in the main interface.
To devise my interface , I use two GroupBox , is it efficient ,or there are another method like dockable panel.
GUI.png
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of sarabande
sarabande
Flag of Luxembourg image

t was not quite easy to decide whether to recommend for deletion or closing the question to be PAQ'ed. As Imen in the meantime asked a new question for the same task, it is obvious that the question here was not fully solved. Nevertheless I chose an answer of Zoppo for the solution which would be a good base for to solve the main requirement of the question: how can we dynamically embed a child dialog after the user clicked to a specific tree item. Perhaps Imen will return to the question and check the 'accepted' answer which might help her going on regardless whether the tree control was then used in a dialog of a dialog-based MFC application or in a form view of an SDI application.

Sara
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo