GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-May-2004, 13:22
Cerf Cerf is offline
New Member
 
Join Date: May 2004
Posts: 1
Cerf is on a distinguished road

Porting from VB to C++ (MFC) -> control arrays


Yo yo yo,

I'm new to the fourm so I'm just saying hello and I'm posting a quick question.

In VB I can create a control array of object, how can I create something simmilar in MFC?

Thanks
  #2  
Old 30-May-2004, 20:27
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Yes, In VB it would be a lot easier. Just copy and paste, but in MFC, I think you should just declare an array of control, create each element of the array and its done

CPP / C++ / C Code:
//in the headear file
CEdit MyControls[10];

//create each element of the array
 MyControls[0].Create(.....);
 MyControls[1].Create(.....);
 MyControls[2].Create(.....);
 .....
 .....
 .....
 MyControls[9].Create(.....);


//u can use a fro loop here if the ResourceID of your control is in sequence,
//each time u enter the loop, just increment the ID number.

Haven't tried this before, but it should work, cause its just the same prinsiple of using an array of a class.
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #3  
Old 08-Jun-2004, 20:33
yermolovd's Avatar
yermolovd yermolovd is offline
New Member
 
Join Date: Jun 2004
Location: Kitchener, ON, Canada
Posts: 6
yermolovd is on a distinguished road
Question

I have a very similar question, that deals with control arrays. Your help would be really appreciated.
The situation is this:
I have, lets say, 5-10 button controls on my form, that I have to hide and show. I programmed for a while in VB and I used control array feature there. Now when I started doing C++, I have some little problems with it.
Here's the code that works fine, except for one thing.

CPP / C++ / C Code:
CButton MyButtons[3];

MyButtons[0].m_hWnd = m_b1; 'button1
MyButtons[1].m_hWnd = m_b2; 'button2
MyButtons[2].m_hWnd = m_b3; 'button3
MyButtons[3].m_hWnd = m_b4; 'button4
for (int i=0; i<3; i++)
{
	MyButtons[i].ShowWindow(FALSE);
}

Everything is fine, except for the last control in the array, which is still visible after the execution of this code. I am really confused about this and have no idea how can this not work. I debugged it and it goes through the last control, still it is visible. Any input is welcome!

Thank You.
Last edited by LuciWiz : 09-Feb-2005 at 02:44. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #4  
Old 08-Jun-2004, 21:05
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
hi yermolovd,

your problem is that you defined an array of buttons with 3 elements only:
CPP / C++ / C Code:
CButton MyButtons[3];

MyButtons[0].m_hWnd = m_b1; 'button1
MyButtons[1].m_hWnd = m_b2; 'button2
MyButtons[2].m_hWnd = m_b3; 'button3

// this should not be,cause its out of range
MyButtons[3].m_hWnd = m_b4; 'button4
for (int i=0; i<3; i++)
{
 // here you hide buttons 0,1,2 that is i<3
  MyButtons[i].ShowWindow(FALSE);
}

this is what you should do:
CPP / C++ / C Code:
CButton MyButtons[4];

MyButtons[0].m_hWnd = m_b1; //button1
MyButtons[1].m_hWnd = m_b2; //button2
MyButtons[2].m_hWnd = m_b3; //button3
MyButtons[3].m_hWnd = m_b4; //button4
for (int i=0; i<4; i++)
{
  MyButtons[i].ShowWindow(FALSE);
}
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #5  
Old 08-Jun-2004, 22:16
yermolovd's Avatar
yermolovd yermolovd is offline
New Member
 
Join Date: Jun 2004
Location: Kitchener, ON, Canada
Posts: 6
yermolovd is on a distinguished road
Oh, LOL. Dumb me.
Thanks alot. My brains are too toast already to notice these things, I better be taking some rest.
  #6  
Old 08-Jun-2004, 22:43
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by yermolovd
Oh, LOL. Dumb me.

I say that to myself alot when i made stupid mistakes in vb.... I'm pretty beginner to vb.. and try's to apply c++ syntax to vb...
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #7  
Old 08-Jun-2004, 23:13
yermolovd's Avatar
yermolovd yermolovd is offline
New Member
 
Join Date: Jun 2004
Location: Kitchener, ON, Canada
Posts: 6
yermolovd is on a distinguished road
The other way around for me.

So is this the only way to manipulate your existing components? The only disadvantage for that is that I have to assign each control to an index of the array.
  #8  
Old 11-Jun-2004, 17:28
yermolovd's Avatar
yermolovd yermolovd is offline
New Member
 
Join Date: Jun 2004
Location: Kitchener, ON, Canada
Posts: 6
yermolovd is on a distinguished road

Need help again


Im tryint to create objects, as you mentioned in the second post
I tried using the code from MSDN

CPP / C++ / C Code:
C++ code
	CButton myButton1;
	myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), pParentWnd, 1);

If I compile it, compiler gives me error, that pParentWnd is undeclared. In MSDN example it doesnt say how should I declare it, and what it equals.
I also tried the same code with 'this' instead of 'pParentWnd'. It goes through the code, but I dont see any buttons created. Maybe it creates them, but somewhere where I can not see them?

Any help on creating components is great. I am going to convert that creation to the creation of array of components, which i know how to do now, only problem now is just creating a single component.

Thanks.
  #9  
Old 12-Jun-2004, 13:35
yermolovd's Avatar
yermolovd yermolovd is offline
New Member
 
Join Date: Jun 2004
Location: Kitchener, ON, Canada
Posts: 6
yermolovd is on a distinguished road
Ok, could someone please explain why

CPP / C++ / C Code:
CButton* pButton = new CButton;
pButton->Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), this, 1);

This works

But

CPP / C++ / C Code:
CButton MyButton1;
MyButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), this, 1);

Doesnt...?

If I have to go the first way of coding, do I have to create an array of pointers, and then apply the code?.. I'm a bit confused, I'll try to work this out, altough I wouldn't mind some help, because I have to write a program by Friday.
Last edited by LuciWiz : 09-Feb-2005 at 02:46. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #10  
Old 13-Jun-2004, 20:51
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Quote:
Originally Posted by yermolovd
CButton* pButton = new CButton;
pButton->Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), this, 1);

CButton MyButton1;
MyButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), this, 1);


both method is accepable and should work, but you need a litle change in your code.

this should be declared in the class defination as a member variable.
CPP / C++ / C Code:
CButton MyButton1;


its all about scope, if you declare it in the same function, the object(button) scope is only withing the funtion and end when it exits the function. So put the defination in the class defination.

On the other hand,
CPP / C++ / C Code:
CButton* pButton = new CButton;

don't have to care for a scope cause its a pointer and it lives as long as you dont 'delete' it. Now, you should see why..

Quote:
Originally Posted by yermolovd
If I have to go the first way of coding, do I have to create an array of pointers, and then apply the code?.. I'm a bit confused, I'll try to work this out, altough I wouldn't mind some help, because I have to write a program by Friday.

yes, you can also make an array of pointers but becarefull when using pointer, if you created it with new don't forget to delete it when youre finished. both ways are ok, but when using pointers, just be extra careful, cause you may get some unexpected debug error..

anyway, friday?? last friday or this coming friday?? Hope I not too late with my help....
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 01:22
FREE 25 MB, No Ads, Control Panel, ASP, ColdFusion, PHP, MySQL, Access Hosting rkmails Free Web Hosting 0 08-Sep-2003 06:49
Ultra-fast hosting with free setup and a Real 24/7 Support, $5/Mo, Control Panel Demo hostorange Web Hosting Advertisements & Offers 0 23-May-2003 08:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:16.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.