need help on using WxTextCtrl to read line by line and save 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.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

and by the way i change the
wxwidgets:
EVT_KEY_DOWN(_2Frame::OnChar)
to
wxwidgets:
EVT_KEY_DOWN(wxTextCtrl::OnChar)

it compiles well with ot any error !but actually it doesnt function as it is supposed to!( its like ive never used that function!!)
No, it's the opposite you need to do. You need to change

Code: Select all

void wxTextCtrl::OnChar(wxKeyEvent& event)
{
to

Code: Select all

void _2Frame::OnChar(wxKeyEvent& event)
{
Sorry to repeat myself, but I strongly recommend to spend more time learning C++ itself; you may think you master it, but an error like above shows you do not fully understand/master classes and inheritance.
so this is what im talking about ! those standard identifiers are only useful when i ='ve already used sth standard, am i right? i believe i saw sth on the online documentation wiki! about "::wxAboutBox" , if i only use this with standard identifier, they would change among platforms! right! so that is the use of standard identifiers !
I'm still not sure what you mean. If by "standard identifiers" you mean IDs like wxID_ABOUT, I think they are only really used on wxMac, where some menu items have pre-definite locations; so using the right ID allows you to use the right location. Apart from that i don't think there's any use.
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Post by Hossein »

thanks , as you've just mentioned , im just a rookie in classes ! ( im a newbie in C++ ) im learning C++ on my own using Deitels C++ how to program ( and so far , ive reached chapter 8! ) ( a deeper look in classes ! is in chapter 10 to 11 i think! i havent reached that! )
tell me if it is essential to read them now .
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
TrV
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 630
Joined: Wed Jul 04, 2007 1:12 pm

Post by TrV »

The essence of C++ is class concept, unless code in C !
Understanding of class, object (/instance), public/private necessity, inheritance and some other very important stuff like constructors is mandatory before to use wxWidgets or other advanced OOP frameworks/libraries.
I repeat : this is mandatory.
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Post by Hossein »

im writing this in case some one else has my problem :
many tanx to dear T-rex for teaching and helping me :)

Code: Select all

void YourFrame::OnSHOWLINESClick( wxCommandEvent& event )
{
    wxArrayString lines;
    for(int i = 0; i < TextCtrl1->GetNumberOfLines(); i++)
    {
        lines.Add(TextCtrl1->GetLineText(i));
        wxMessageBox(TextCtrl1->GetLineText(i));
    }

}
and for implementing Open/save/Save As/ New/Close file ( for working with TextCtrl use the following :

Code: Select all

void YourFrame::NewFile(wxCommandEvent& WXUNUSED(event))
{
	// Clear the edit box
	TextCtrl1->Clear();
	// reset the path of our current open file
	CurrentDocPath = wxT("C:/");
	// Set the Title to reflect the file open
	SetTitle(_("Edit - untitled *"));
}

void YourFrame::OpenFile(wxCommandEvent& WXUNUSED(event))
{
	wxFileDialog *OpenDialog = new wxFileDialog(this, _("Choose a file to open"), wxEmptyString, wxEmptyString,_("SML files (*.sml)|*.sml|Binary files (*.bin)|*.bin|Text files (*.txt)|*.txt|C++ Source Files (*.cpp, *.cxx)|*.cpp;*.cxx|C Source files (*.c)|*.c|C header files (*.h)|*.h"),	wxFD_OPEN, wxDefaultPosition);

	// Creates a "open file" dialog with 4 file types
	if (OpenDialog->ShowModal() == wxID_OK) // if the user click "Open" instead of "cancel"
	{
		CurrentDocPath = OpenDialog->GetPath();

		// Sets our current document to the file the user selected
		TextCtrl1->LoadFile(CurrentDocPath); //Opens that file
		// Set the Title to reflect the  file open
		 OpenDialog->GetFilename();
		 //SetTitle(wxString(("Edit - "))<<
	}
}

void YourFrame::CloseFile(wxCommandEvent& WXUNUSED(event))
{
	// Clear the Text Box
	TextCtrl1->Clear();
	// Reset the current File being edited
	CurrentDocPath = wxT("C:/");
	// Set the Title to reflect the file open
	SetTitle(_("Edit - untitled *"));
}

void YourFrame::SaveFile(wxCommandEvent& WXUNUSED(event))
{
	// Save to the already-set path for the document
	TextCtrl1->SaveFile(CurrentDocPath);
}

void YourFrame::SaveFileAs(wxCommandEvent& WXUNUSED(event))
{
	wxFileDialog *SaveDialog = new wxFileDialog(this, _("Save File As _?"), wxEmptyString, wxEmptyString,_("SML files (*.sml)|*.sml|Binary files (*.bin)|*.bin|Text files (*.txt)|*.txt|C++ Source Files (*.cpp)|*.cpp|C Source files (*.c)|*.c|C header files (*.h)|*.h "),wxFD_SAVE | wxFD_OVERWRITE_PROMPT, wxDefaultPosition);

	// Creates a Save Dialog with 4 file types
	if (SaveDialog->ShowModal() == wxID_OK) // If the user clicked "OK"
	{
		CurrentDocPath = SaveDialog->GetPath();
		// set the path of our current document to the file the user chose to save under
		TextCtrl1->SaveFile(CurrentDocPath); // Save the file to the selected path
		// Set the Title to reflect the file open
		SaveDialog->GetFilename();
		//SetTitle(wxString("Edit - ") <<
	}
}
dont forget to include the proper header files!
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
gtafan
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 201
Joined: Wed Mar 29, 2017 9:52 am

Re:

Post by gtafan »

Hossein wrote:im writing this in case some one else has my problem :
many tanx to dear T-rex for teaching and helping me :)

Code: Select all

void YourFrame::OnSHOWLINESClick( wxCommandEvent& event )
{
    wxArrayString lines;
    for(int i = 0; i < TextCtrl1->GetNumberOfLines(); i++)
    {
        lines.Add(TextCtrl1->GetLineText(i));
        wxMessageBox(TextCtrl1->GetLineText(i));
    }

}
This code looks interesting, but in the wxWidgets Reference I read, that GetNumberOfLines() shouldn´t be used:

Image
lfjking
Earned some good credits
Earned some good credits
Posts: 102
Joined: Mon Nov 14, 2016 1:35 pm

Re: need help on using WxTextCtrl to read line by line and save

Post by lfjking »

I thought you want to show the cmd on the textctrl and get the code to the cmd to run?
if right, you can chang to use wxExecute():http://docs.wxwidgets.org/3.1.0/group__ ... 1b73399b33

One TextCtrl only show the cmd.
One TextCtrl to get the code to cmd.
Post Reply