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

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. wxTextValidator Problem
#3766
Posted by: 2004-04-08 18:15:52
Hi,
firt sorry for my broken English, I?m a pupil from Germany.
I?ve a problem with a wxTextCtrl Control and it?s validator.
It doesn?t write in the value that I give the wxTextCtrl to write
and if I compare the value of the wxTextCtrl (maybe empty) with a std::string
the programm crashed.

I?ve made a Little running Example with the MinimalSample:

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWindows headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

// ----------------------------------------------------------------------------
// resources
// ----------------------------------------------------------------------------

// the application icon (under Windows and OS/2 it is in resources)
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
    #include "mondrian.xpm"
#endif

#include <string>
using namespace std;

// ----------------------------------------------------------------------------
// private classes
// ----------------------------------------------------------------------------

// Define a new application type, each program should derive a class from wxApp
class MyApp : public wxApp
{
public:
    // override base class virtuals
    // ----------------------------

    // this one is called on application startup and is a good place for the app
    // initialization (doing it here and not in the ctor allows to have an error
    // return: if OnInit() returns false, the application terminates)
    virtual bool OnInit();

    // this methode compare the std::string with the wxString:
    void TestVoc()
    {
        if(TestValue.compare( stdString.c_str() ) == 0){
            wxMessageBox("equal");
        }
    }
    MyApp():stdString("Hello"){}
private:
    string stdString; // the std::string
    wxTextCtrl *TestInputField;// the textfield
    wxString TestValue;// In it should be saved the value of the wxTextCtrl
  
};

// Define a new frame type: this is going to be our main frame
class MyFrame : public wxFrame
{
public:
    // ctor(s)
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
            long style = wxDEFAULT_FRAME_STYLE);
private:
    // any class wishing to process wxWindows events must use this macro
    DECLARE_EVENT_TABLE()
};

// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------

// IDs for the controls and the menu commands
enum{
    // ID of the Textfield:
    TestInputFieldID
};

// ----------------------------------------------------------------------------
// event tables and other macros for wxWindows
// ----------------------------------------------------------------------------

// the event tables connect the wxWindows events with the functions (event
// handlers) which process them. It can be also done at run-time, but for the
// simple menu events like this the static method is much simpler.
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_TEXT_ENTER(TestInputFieldID,MyApp::TestVoc)// If enter was pressed, the     //methode will be called.
END_EVENT_TABLE()

// Create a new application object: this macro will allow wxWindows to create
// the application object during program execution (it's better than using a
// static object for many reasons) and also declares the accessor function
// wxGetApp() which will return the reference of the right type (i.e. MyApp and
// not wxApp)
IMPLEMENT_APP(MyApp)

// ============================================================================
// implementation
// ============================================================================

// ----------------------------------------------------------------------------
// the application class
// ----------------------------------------------------------------------------

// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
    // create the main application window
    MyFrame *frame = new MyFrame(_T("Minimal wxWindows App"),
                                 wxPoint(50, 50), wxSize(450, 340));

    // and show it (the frames, unlike simple controls, are not shown when
    // created initially)
    frame->Show(TRUE);
  
    // The TextField:
    TestInputField = new wxTextCtrl(frame,TestInputFieldID," ",wxPoint(170,140),
                     wxSize(160,45),0,wxTextValidator(wxFILTER_NONE,&TestValue));
    TestInputField->Show(TRUE);

    // success: wxApp::OnRun() will be called which will enter the main message
    // loop and the application will run. If we returned FALSE here, the
    // application would exit immediately.
    return TRUE;
}

// ----------------------------------------------------------------------------
// main frame
// ----------------------------------------------------------------------------

// frame constructor
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
       : wxFrame(NULL, -1, title, pos, size, style)
{
}


Please help, I havn?t any idea what?s wrong.
Thanks.

MfG Max
Message2. Re: wxTextValidator Problem
#3768
Posted by: 2004-04-08 18:53:54
hi,

why do you use std::string and not wxString?
This may be the problem, because you compare a std::string with a wxString,
like apples and pears ;-)
I guess it might be good idea, to use wxString only, while using wxWidgets.

Wgg
Achim
Message3. Re: wxTextValidator Problem
#3769
Posted by: 2004-04-08 19:02:54
Yes, but I?ve coded a methode that write the value of a .txt Data into a std::vector<string>. Is it able to write a txt Data into a vector<wxString>?

Thanks.

MfG Max
Message4. Re: wxTextValidator Problem
#3770
Posted by: 2004-04-08 19:06:50
I?ve heard wxWidgets 2.5 will assist the STL.
Is that right?

MfG Max
Message5. Re: wxTextValidator Problem
#3771
Posted by: upCASE 2004-04-08 19:08:33
Hi!
Check this out and ask if you don't understand it.
Header:

#include "wx/wxprec.h"
#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
    #include "mondrian.xpm"
#endif

#include <string>
using namespace std;

class MyApp : public wxApp
{
public:
     virtual bool OnInit();
};

class MyFrame : public wxFrame
{   wxString TestValue;// In it should be saved the value of the wxTextCtrl
    wxTextCtrl *TestInputField;// the textfield
    string stdString; // the std::string
public:
    MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size,
            long style = wxDEFAULT_FRAME_STYLE);
    
private:
    void TestVoc(wxCommandEvent& evt);
    DECLARE_EVENT_TABLE()
};

enum{
    TestInputFieldID
};

Source:

#include "base.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame(_T("Minimal wxWindows App"),
                                 wxPoint(50, 50), wxSize(450, 340));
    frame->Show(TRUE);
    return TRUE;
}


BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_TEXT_ENTER(TestInputFieldID,MyFrame::TestVoc)
END_EVENT_TABLE()

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
       : wxFrame(NULL, -1, title, pos, size, style)
{
     stdString = "Hello";
     wxTextValidator val(wxFILTER_ALPHA,&TestValue);
     TestInputField = new wxTextCtrl(this,TestInputFieldID," ",wxPoint(170,140),
                     wxSize(160,45),0,val);
}

void MyFrame::TestVoc(wxCommandEvent& evt)
{       TransferDataFromWindow();
        if(TestValue.Cmp( stdString.c_str() ) == 0){
            wxMessageBox("equal");
            }
        else
         {        wxString t;
                  t << stdString.c_str() << " - " << TestValue;
                  wxMessageBox(t);
         }    
}

Note: This validator will not accept numbers.
Hope this helps a little...
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message6. Re: wxTextValidator Problem
#3772
Posted by: 2004-04-08 19:15:32
Hi,
thank you verry much. (That was what I?ve need)
I?ve a last little question, how do you made codetags?

MfG Max
Message7. Re: wxTextValidator Problem
#3773
Posted by: upCASE 2004-04-08 22:12:41
Hi!
1. About that vector<wxString> thing -> It should work! Although it may depend on how you implemented your txt file reading method. wxWidgets offers a "replacement" for vectors, wxArray. Maybe have a look at it. But I don't see any reason to not use the STL vector, since every new compiler has support for templates, so you won't break platform independence.

2. Yes, 2.5.1 has support for STL. BUT there's a drawback -> I couldn't get it to work using gcc. May not be a problem with VC++, but I'll wait till 2.6 and then try again. Using STL actually would mean that wxArray is nothing else than a std::vector<> :)

3. The tags for this forum are listed below.

Greetings from Cologne!
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message8. Re: wxTextValidator Problem
#3775
Posted by: 2004-04-09 02:59:18
Thanks at all, that?s a verry good board.

Greetings from Munich.

Max
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Wed 2021-7-28  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0