Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / Better programmingstyle

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Better programmingstyle
#3787
Posted by: 2004-04-09 20:47:58
Hi,
I?ve a problem with my current wxWidgets programmingstyle.
I?ve only two classes (From the Minimal Sample, MyApp and MyFrame)
and in this classes I?ve methodes like:



CreateMainMenu();
ClearMainMenu();

CreateOptionsMenu();
ClearOptionsMenu();



But two big classes are not good!
I wood make for every Menu an own class.
I?ve a few questions:
Need every class an own eventtable?
Must be every class I use in an eventtable derrivied from wxApp?

In my actuell classes I use
this
in much Controls,
so I try to make a base class and derrivie the other
menu classes from it. But I can?t acces to the wxFrame.

Knows someone a good designe Concept?

Thanks.

Greetings from Munich.
Message2. Re: Better programmingstyle
#3793
Posted by: 2004-04-10 16:29:53
Ok,
now I know that I need vor everey class an own Eventtable.

I have a class for the mainmenu,
in it I create The mainframe. (wxFrame)

class MainMenu : public wxFrame{
// ctor Create Controls with this as parameter for the parentwinows
//.....
};
The methodes for the buttons of the mainmenu:

void OnButton1(){

// Here I want create an object from the Menu2, but this doesn?t go.
// My problem ist, that I don?t know what I can give as parent.
// I don?t know the name of the window, I?ve create in the MainMenu Ctor
Menu2 *m = new Menu2(this); // this don?t work! (programm crashed)

}

Then I?ve the Menu2 class:

class Menu2 : public wxFrame{
// in the ctor I create the Controls 
Menu2(wxWindow *parent){
wxButton *b = new wxButton(parent);
// ....
}
I hope you understand my problem.

Thanks.

Greetings from Munich.
Max
Message3. Re: Better programmingstyle
#3795
Posted by: 2004-04-10 20:59:57
Hi!
Nope, to be honest: I don't understand your problem.
Why would you create a class that should be a menu, like MainMenu, and derive it from wxFrame instead of wxMenu?
Seriously: Have you worked with C++, Java or any other object oriented programming language before?
The question is: Why would you want to derive a new class from a given one? Normally you will want to extend it's capabilities. So, you've got your own frame class, derivef from wxFrame, which resembles the frame you want with all the base functionality (and methods) wxFrame offers. You could go and create a new MyMenu class derived from wxMenu if you want, too, but I doubt that this makes much sense, because you don't add functionality to it.

Maybe wxGuide would be interesting to you. http://wxguide.sourceforge.net/
It deals with design aspects and gives some advise on programming style.

Need every class an own eventtable?
Must be every class I use in an eventtable derrivied from wxApp?
No, it doesn't have to have an eventtable. If you need one, declare it, otherwise don't. Events generated by derived classes which are not dealt with in the class itself will nornmally propagate to it's parent.

Sorry that I can't offer any good help at all. Try to be a little bit more concrete and I'll try.
Message4. Re: Better programmingstyle
#3798
Posted by: 2004-04-10 22:33:14
Ok, i have wangled that I can now create the window with a pointer.
(I don?t know what I?ve maked false)

Why would you create a class that should be a menu, like MainMenu, and derive it from wxFrame instead of wxMenu?
Because I?ve thinked that every class, that I want to use in an Eventtable must be derrivied from wxFrame and I don?t know wxMenu. If I use an eventtable with only one class argument, I get a syntax error.
(DeclareEventTable is in the definition of the class )

Seriously: Have you worked with C++, Java or any other object oriented programming language before?
Yes I?ve and I?ve much more C++ experience as you seem to think.

No, it doesn't have to have an eventtable. If you need one, declare it, otherwise don't. Events generated by derived classes which are not dealt with in the class itself will nornmally propagate to it's parent.
I wanted to aks that I can use  1Eventtable for many classes.

In the MainMenu class I create the Window:

class mainmenu : public wxFrame{
    // ctor create the Window, but I don?t 
    MainMenu(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame(frame, -1, title, pos, size)
    {
    // Create Controls
    } 
    // The variable for the window:
    wxFrame *frame; 
    // here I delete the controls (I want this methode, not a dtor):
    ClearMenu(){
    // deleteControls
    } 
    // Methode will be called if the Menu2 Button was pressed:
    void OnMenu2Button(); 
};

Then I?ve a Class that should create other controls in the self window:

class Menu2 : public wxFrame{
    // The ctor
    Menu2(wxWindow *parent){
    // Here I create Controls in the window, that the mainmenu have created:
    wxButton *b = new wxButton(parent,-1);
              b->Show(TRUE);
    } 
    // here I delete the controls (I want this methode, not a dtor):
    ClearMenu(){
    // deleteControls
    delete b;
    }   
};
MyProblem is the OnButton2 methode:

void MainMenu::OnButton2(){
// Here I clear the mainmenu
ClearMenu();
// And I want to create an instance of Menu2:
Menu2 *m = new Menu2(frame); // Programm crashed by creating Menu2!
}
The Menu2 ctor get the parent window as parameter and the controls use it,
but the programm crashed.

My C++ is good enough, I think my English is to broken:), to explain my Problems.











  
Message5. Re: Better programmingstyle
#3799
Posted by: 2004-04-10 22:39:07
A comment in the definition of the class ahead was wrong, it should be:

class mainmenu : public wxFrame{
    MainMenu(const wxString& title, const wxPoint& pos, const wxSize& size)
    : wxFrame(frame, -1, title, pos, size)
    {
    // Create Controls
    } 
    // The variable for the window:
    wxFrame *frame; 
    // here I delete the controls (I want this methode, not a dtor):
    ClearMenu(){
    // deleteControls
    } 
    // Methode will be called if the Menu2 Button was pressed:
    void OnMenu2Button(); 
};
Message6. Re: Better programmingstyle
#3800
Posted by: 2004-04-10 22:47:17
With menu I meant a window in that I create controls, I don?t mean the Windowmenu.

It should will be a VocabularyTrainer, so I?ve to clear the window Controls,
like Buttons if I Create a new Submenu.

A Sample:

The MainMenu:

|-----------|
|  Button1  |
|           |
|-----------|

Then I clear it and Create an new Control on the same postition:
|-----------|
|  Button2  |
|           |
|-----------|
With one class no problem, because all methodes have acces of the
frame in that the create,(all times the same frame)
but with more classes (one class for every Sub-menu)
I is hard. I give the window as parameter, but the programm crashed,
maybe I?ll derrivie all Sub-Menus from the Mainmenu.

Thanks.

Greetings from Munich.
Message7. Re: Better programmingstyle
#3801
Posted by: 2004-04-11 01:02:51
Hi!
Because I?ve thinked that every class, that I want to use in an Eventtable must be derrivied from wxFrame and I don?t know wxMenu.
You can set up event tables for all classes that were derived from wxEvtHandler. Since most controls are derived from it, you can use them in event tables. But there is no need to do so. You could use SetEventHanlder() or PushEvtHandler() and don't use event tables at all.
Yes I?ve and I?ve much more C++ experience as you seem to think.
I didn't mean to offend you. If I did, I'm sorry and hope that you'll except my appology. I just wanted to be sure that I'm not dealing with a newbie who has some crud ideas... If you know what you want -> fine! :)

So, I think I'm getting on to it... You want a frame, maybe with a panel, display a button on it and when some action takes place you want to change the button. Well, if I got that right I still don't get why you need two wxFrames for that.

If you'd like to, mail me in german at upcaseATgmxDOTnet (sorry for this, but I currently can't login to my account, so there's no mailto).

upCASE
Message8. Re: Better programmingstyle
#3802
Posted by: Master_Max 2004-04-11 01:24:16
Ich habe ein Fenster und will das beide Klassen (MainMenu + Menu2)
in dieses Fenster schreiben. Deshalb übergebe ich dem Konstruktor
von Menu2 die Adresse des Fensters(welches in MainMenu deklariert wurde).
Wenn ich nun ein Controll in der Menu2 Klasse erstelle, welches ja das Frame
aus der MainMenu Klasse per Parameterübergabe im Konstruktor erhalten hat,
stürtz das Programm ab.

Danke.
Message9. Re: Better programmingstyle
#3803
Posted by: Master_Max 2004-04-11 01:33:39
Zu den Posts oben und warum der Thread Better programmingstyle hei?t,
ich habe im moment nur zwei gro?e Klassen in denen ich die unterschiedlichen Submenüs erstelle und die Klasse hat nun schon ohne Header! 600 Zeilen Code.
Deshalb will ich für jedes Submenü eine eigene Klasse machen, doch alles Subenüs sollen in das selbe Fenster schreiben, es wird im Programm (Vokabeltrainer) kein einziges anderes Fenster err?ffnet.
Message10. Re: Better programmingstyle
#3814
Posted by: upCASE 2004-04-13 00:35:40
Hi!
Max: When I said "mail me in german", I meant mail me in german, not post to this thread in german. Next time please MAIL me, because I think that a forum should be accesible and understandable for everyone who visits.

Ok, I think by now I understood what you want to do. I'll try to explain it now, in case somebody else want to comment, too.
Max is writing a vocabulary trainer that consists of one frame. During runtime there will be no other frames or dialogs. His "menu" is a set of controls, most likely buttons, he wants to be displayed in the main frame. Therefor, when creating Menu2, he passes the frame pointer on as the parent window for the controls. Problem is that when he tries to generate a control in Menu2 the program crashes. He named the thread "better programmingstyle" because his classes have more then 600 lines of code and he now wants to split the "menu" classes to seperate them.

I guess the problem is the logic of inheritance. Deriving the "menu" classes from wxFrame makes no sense at all. Think of it: You create one wxFrame derived class, e.g. MainMenu. Now you create a second wxFrame derived class, e.g. Menu2, that gets the pointer to the frame passed and stores it. So when you create a new instance of Menu2, you implicitly call the default constructor of wxFrame, meaning that there is another frame existing by now. I guess this is what crashes the program.

My advise: Rethink what you want to do and derive your menu classes from wxEventHandler if they just do event handling stuff.
Maybe someone else can comment on that...
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message11. Re: Better programmingstyle
#3846
Posted by: Master_Max 2004-04-16 01:20:14
Sorry for posting in German I wont doing again, but I have hoped that there are more German members...

I will try what you said and if it doesn?t work, I will ask in an other Forum in German.

Thank you for your help
MfG Max
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Fri 2024-4-19  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0