Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping the values when a Dialog is closed and opened

Status
Not open for further replies.

LuisEnrique

Programmer
Nov 15, 2001
10
0
0
US
Hello everybody:
I have a Dialog with a combo box. I supposed to pick a value from the combo box and then when I close the dialog and open the same dialog again, the value that I was selected before closing the dialog is gone. Is there a way of keeping those values when opening and closing a dialog? Any answer is more than welcome.

Thanks and have a good day,

Luis E. Cuadrado
:0)

 
If the dialog is being called fom another form or main application you could set a public integer variable in the caller form and when the dialog is being closed you could set the varaible to the value of the combos index by including the header file of the caller form in the open/close dialog cpp file and setting the variable in the OnClose message of the window
.Then showwindow message of the open/close dialog you couls use the SetCurSel property of the combo to display the current value.

You would also need to add 2 variable to the combo box by right clicking the combo. One would be a control varaible and one would be the value variable of type int.

e.g

CallerForm = CCaller
DialogForm = COpenClose
Combo Control Variable = m_cmbMain
Combo Value Variable = m_cmbMainVal

CCaller.h file
public:
int iComboVal

COpenClose.cpp File

#include CCaller.h

COpenClose::ShowWindow
{
CCaller* frmCaller;
m_cmbMain.SetCurSel(frmCaller->iComboVal);
}

COpenClose::OnClose
{
CCaller* frmCaller;
frmCaller->iComboVal = m_cmbMainVal;
}


Hope This helps !!

[ponytails]
 
A few other options to look into could be

1. Make a registry setting to read in and out from
2. Use setProfileInt and GetProfileInt (int can be replaced with string, etc) and depending on OS this will either write a .ini file or a registry setting
3. Keep static variables in the class and set those on close and read from em' on open. If they are empty, select from the top of the list.


The static approach should be fine if every time you load ther app you dont want to remember what they did the last time. If you want to exit the app completely and remember it on subsequent runs then use option 1 or 2. If you have little experience with the registry, I would suggest option 2 with the Profile api calls.

Matt
 
Well, I think what I want is the form to remember the values selected in the combo box. So looks like I need to look more about registry stuff. Honestly, I'm not quite familiar with the registry. Is there anything resource that I can find online that I can read about reading values from the registry? Ambles and Zyrenthian thank you very much for your help. I really appreciate it.

Best regards,
 
If you get really stuck try using global variables to store the values.

In the .cpp file that calls the dialog, add to the top (before all the functions) something like:

int globalIntValue; // the 'int' is just an example
// as the variable can be of any type
// - even your own structure

In the .cpp file for the dialog, add (above all functions) the 'extern' version of the variable declaration:

extern int globalIntValue;

The 'extern' declaration in another .cpp code file means that the global variable has already been declared in another .cpp file but you'd like access to it in this particular .cpp file too.

Although using globals kind of defeats the whole object of using C++ to program with, it would at least get your code up and running and your dialog will always have access to the global varaible (ie. reading it's value for example to initialize something in the dialog before it is shown and, also by setting it when the user clicks the OK button.

eg:

OnInitDialog()
{
// initialize something in your dialog using the
// global varaible before the dialog is shown

m_MyDialogsComboBox.SetCurSel(globalIntValue);
}

OnOK()
{
// The user clicked 'OK' so let's get the value
// of our combo box and set globalIntValue to
// the selection value

globalIntValue = m_MyDialogsComboBox.GetCurrSel();
}

// Because the integer is declared 'globally', it's
// value is accessible to any .cpp file, anywhere in the
// program so long as the appropriate declarations are
// made in the .cpp files that need access to it

There's also other methods you can use but I just figured you would want something easy to understand and get you up and running straight away.
 
Go with SetProfileXXX and GetProfileXXX where XXX is a string, int, etc... It will be the easiest approach and you dont need to know the directory to load and write to. The api will take care of it. For straight registry programming search the web on CRegKey, it will show you the basics but it can be a bit cryptic but it is a wrapper class for registry functions.

Matt
 
why dont you simply create a file and save your values in it,so when you open back your "Dialog" you just need to load the values from the file and put them to the "combo box".

here is an example:

#include<stdio.h>
.........
....
void SaveValues( int value1, int value2, int value3 )
{
FILE *fb;

fb = fopen(&quot;backup.txt&quot;, &quot;w&quot;);

if( fb )
{
fprintf( fb, &quot;%d\n%d\n%d\n%d&quot;,value1, value2, value3 );
}
fclose(fb);
}

void LoadValues( int value1, int value2, int value3 )
{
FILE *fb;

fb = fopen(&quot;backup.txt&quot;, &quot;r&quot;);

if( fb )
{
fscanf( fb, &quot;%d %d %d %d&quot;,&value1, &value2, &value3 );
}
fclose(fb);
}
.......
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top