Link to home
Start Free TrialLog in
Avatar of jaroslav
jaroslav

asked on

Create controls dynamically

I want to build a meta-database.
This database contains all information about objects to be created.

e.g.
Forms:
id   text  x  y height width ...
1    cust  0  0 500    600

Fields:
id  type  name  x   y   height  width...
1   CEdit name  10  10  50      300

In my app I have to create the
form and within this form all the controls

My question:
there's no problem to create these objects - but:
- how do I interact with these objects?
- how do I know, what's the text of a specific edit control?
- how do I capture and identify the messages sent by any of these objects?

Of course, I can create an application with any controls and VC 6.0 does everything for me. But that's not the way because I want that the description of everything is described externally and the creation is dynamically...
Avatar of KangaRoo
KangaRoo

You want the application to fetch form layout from this db or create resource scripts from the db?
Hi jaroslav
lets take a example of a edit box U have created
in .h file
CEdit edit;

in implimentation file
CRect editrect(100,270,220,290);
edit.Create(WS_CHILD | WS_VISIBLE |WS_BORDER|ES_CENTER|ES_AUTOHSCROLL,editrect,this,200);

1. To interact with the object U can do like this
U have id lets say 200 which U have used while creating.
U can use the variable to interact say U want to set some text then
edit.SetWindowText("MyText");
2.to know text of this edit control
CString strMyString;
edit.GetWindowText(strMyString);

3.To capture the messages sent U have to do the following

a. in UR BEGIN_MESSAGE_MAP code

put
ON_EN_KILLFOCUS(200, OnKillfocusEdit1)

and in the header file add entry

afx_msg void OnKillfocusEdit1();

as soon as kill focus happens the function OnKillfocusEdit1(){} will be called.

Hope this helps
inpras



Hi inpras !!
   But he/she doesn't know what type of control it is, he/she reads it from the database.
 Am i right jaroslav
Avatar of jaroslav

ASKER

Inpras, thank's for your feedback.

What akalmani wrote, is correct.

Just my db knows what kind and how much objects I am to create. There can be different controls (e.g. 5 edit boxes, 2 buttons, 3 list views, ...). The content of these controls is linked in my meta-database to real database fields (e.g. there is a sql-command: select id, name, address from customers
the db fields are mapped to controls:
- Form:
name=customers x=0 y=0 h=500 ...
- Fields:
ID 1: name=Customer Name type=CEdit x=10 y=10 ...
ID 2: name=Customer Address type=CEdit x=10 y=80 ...

So I'll have to create all these controls, perform my sql-command, set the values of the controls to the corresponding value from my query.

Then I'll have to catch all the modifications made in any of the controls and write them back to db:
update customers set name=<value of corresponding CEdit control (ID=1 - name = Customer Name)>

Hope, there's a way for that!

regares - jaroslav
Yep, it's called MS Access ;) ,Just kidding, even this kind of control repository is not in its trick-box.
Obviously, this is not a trivial problem. My application does this, and the way that I've implemented it is with a Field object that keeps track of all the Edit controls (various types).
The Field stores a master copy of the data. The Field is also responsible for reads and writes, creating the fields, resizing, etc.
Then I keep a list of these fields in the Form object and iterate thru the fields when I need to access them. There are problems such as tabbing and handling other Windows messages at the edit control and keeping track of the which field you're in (which isn't really important), just has to appear correct to the user.  You do this by sending messages to the parent and let the parent handle the tabbing etc, by sending messages back.
When the form closes, you then iterate thru the lists to close everything.
Good luck, hopefully I've given you enough of an idea to get you going.
Thank's for the feedback.

The described way sounds good - but I need an example, part of a source doing that.

jaroslav
You're welcome. But the source to describe it would be too large. There are many interrelated classes, such as fonts, different styles of edit boxes, such as single line edit/ multiline, checkboxes, popups, etc.

Good luck with your hunt.
You could do like inpras said and use ranges for your ID's:

100-199 = Edit
200-299 = List
....

Regards,

Bernd
An example would be great. I need a logical walkthrou in order to be able to construct the app.

Hey, I don't have much more points to give!

Or, is that, what I want so extraordinary or complicated?

I started with C++ some months ago (I worked with PowerBuilder) just because I like this language and thougt, that I could do anything, things I wasn't able to do with other languages. Sometimes, it needs just a litte help...

thank's and regards

jaroslav
As stated before, it's quite complicated.
You may want to take a look at the patterns book "Design patterns, elements of reusable, object oriented software" from Gamma, Helm et al (also know as the GoF).
Tell me what your app what to do, and I'll try to give you a brief walk thru.




Nevering, thank's for your reply.

Usally, I have programs with a static design. After having written an application, I had to add some fields, modify the position of existing fields, grids, buttons...

Most of my applications are designed for customers like wholesalers, let's say, database-application with quite a known set of functions, e.g.:
- work with simple data (customer, item, supplier...)
- reports
- forecast
- navigation (first record, next record..., insert, update, delete, ...)

As you can see from earlier messages I quoted, I'd like to describe the forms, fields, grids, validations, relations in a separate part of my db - in configuration-tabels.

My goal:
- user starts app
- select what he/she wants to do (e.g. add customer)
- what will happen, is described in my db (load form, load fields, connect to table(s), load db-fields into form-fields)
- user makes modifications
- request for saving data
- validation
- update table(s) from form-fields

Reading the required values from these configuration-tables, that's not my problem. But how do I create all the forms, fields, etc., dynamically and I still do know, what the user is doing, which fields the user is modifying and how can I create shortcuts for dynamically created fields.

Thank's for your effort trying to help me.

Regards

jaroslav

ASKER CERTIFIED SOLUTION
Avatar of Nevering
Nevering

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for my late reply - have been out of work for a while...

I had a look at your code and tried to build my own code using your input.

It's a lot of work - but it will work this way.

Thank's a lot.

Regards - jaroslav