Focus problem Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Focus problem

Post by Wolfgang » Mon Oct 21, 2019 6:07 pm

Code: Select all

EVT_SET_FOCUS(MyFramebearb::Onsetf)
EVT_TEXT_ENTER(wxID_ANY,MyFramebearb::Ontenter)

Code: Select all

    void Onsetf(wxFocusEvent& event);
    void Ontenter(wxCommandEvent& event);

Code: Select all

void MyFramebearb::Onsetf(wxFocusEvent& event)
{
    wxInt16 count;
    count = 0;
    }    while (event.GetId()!= my_bib[count]->GetId()&&count<200){}
I have a panel with a lot of wxtextctrl my_bib[20], so what I want when ever one of them is active and has the cursor blinking in it, I want to knwo which one it is.
However the focus event does not get called, so I might use the wrong event ofr it.

For the second problem:

When changing is done, (enterpressed) I want to set the focus back so that the window is again scrollable, as it is, before the first wxtextctrl did get activated. The panel is the top panel of the whole frame. the event gets called, but the focus is not correctly set back to the one I need it.

Code: Select all

void MyFramebearb::Ontenter(wxCommandEvent& event)
{
    panel->SetFocus();
}

User avatar
doublemax
Moderator
Moderator
Posts: 15079
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Focus problem

Post by doublemax » Mon Oct 21, 2019 6:54 pm

wxFocusEvent does not derive from wxCommandEvent. This means that it doesn't propagate upwards in the window hierarchy. You need to catch the event at the control that generates the event.

In the loop where you create the wxTextCtrls:

Code: Select all

wxTextCtrl *tc = new wxTextCtrl(...);
tc->Bind( wxEVT_SET_FOCUS, &MyFramebearb::Onsetf, this);
(This code assumes that it runs in the MyFramebearb constructor).

Regarding the second problem, i'm not sure, but maybe the SetFocus() calls comes too early. Try this:

Code: Select all

void MyFramebearb::Ontenter(wxCommandEvent& event)
{
  CallAfter( [this] {
    panel->SetFocus();
  });
}
Use the source, Luke!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4368
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Focus problem

Post by ONEEYEMAN » Mon Oct 21, 2019 7:30 pm

Hi,
Also, you ABSOLUTELY HAVE TO call event.Skip() in all non-wxCommandEvent handlers, unless you know what you are doing.

Thank you.

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: Focus problem

Post by Wolfgang » Tue Oct 22, 2019 10:36 am

To Problem 2:
What I forgot to write is, that the scrolling with the mouse wheel should work again, and that does not work. Solved it by setting the focus to the cancel button, and not to the panel, then it works. What I guess, any object which is not above( hierarchy wise) of the current textctrl will work, but other textctrl would not help either.

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: Focus problem

Post by Wolfgang » Tue Oct 22, 2019 11:59 am

To problem1:

Code: Select all

        my_bib[count + addc * 10] = new wxTextCtrl(mybuild, wxID_ANY, myword[count + addc * 10], wxDefaultPosition, wxSize(150, 25), wxTE_PROCESS_ENTER);
           my_bib[count + addc * 10]->Bind(wxEVT_SET_FOCUS, &MyFramebearb::Onsetf,this);

in windows.h

Code: Select all

operator wxWindowID() const
    {
        return m_id;   //HERE ERROR
    }
reading access violation
this was 0xFFFFFFFFFFFFFF

This error comes when it builds up the frame, and whenever I click on one of the bound wxtextctrl. If I remove the binding everything works fine.

What could be the problem?

User avatar
doublemax
Moderator
Moderator
Posts: 15079
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Focus problem

Post by doublemax » Tue Oct 22, 2019 12:14 pm

What's the context of the code? It only works if "this" points to an instance of MyFramebearb
This code assumes that it runs in the MyFramebearb constructor
Use the source, Luke!

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: Focus problem

Post by Wolfgang » Tue Oct 22, 2019 1:54 pm

Code: Select all

MyFramebearb::MyFramebearb(wxWindow * parent, const wxString &title, int x, int y, int w, int h) :
	wxFrame(parent, wxID_bearb, title, wxPoint(x, y), wxSize(w, h))
{
Further, it does not through the exception by the bind, it throughs it by showing the form and then by clicking on the items.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 4368
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Focus problem

Post by ONEEYEMAN » Tue Oct 22, 2019 1:58 pm

Hi,
Wolfgang wrote:
Tue Oct 22, 2019 11:59 am
To problem1:

Code: Select all

        my_bib[count + addc * 10] = new wxTextCtrl(mybuild, wxID_ANY, myword[count + addc * 10], wxDefaultPosition, wxSize(150, 25), wxTE_PROCESS_ENTER);
           my_bib[count + addc * 10]->Bind(wxEVT_SET_FOCUS, &MyFramebearb::Onsetf,this);

in windows.h

Code: Select all

operator wxWindowID() const
    {
        return m_id;   //HERE ERROR
    }
reading access violation
this was 0xFFFFFFFFFFFFFF

This error comes when it builds up the frame, and whenever I click on one of the bound wxtextctrl. If I remove the binding everything works fine.

What could be the problem?
This means that the constructor of the text control failed.

Can you show the constructor of the frame please?

Thank you.

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: Focus problem

Post by Wolfgang » Tue Oct 22, 2019 2:12 pm

inside bearb.h
The constructor

Code: Select all

MyFramebearb(wxWindow * parent, const wxString &title, int x, int y, int w, int h);
Last edited by Wolfgang on Tue Oct 22, 2019 2:13 pm, edited 1 time in total.

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: Focus problem

Post by Wolfgang » Tue Oct 22, 2019 2:13 pm

inside mainprogramm I open the frame like this:

Code: Select all

                  frame6 = new MyFramebearb(this, sp_text[58], bearbx, bearby, 1530, 700);
               
                    frame6->Show(true);

alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 317
Joined: Tue Oct 18, 2016 2:31 pm

Re: Focus problem

Post by alys666 » Tue Oct 22, 2019 2:56 pm

people asked you about constructor body, not its declaration
ubuntu 16.04, wxWidgets 3.0.4

User avatar
doublemax
Moderator
Moderator
Posts: 15079
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Focus problem

Post by doublemax » Tue Oct 22, 2019 2:57 pm

These single line code snippets don't help much. I still can't see in which context the wxTextCtrl construction is executed.
Use the source, Luke!

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: Focus problem

Post by Wolfgang » Tue Oct 22, 2019 3:13 pm

Code: Select all

MyFramebearb::MyFramebearb(wxWindow* parent, const wxString& title, int x, int y, int w, int h):
       wxFrame(parent, wxID_bearb, title, wxPoint(x, y), wxSize(w, h))
{

	

    panel = new wxScrolledWindow(this, wxID_ANY, wxPoint(x, y), wxSize(w, h));

   mybuild = new wxScrolledWindow(panel, wxID_ANY, wxPoint(x, y), wxSize(w, h - 200));
    mainsizer = new wxBoxSizer(wxHORIZONTAL);
    mainsizer->AddSpacer(7);
    wxBoxSizer* row_sizer[400];
    wxInt16 count;
    count = 1;
    wxSQLite3ResultSet set, set3;
    wxString help;
    help << bearbvn;
    if (ord_wnum == "yes")
    {
        set = h_dbi[subibi[sucwin]].ExecuteQuery(wxS("SELECT * FROM bibel where versnum=" + help + " order by wnum;"));
 }
    else
    {
        set = h_dbi[subibi[sucwin]].ExecuteQuery(wxS("SELECT * FROM bibel where versnum=" + help + " order by wnum2;"));
    }
    while (set.NextRow())
    {

        wxSQLite3ResultSet set2;
           
        help2[count] = set.GetAsString(0);
        help2[count] = help2[count].Left(help2[count].Length() - 2);
        myspez[count] = set.GetAsString(7);
        myword[count] = set.GetAsString(8);
        mywnum[count] = set.GetAsString(9);
        mywnum[count] = mywnum[count].Left(mywnum[count].Length() - 2);
        mystron[count] = set.GetAsString(6);
        myxref[count]= set.GetAsString(10);
        mynote[count] = set.GetAsString(11);

        wxString ref_help, ref_help2;
        ref_help2 = myxref[count];
        while (ref_help2.Length() > 0)
        {
            wxString ref_help3;
            if (ref_help2.find(";") > 0)
            {
                ref_help3 = ref_help2.Left(ref_help2.Find(";"));
                ref_help2 = ref_help2.Right(ref_help2.Length() - ref_help2.Find(";")-1);
            }
            else
            {
                ref_help3 = ref_help2;
                ref_help2 = "";
            }
            wxString ref_help4;
            wxInt16 ref_help5;
            ref_help4 = ref_help3.Left(ref_help3.Find("."));
            ref_help3 = ref_help3.Right(ref_help3.Length() - ref_help3.Find(".")-1);
            ref_help3.Replace(".", ";");
            ref_help5 = wxAtoi(ref_help4);


            ref_help = ref_help+bushnam[ref_help5] + " " + ref_help3+";";
        }








        myxref[count] = ref_help;
        set2 = h_dbi[5].ExecuteQuery(wxS("SELECT * FROM bibel where wnum=" + help2[count] + ";"));
        if (set2.NextRow())
        {
            mymorp[count] = set2.GetAsString(8);
        }
        set3 = h_ddi->ExecuteQuery(wxS("SELECT * FROM topics where subject='" + mystron[count] + "';"));
        wxString newh;
        newh = set3.GetAsString(0);
        set3 = h_ddi->ExecuteQuery(wxS("SELECT * FROM content where topic_id=" + newh + ";"));
        wxString longstron;
        longstron = set3.GetAsString(2);
        mydata[count] = longstron;

        count++;
    }
    count = 1;
    wxInt16 addc = 0;
    while (count<11){
      row_sizer[count] = new wxBoxSizer(wxVERTICAL);
        addc = 0;

        while (help2[count + addc * 10] != "") {

     
            my_bib[count + addc * 10] = new wxTextCtrl(mybuild, wxID_ANY, myword[count + addc * 10], wxDefaultPosition, wxSize(150, 25), wxTE_PROCESS_ENTER);
           my_bib[count + addc * 10]->Bind(wxEVT_SET_FOCUS, &MyFramebearb::Onsetf,this);
            wxFont myFont(10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD);
           
            my_bib[count + addc * 10]->SetFont(myFont);
            row_sizer[count]->Add(my_bib[count + addc * 10]);
            my_wnum2[count + addc * 10] = new wxTextCtrl(mybuild, wxID_ANY, mywnum[count + addc * 10], wxDefaultPosition, wxSize(150, 20));
            row_sizer[count]->Add(my_wnum2[count + addc * 10]);

            my_stat = new wxStaticText(mybuild, wxID_ANY, help2[count + addc * 10], wxDefaultPosition, wxSize(150, 20), wxALIGN_CENTRE_HORIZONTAL);
            row_sizer[count]->Add(my_stat);
            wxColor mycol;
            mycol.Set("#B8860B");
            my_stat2 = new wxStaticText(mybuild, wxID_ANY, mymorp[count + addc * 10], wxDefaultPosition, wxSize(150,45), wxALIGN_CENTRE_HORIZONTAL);
            my_stat2->SetForegroundColour(mycol);
            row_sizer[count]->Add(my_stat2);

            my_stat3 = new wxStaticText(mybuild, wxID_ANY, mystron[count + addc * 10], wxDefaultPosition, wxSize(150, 20), wxALIGN_CENTRE_HORIZONTAL);
          
            mycol.Set("#"+strcolor);
            //wxColor(51, 102, 255)
            my_stat3->SetForegroundColour(mycol);
            row_sizer[count]->Add(my_stat3);
            my_xref[count + addc * 10] = new wxTextCtrl(mybuild, wxID_ANY, myxref[count + addc * 10], wxDefaultPosition, wxSize(150, 20));
            row_sizer[count]->Add(my_xref[count + addc * 10]);
            my_note[count + addc * 10] = new wxTextCtrl(mybuild, wxID_ANY, mynote[count + addc * 10],wxDefaultPosition,wxSize(150,20));
            row_sizer[count]->Add(my_note[count + addc * 10]);
          //  my_stat6 = new wxStaticText(mybuild, wxID_ANY, mymean[count + addc * 10]);
          //  row_sizer[count]->Add(my_stat6);
           
            wxStringInputStream in{ mydata[count + addc * 10] };
            wxRichTextXMLHandler handler2;

            row_sizer[count]->AddSpacer(7);
            m_toolbe[count] = new wxRichTextCtrl(mybuild, wxID_ANY, "test ");
            m_toolbe[count]->SuppressingUndo();


            if (!handler2.LoadFile(&m_toolbe[count]->GetBuffer(), in))
            {
                throw std::runtime_error{ "Failed to set the contents of wxRichTextCtrl!" };
            }

            m_toolbe[count]->LayoutContent();
            m_toolbe[count]->PositionCaret();
            m_toolbe[count]->SetupScrollbars(true);
            m_toolbe[count]->Refresh(true);
          
            m_toolbe[count]->SetInitialSize(wxSize(150, 100));
            m_toolbe[count]->SetEditable(false);
           // m_toolbe->SetScrollPos(wxVERTICAL, 500, true);
            wxLongLong mlinb;
            mlinb = 0;
 
            wxString smean;
            wxInt16 mlinb2 = 100;
            m_toolbe[count]->SelectWord(mlinb2);
            smean = m_toolbe[count]->GetStringSelection();
            while (smean != "Definition")
            {
                mlinb2=mlinb2 + 5;
                m_toolbe[count]->SelectWord(mlinb2);
                smean = m_toolbe[count]->GetStringSelection();
            
            }
            m_toolbe[count]->ShowPosition(mlinb2);
            m_toolbe[count]->ScrollLines(15);
            m_toolbe[count]->SetSelectionRange(wxRichTextRange(0, 0));
            m_toolbe[count]->Update();
            row_sizer[count]->Add(m_toolbe[count]);
            row_sizer[count]->AddSpacer(15);

            addc++;
  
        }
        row_sizer[count]->AddSpacer(10);
        mainsizer->Add(row_sizer[count]);
        
        count++;
    }
   mybuild->SetScrollbar(wxHORIZONTAL, wxDEFAULT, 10, 100, true);
   mybuild->SetScrollbar(wxVERTICAL, wxDEFAULT, 10, 100, true);
   mybuild->SetScrollRate(150, 0);
    mybuild->FitInside();
    mybuild->SetSizerAndFit(mainsizer);
    mainSizer = new wxBoxSizer(wxVERTICAL);
    mainSizer->AddSpacer(7);
    mainSizer->Add(mybuild);
    wxBoxSizer* lower_sizer = new wxBoxSizer(wxVERTICAL);
    wxPanel* panel2 = new wxPanel(panel, wxID_ANY, wxDefaultPosition, wxSize(w, 20), wxTAB_TRAVERSAL);
    panel3 = new wxPanel(panel, wxID_ANY, wxDefaultPosition, wxSize(w, 20), wxTAB_TRAVERSAL);
    wxBoxSizer* button_sizer2 = new wxBoxSizer(wxHORIZONTAL);
    spez_butsizer = new wxBoxSizer(wxHORIZONTAL);

    my_abb = new wxButton(panel2, Button_abb_bearb, sp_text[10]);
    button_sizer2->Add(my_abb, 0, wxALL, 10);
    
      //  my_abb->SetBackgroundColour(wxColour("#ff0000"));
    my_uber = new wxButton(panel2, Button_uber_bearb, sp_text[11]);
  //  button_sizer2->Add(new wxButton(panel2, Button_uber_bearb, sp_text[11]), 0, wxALL, 10);
    my_uber->SetBackgroundColour(wxColour("#" + strcolor));
    button_sizer2->Add(my_uber, 0, wxALL, 10);
    if (ord_wnum == "yes")
    {
        button_sizer2->Add(new wxButton(panel2, Button_order, sp_text[57]), 0, wxALL, 10);
    }
    else
    {
        button_sizer2->Add(new wxButton(panel2, Button_order, sp_text[56]), 0, wxALL, 10);
    }
    if (nmchan == "yes")
    {
        my_ver = new wxButton(panel2, Button_mark, sp_text[110]);
    
    }
    else
    {
        my_ver = new wxButton(panel2, Button_mark, sp_text[109]);
      
    }
    my_ver->SetBackgroundColour(wxColour("#"+notcolor));
    button_sizer2->Add(my_ver, 0, wxALL, 10);

    button_sizer2->Add(new wxButton(panel2, Button_clear, sp_text[111]), 0, wxALL, 10);
    button_sizer2->Add(new wxButton(panel2, Button_num, sp_text[112]), 0, wxALL, 10);
    button_sizer2->Add(new wxButton(panel2, Button_add_but, sp_text[114]), 0, wxALL, 10);
    button_sizer2->Add(new wxButton(panel2, Button_cle_but, sp_text[115]), 0, wxALL, 10);
    //spez_butsizer->Add(new wxButton(panel3, Button_cle_but, sp_text[115]), 0, wxALL, 10);
    lower_sizer->Add(button_sizer2);
   // lower_sizer->AddSpacer(7);
   // lower_sizer->Add(spez_butsizer);
    //lower_sizer->AddStretchSpacer(wxALL);
    panel2->SetSizerAndFit(lower_sizer);
    panel2->Layout();
   spez_butsizer->AddSpacer(7);
    panel3->SetSizerAndFit(spez_butsizer);

    panel3->Layout();
    mainSizer->Add(panel2);
    mainSizer->Add(panel3);
	mainSizer->SetMinSize(wxSize(w, h));


panel->SetSizer(mainSizer);
panel->SetScrollbar(wxHORIZONTAL, wxDEFAULT, 10, 100, true);
panel->SetScrollbar(wxVERTICAL, wxDEFAULT, 100, 10, true);
panel->SetScrollRate(150, 150);
panel->Layout();
my_abb->SetFocus();
}

Wolfgang
I live to help wx-kind
I live to help wx-kind
Posts: 174
Joined: Mon Jan 28, 2019 8:22 am

Re: Focus problem

Post by Wolfgang » Tue Oct 22, 2019 3:21 pm

As said everything works as long as I do not use the bind, with the bind there is a problem.

User avatar
doublemax
Moderator
Moderator
Posts: 15079
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Focus problem

Post by doublemax » Tue Oct 22, 2019 4:46 pm

Thanks, that looks fine. How does the MyFramebearb::Onsetf method look like?
Use the source, Luke!

Post Reply