Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a .txt with edtiboxs current values 1

Status
Not open for further replies.

Filip Brnic

Programmer
Dec 25, 2023
39
0
6
RS
I want a button that creates a .txt, lets me name it and lets me choose where to save it, but it has the data (words numbers whatever,) that i've typed in my edit box. So basically i have a form and i have like a concept of typing daily in the editbox and at the end of the day having it saved to txt, regardless chatgpt is useless, and i dont understand how propertys and methods work, i kinda do but i cant make them myself, can someone just do it with a File to str, or something
the editbox goes by thisform.edit1. so please dont send me messages replace this replace this, if its not necesseary becasue i am a beginner so i dont really know that much, but i had this idea for a long time... Thank you guys :)


 
Filip,
Well, there are a number of ways to attack this.
But Tek-Tips isn't a place to "get someone to do the work for you". What we will do is help you understand how to achieve what you're intent is.
First, you mention you want to save the file to any name you want. There are a lot of ways to do this, but one simple way would be to have a field above your edit box (I assume you are saving that content in to a memo field in a table? Or are you just typing text, and at "the end of the day" you want to print that. There is a potential problem here, if you close that form, or exit the application, that data isn't saved. So another thing you want to do if you haven't already is put a table behind that field, and bind the edit box to the field in the table. Since you want free-form text, I suggest using a memo field.
Also, if you want rich text editability, you can implement RTF controls. There is a good example of this in the VFP sample files. I suggest look at that and understand what is going on there, if this is of interest (color, font size, font property control, etc. is available here).

If you just want plain text, then that's easier, you just bind the memo field to the edit box on your form.
Getting back to that, have a field above it that is your file name. You could even get that to be generated automatically, and either editable, or fixed. I would save this name with the data for the detail in your table as well, but it's optional. Place a text box on the form above or below your edit box, and then in the "init" method you could set something like: This.Value = "Save Data"+ ALLTRIM(DTOS(DATE()). The field will then default to this, and since it's also an editable field, you can change it if you wish. I recommend this over using a dialog box for simplicity, but you can play with other ways to getting either a VFP or Windows dialog if you want to navigate your system, and enter values, otherwise, you can set an output directory in a similar way. I would avoid the old "hard coded value", and instead do something similar with your file name filed. Create another text box that is "Location", then default it to where you want it to go, for example: in the init of that text box you could have: This.Value = "C:\Users\(your user director)\Documents".

Now, add your "save data" button onto your form. In the Click Event, you would put in code like this:

Code:
 LOCAL lnFileHandle, lcText, lcSavePath

lcSavePath = ALLTRIM(Thisform.PathField.Value)+ALLTRIM(ThisForm.FileNameField.Value)

*-- Get the text from the edit box
lcText = Thisform.EditBox.Value

*-- Create the file
lnFileHandle = FCREATE(lcSavePath)
IF lnFileHandle < 0
    MESSAGEBOX("Unable to create file.")
    RETURN
ENDIF

*-- Write the text to the file
= FWRITE(lnFileHandle, lcText)

*-- Close the file
= FCLOSE(lnFileHandle)

On the point of "I don't know how properties and methods work", let's address that.
Think of object properties as you would of "worldly objects". Let's take a ball. The "object" is the ball. But not all balls are equal. What color is the ball? Well, a "ball" object's color proprty would be set like this: Ball.Color = "Purple" Note that the .color is the "property" of the object. In object oriented programing, properties are referenced by .<something>, or a sequence of things like, homefield.ball.color() -> "Red" says the color of the ball in the homefield is Red. This is how to address properties that are embedded, like ThisForm.Editbox.Value says "on this form, in the edit box, what is the value?)

Methods and Events are an opportunity to get your form to "do something". All events are methods, but not all methods are events. The key difference here is, events happen when an "event" occurs. Such as, moving your mouse into a field, may cause something to occur (the event "triggers" some code to execute. You will notice in the EditBox object (and most form objects) uder the "Methods tap" something called "Mouse Enter". This is an event, and if you edit that event you can put code into that event that will run when the mouse enters the field. You will note events under the method tab by the lighting bolt symbol at the far left. Methods on the other hand are your opportunity to "call" code when you want it. ZOrder is a good example of a Method. It does not happen unless you "call it" in some other code, either in an event or a method. As a practice you should never CALL events, as this can result in some odd behaviors (especially around Refresh(), Valid() and INIT().

It's useful to study these "chain of events", especially for forms, you can save yourself a lot of headaches by knowing what events occur first, and then the cascading order.
This should get you started. As you encounter some further issues, most of us here are happy to help. But help us help you.

Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD

"I try to be nice, but sometimes my mouth doesn't cooperate.
 
Nice work (and explaining), Scott. Is there any reason why you didn't use STRTOFILE() instead?
 
Alright thank you, very professional answers, i was thinking this was a laid back site but im glad ive found it, you guys know your stuff!!!
Im 16 and my further familly is preparing me for working in vox (my dead grandads) program, so basically im trying alot of different stuff right now, and ive been working on a program that like managaes caffees, and im curious if someone has had a project like that and has any ideas what could i add
 
Filip said:
I want a button that creates a .txt, lets me name it and lets me choose where to save it, but it has the data (words numbers whatever,) that i've typed in my edit box.
...
the editbox goes by thisform.edit1

The line of code you need in the button.click event is
Code:
STRTOFILE(Thisform.edit1.value, PUTFILE('','','*.txt'))
Edit: Important, I had these two parameters backwards, first parameter is what to save, i.e. the value of the editbox, second parameter is the file name.

Filip said:
please dont send me messages replace this replace this, if its not necesseary becasue i am a beginner so i dont really know that much
I fear I already did, but how could you know what to do, if you don't get told? I think you fear not understanding the instructions, if they are too brief, but also, when they are too verbose and full of things you don't know that.

Beginning with programming is overwhelming, nobody can protect you from that, sorry.

To get a bit less brief, let's go some steps back tto where you are: You have put an editbox onto a form, that ot the name "Edit1", which is normal, yes. And in the first place, this is okay and doesn't need change. I actually assume now, that you alread also put a button on the same form, because that does only differ in picking a button instead of editbox and drag that into the form area.

To get to the click event you have several ways, one very simple, intuitive way is double click on the button. You get a white empty code window and it will be the click event of the button. That's displayed on top of the empty code window:

yourfirstcodeCapture_urrjl2.jpg


You could then run the form and have your first sucess experience.

It's not the worst idea to write more verbose code and also add more to the form as Scott has suggested. There are some things that are not ideal with my short oneliner code. One thing is that you can close the "save as" dialog, in which case the file name will be empty and the STRTOFILE() call will fail. So it is better to do things in multiple lines of code and be ablte to react to all the cases that can be exöpectged to happen. Well, that's actually describing how to think in programming, you alwayss have to go through all the major cases that could happen and think about them.

I don't know if programming is for you, but I don't want to curb your enthusiasm. Maybe it actually sparked just today, that you want to learn programming. Before you continue in VFP specifically, know this isn't a programming language with a bright future, it is a discontinued progtramming language. Also, I don't know if putting an editbox on a form you got the impression and idea, that programming VFP is just visual, dragging objects on design areas. The intuitive way of getting to the click event of a button by just clicking on it is nice, but doesn't work as intuitive and simple foir everything. If you double click on the empty area of the form, you end up with an editor Window of the Load() event of the form. That allows you to write code that runs first, but it's not the natural event to program, you better program something in the Init() of a form, because at that stage of a form all controls on it actuall will also be there when this code runs, while the form is empty when the code in the Load() event is execute.

And that's where things already become unintuitive, not as you'd expect it to work, because of cause you already put all the objects on the form when programming/designign the form, so why is the form empty when it runs? I'll not explain this right now. I just mention this as you need to understand one important thing: Nobody can protect you from getting stumped, not only because we explaiun things with a base knowledge in mind that you don't have yet, but because things are actually not intuitive before you know a bit of everything. Unfortunately no programming language is something you can learn in some path that's builds up knowledge in simple logical steps like learning a natural language or something like driving a car or skiiing. And you very well know learning to drive on the actual streets has some safeties for you as a learner like a driving instructor able to intervent with his own brake.

If you fear to do something wrong then better stop now. What you want in this special case is already programmed. The Notepad editor does that, and one step better it's done in Notepad++, where a new text you start writing is saved, even if you never actually gave it a name. Notepad++ even saves when the computer crashes and will restore the state it ended in with the next start. Texts are stored in tabs and where you never gave a file name, the tab captions will be "new 1", "new 2", , etc.

Also, there is something built into VFP, you can write a text and get back to it by date with a system window that's named 'calendar' and has, well, mainly a calendar on the left form side and an editobox which you can resize in width. If you go to a specific date what you wrote into that is restored. It's based in the idea Scott also sketched, it's store in a table in a memo field. Just into an already existing system dbf of VFP, soo all you need to get a form saving whatever you enter into an editobx per day is
Code:
Activate Window 'calendar'

It's actually also mentioned in the help of VFP, it's not a secret or easter egg of VFP.

Now, as a last step I'll tell you the second best way but also a more generally working way to switch bewtween the visual form design and the code behind all the objects: Move the mouse within the designer window to the white area outside the form and right click there, this will give you a popup context mewnu with one item "Code...", where you switch to some method or event of some objects. Depends on what is selected on the form or what you last edited.

Many people actually dislike this way, because there is no single overview code window with everything. But believe me, it actually forces you to concentrate on one specific thing, one event or method. Right above the code editing "editbox" you can select objects of the form or, at the root and as topmost element, the form itself, And the box right from the object allows you to switch between the methods and events.

I personally think it forces you to focus and concentrate on one thing you're concerned with. When you program what a button click should do, you don't need to see what code executes when the form starts, if at all.

There is a way to get an overview of all, but it's only for that aspect, you couldn't necessarily run that overall code also changing it won't change the individual methods or events of the form or objects on the form . If you don't like this concpet at all, then you better go for seomthing else, like HTML, but even in such a different "genre" of programming that's all on one, you get teached separation, for example defining the design decisions in CSS files and code in JS files and HTML files only concerned with the base HTML strcutrue that only gets it's final look with CSS.

If you seek for something completely visual, I think you won't find anything like that outside of introductory concepts like Scratch is for kids to teach programming in a visual way or like Logo was in earlier days for programming how a turtle walks. You won't be able to create fully fledged applications with such concepts, though.

I also actually thank you for pointing out that using ChatGPT to get some code can't replace all programming skills, even though Scott said: "If I had written this myself it would have taken hours to days for me to work out, not that I'm a VFP/WinAPI genius, but that's part of the point.".

If you're a novice in programming, you will not know what to do with a piece of code. And just like a single prompt will not give you the code for a full application, you also won't learn hopw to program in one question to a forum. So get used to having more questions than you started with, after learning one step towards your goal. That's not a problem, that's normal. Not only, if you are a beginner.

Chriss
 
Hey Chris, ill definetly try this, although saying im a beginner wasnt actually that true, im just a beginner compared to like people like you, ive already gone over the basics, now i just wanna learn to connect vfp with other things like txts. sites. videos, links, download something, etc...

Regardless ill try to record my program/project so u guys have a better idea of what i have and what i want, but regardless thank yuo.
 
Coming back to this:

Code:
STRTOFILE(Thisform.edit1.value, PUTFILE('','','*.txt'))

If you only want to be asked for where to save with which name once, you should put the result of the function PUTFILE() into a property, ideally of the editbox, because when extending the form to have multiple tabs each with an editbox you could still remember each filename directly at the editbox that contains the saved text.

For this you would need to use the class designer to add a filename property to your own editbox class and use that instead of the normal editbox. It's not that complicated, but as I know that could stress you out, you could add a property, like this, in the form INIT() event:

Code:
AddProperty(Thisform.Edit1,'Filename','')

Then change the save button CLICK() event this way:
Code:
If Empty(Thisform.Edit1.Filename)
   Thisform.Edit1.Filename = PUTFILE('','','*.txt')
Endif

If Empty(Thisform.Edit1.Filename) && the choice/answer of the user could still be empty
   MessageBox('Without a file name, the text can't be saved.')
   * you could instead generate a random filename here
   * but would also need to assure it is in a directory the user can save to.
Else
   Local lcCurrentSafetySetting
   lcCurrentSafetySetting = Set('Safety')
   Set Safety Off && to be able to also overwrite the file, if it already exists,
   * i.e. to safe the file again under the same name as given when first time asking for it.
   StrToFile(Thisform.Edit1.Value, Thisform.Edit1.Filename) 
   If Lower(lcCurrentSafetySetting)=='on'
      Set Safety On && set it on again, if it was on
   Endif
Endif

This is still only for one Edit1 editbox on the form, if you had many, i.e. using a multiple page pageframe, that would need to adapt the editbox addressed to the currently activated page editbox., for example Thisform.Pageframe1.Pages(Thisform.Pageframe1.ActivePage).Edit1 and then the form init code would also need to add the filename property to all pages editboxes.

Also notice I added the necessary code for overwriting the file when a user saves after having saved and then again changed the editbox, without being asked for yet another filename. Notice this mechanism is not programmed very intelligently, even if the user chooses a filenmae in the dialog caused by PUTFILE(), that fie would be overwritten, even if is a valuable text. You could blame the user to pick an already existing name, but it would be more to the point to add a question to the user, whether he really meant to overwrite that file, directly after picking the filename with PUTFILE. I'll leave that to you, to do. Once that choice has been confirmed and the user clicks the save button again, he'll only overwrite what he last saved from the editbox and it's opkay to assume he wants to overwrite that.

You're obviously free to do whatever you want, so a feature you could implement is copy a file to an application data directory before overwritring it to keep all versions of the file and then over to go back to previous states. VFP does not provide an undo history, the CTRL+Z feature of Windows itself is still available within a VFP editbox, but doesn't work in the sense of a savepoint undo history. Also notice, VFPs own system menu defines CTRL+R for redo, not CTRL+Y, as is ususal.

So to change that to default behavior you'll also need to define a menu. Which is a good idea anyway. There should be a FIle menu with menu items to "Save As", which always prompts for a file name, and "Save", which forwards to "Save As" when it is selected for first saving or saves under the name given the last time saving, if the user already used "Save" that way or saved with "Save as" the last time.

You see, there's already a bit more peskier work to do, if you want to comply with Windows standards the user is used to. Even VFP itself doesn't provide the usual Widnows standard behavior using CTRL+R instead of CTRL+Y for redo within the VFP IDE. It'll even be worse if you don't provide a file menu with Undo/Redo menu items. Then the editbox will react to key combinations like CTRL+Z,Y,C,X,V etc by writing a "box" character and not doing the default Windows actions, which is something that other programming languages do better by still complying to defaults, if nothing else is specified by a programmer.

Chriss
 
DOWNLOAD THE VIDEO FIRST OF COURSE

Okay guys, i wanted to show you what im even doing, basically i have the following,

Main form, on that form there are commandgroups,
1 command group is for 1 Caffee (imagine being an owner of multiple)
There you can choose if you want to go to the storage->magacin
to the table->sto
or if you want to check about the caffee -> osnovni podaci

So lets say you work at a caffee and you have someone order,
0:24 is the main program that would be always open while someone was working on that,
so basically sto 1.2.3 etc are tables.
0:26 When we press on the little icon, we open a form thats for calculating the receipt or adding to the receipt or whatever

There we have to first Confirm table number, by doing so the form caption changes to sto (table) + n (number) so you have sto 1 sto 2, and basically for easier managing, as so on 0:33 the message is just a "succesfully created a table"
next, on the form we see drinks to the right, by clicking on a drink you can add them to the (naruceno) ordered.
Every click we add 1 drink the quanitity of it goes down by 1, so you are losing them in stock, so what i did was i made a blue button (ocisti polja) which empties the fields and returns the values to the first ones when opening the form. Ive done that by having a textbox init have this.value = thisform.textlemonade.value, (those txtboxes are hidden and not visible), but when we check the Storage/magacin, we can refesh it and see the values go down.

About this i just wanted anyone to tell me if thats reliable way of using it instead of tablerevert because i had a harder time making that work,but basically yea it does the job for me for now...

Of course this isnt anything good, nor am i really proud of it, im just triyng to learn by making something creative/usable, and id like to take this concept to the next level by implimanting like pui/receipt prints, more managment and etc...

But lets get back to something i was curious, if you see on 0:24 there is a label PODSETNIK (its a reminder of whats happening) when you click on the talbe you have the reminder say, table1 ordered, when you close the form and press on the icon again you have the reminder say, table has been served, and then you have the cash icon which is when someone pays, they work hand in hand making each visible/not visible.
next to it we have an empty editbox 2 textboxes and a command,
2 textboxes are filled with what Scott24x7 told me to put and the commnad as well, except when i use the command it gives me the error thats build in error messagebox in the command.

It might be me or the ecode but i want to explain now what was my intent,
I wanted it just to evident something im doing so lets say, there is 10 orders a day, i write each one there, and then transfer that to a .txt
that txt being names that date, so i have a clear list of everyday, so if i have to check if something is out of ordinary i can just check .txt file and read what has been ordered troughout the day, basically giving me the history of orders that day,
What might be a better idea is to make it when i press on the (0:37) sto je placen - (the table has paid) and then it takes all the fields and prices and makes them into a report, that would be also good idea, but anyway im sorry if im not clear on what i want or how i want it, im serbian so makes communication a little tough, but its fine i hope you guys like what i did, i found it unique, it was fun making it and i want to make it go alot more, make reports, implement excel files, import export excel, import export txts, i have alot of ideas, but let me hear you guys what do you think :D Im more then happy to learn and am willing to pursue working in fox to keep its legacy going!
 
 https://files.engineering.com/getfile.aspx?folder=350d52e3-f35e-4541-8bcc-c2cc7542764c&file=bandicam_2023-12-26_13-05-59-676.mp4
Dan,
As I mentioned, there are multiple ways to attack this.
Filip mentioned he's a beginner.
I like to show the low-level functions in this case, because they are fundamental to many aspects of VFP, and make solutions limitless.
We could have used COPY TO, STRTOFILE(), SET ALTERNATE TO <filename>, CREATE TEXT - <filename>.
There's lots of ways to do this.


Best Regards,
Scott
MSc ISM, MIET, MASHRAE, CDCAP, CDCP, CDCS, CDCE, CTDC, CTIA, ATS, ATD

"I try to be nice, but sometimes my mouth doesn't cooperate.
 
On the level of croatian (I guess) videos, we don't get forward and I don't get an impression of what you want to achieve at all.

Sorry, I can't help you even with giving you an idea of how to do something if I don#t even have the vaguest idea of what you try to achieve.

What I think you aim for is recording what happens in a POS system during a day. Well, then a text file is the least safest way, as it is the easiest to manipulate file format you could choose at all. Many countries have laws and regulations with the aim of reducing or preventing manipulations of POS systems and they are based on complex algorithms and cryptography.

Anyway, the best advice I think I can give at this point is that you look for a croation (or other language/country) community of programmers, not just only FoxPro, to discusss such concepts and I think you could also turn to your own countries government, tax office or other places concerned with such things, that could point ot the regulations and technical standards for POS systems in your country. One thing is for sure, though, a text file won't give you a safe way of verification, such things are perhaps still best done with video recording. I know big stores use a camera recording of POS systems from above to verify the payment interaction by video, if in doubt.

If you do something just for yourself and your own requirements are that low, then nodbody will hinder you to write out texts, you know now how to do in several ways, already. You're getting an error, it's even easy to reproduce, then just post the error message and in which line of code it appears and we could help with that error. If you wanted to show the whole process for some reason, please expand your explanation, I don't see what I should get from knowing all that for your final actual question and problem.

By the way, I don't even identify a single editbox that relates to your first question, I don't see a prompt for a file name or a place a user could enter that, maybe you included that into the code, because you just want to save something to a txt without the user knowing that, well, you likely forgot something important, like the user still needs to have the permission to create the file in the place you want it to be stored. Which also means he'll always have access to it, no matter if you "hide" it in some place. Also, if you don't specify a directory before the actual file name, the STRTOFILE() function would save into the current directory. That might be very likely a place the user can't write into, it could be the direcory of the EXE in program files, where write access is forbidden.

Sorry, you were informed, even if neither Scot, Dan or me pointed that out explicitly, Scott and I told you to specify a directory. I suggested to use the PUTFILE() function, which opens a dialog to pick not only a file name but also a directory. The user could still pick a place he can't write to, but then it's a user fault, not program fault. I also already warned you the result of asking the user could be empty and you would need to cater for that case, if in doubt don't save or ask for a working filename and directory.

Chriss
 
Yes i did some research, i am trying to make a POS system for a caffee/restourant, is there any guides or tutorials on how the concept should be??
 
You seem to only think about the requirements of a shop. A POS system is registering an audit trail of all data related to sales and is all data of major interest for tax offices. Inform yourself about regulations that are in effect in your countries, i.e. talk to the financial offices. If you think the topic of tax declaration is outside the scope of programmnig a POS system, you may be right in many countries, but surely not in all, there are regulations valid for the EU, some for only single countries. So that alone makes it a hard task to implement a system that you later could sell internationally. Besides all the competition of POS systems already available.

Here's that topic on the surface:

There's unfortunately not one system that's working internationally. Some examples are
Germany: KassenSichV, GoBD
Austria: RKSV

In the best case for you such regulations can be fulfilled by hardware, i.e. a fiscal module will do all the stuff necessary for later tax declarations or audits. But if I were you I'd inform myself before starting to implement the core POS, this task alone is far harder and more demanding than any other aspect of a POS software and even if you solve it by a hardware module, learning how to use that could be a major hurdle and also rise the costs for your customer(s).

What makes this topic so important for the countries/regions that require it is that it has to be some verifiable cryptographic signature encoded in a QR barcode on every receipt. And don't assume on the lose subjects like dta security it's your own choice how to implement that aspect, the tax offices at least of some countries have very high demands in that aspect of verifying the payment transactions, they will actually demand the full transaction log of that and they mke requirements that make tampering with it recognizable.

So, when your motivation to write a POS is that you have a great idea about how the user interface is simplified for a business like a coffee shop, that's fine, but that's the least hard part of a software that is inn this zone of interest of tax offices and thus governments. You're dealing with payments, that's not only business critical for the shop owners, there are also interests of the government and in the future it will not get simpler, you may need to get a certificate to have a qualified system that can actually be used, and the older and bigger players and vendors of such software will likely have it esier to cope with such requirements, as costs for that get low per license sold, when they already have a larger cusotomer base. Also, I don't assume you'll be the first one specialisiing on this, there are POS systems speciallising on groceries, clothing stores, gas stations, restaurants, cinemas, etc. etc.

You try to enter a really saturated market.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top