Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with my almost done GUI Prog
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Coog03




PostPosted: Sun Jun 15, 2003 12:15 am   Post subject: Help with my almost done GUI Prog

Well I have got my GUI program working pretty good, ?I would appreciate any help in making this program any better.

Here is the code you'll have to create a names.txt file and a temp.txt file as well as change the path to the files.

code:

%---------Must be included into programs using The GUI Module-----------------------------------%
import GUI in "%oot/support/lib/GUI"
%---------Set Window Size to 780x475-----------------------------------------------------------%
View.Set ("graphics:780;475")
%---------Set Background Color-----------------------------------------------------------------%
GUI.SetBackgroundColor (30)
/*##############################################################################################
 ########################### GLOBAL VARIABLES ##################################################
 #############################################################################################*/
%---------Menu Variables-----------------------------------------------------------------------%
var first, second, third, fourth : int
%---------Menu Items Variables-----------------------------------------------------------------%
var menuItem : array 1 .. 10 of int
%---------Menu Item Names----------------------------------------------------------------------%
var menuName : array 1 .. 10 of string := init
%---------File Menu Items----------------------------------------------------------------------%
    ("New Database", "Open Database File", "Close Database File", "---", "Exit Program",
%---------Records Menu Items-------------------------------------------------------------------%
    "Add Records", "View Records", "Sort Records",
%---------About Menu Items---------------------------------------------------------------------%
    "About LDS", "User Manual")
%---------File Variables-----------------------------------------------------------------------%
var file, temp : int
%---------Text Field Variables-----------------------------------------------------------------%
var titleTextField, firstnameTextField, lastnameTextField, valueTextField,
    genreTextField, keywordTextField, subjectTextField : int
%---------Font Variables-----------------------------------------------------------------------%
var title, title2, main, small : int
title := Font.New ("arial:8:bold")
title2 := Font.New ("arial:10:bold")
main := Font.New ("arial:10")
small := Font.New ("arial:8:italic")
%---------Data Variables-----------------------------------------------------------------------%
var Data :
    record
        title : string
        firstname : string
        lastname : string
        value : string
        genre : string
        keyword : string
        subject : string
    end record
/*##############################################################################################
 ########################### PROCEDURES ########################################################
 #############################################################################################*/
%---------Called When Menu Items are Selected--------------------------------------------------%
procedure MenuSelected
    for i : 1 .. 10
        if menuItem (i) = GUI.GetEventWidgetID then
            locate (maxrow, 1)
            put "Menu Item \"" + menuName (i) + "\" selected         " ..
        end if
    end for
end MenuSelected
%---------Called When User Selects Open Database File------------------------------------------%
/*procedure OpenFile
 var data : string
 open : file, "a:/names.txt", get %open file to get previous data
 open : temp, "a:/temp.txt", put % open the temporary file to store previous data and get more
 loop
 exit when eof (file) %exits when you get to the end of the file
 get : file, data %get the data from the old file
 put : temp, data %write to the temp file
 end loop
 end OpenFile*/
%---------Called When User Selects Close Database File-----------------------------------------%
/*procedure CloseFile
 close : file
 close : temp
 File.Delete ("a:/names.txt") %deletes names
 File.Rename ("a:/temp.txt", "a:/names.txt") %rename temp to names
 end CloseFile*/
%---------Called When User Selects Add Book----------------------------------------------------%
procedure AddBook
    var data : string
    var errormessage : string := ""
    var valid : boolean := true
    %set values in text boxes to data record
    Data.title := (GUI.GetText (titleTextField))
    Data.firstname := (GUI.GetText (firstnameTextField))
    Data.lastname := (GUI.GetText (lastnameTextField))
    Data.value := (GUI.GetText (valueTextField))
    Data.genre := (GUI.GetText (genreTextField))
    Data.keyword := (GUI.GetText (keywordTextField))
    Data.subject := (GUI.GetText (subjectTextField))
    open : file, "a:/names.txt", get     %open file to get previous data
    open : temp, "a:/temp.txt", put     % open the temporary file to store previous data and get more
    loop
        exit when eof (file)     %exits when you get to the end of the file
        get : file, data     %get the data from the old file
        put : temp, data     %write to the temp file
    end loop
    %save data to temp.txt
    put : temp, "Title: " + Data.title
    put : temp, "Author's Name: " + Data.firstname + " " + Data.lastname
    put : temp, "Book Value: " + Data.value
    put : temp, "Genre: " + Data.genre
    put : temp, "Keyword: " + Data.keyword
    put : temp, "Subject: " + Data.subject
    put : temp, repeat ("-", 30) + "Book #", entrynum , repeat ("-", 30)
    %Clear Text Fields
    GUI.SetText (titleTextField, "")
    GUI.SetText (firstnameTextField, "")
    GUI.SetText (lastnameTextField, "")
    GUI.SetText (valueTextField, "")
    GUI.SetText (genreTextField, "")
    GUI.SetText (keywordTextField, "")
    GUI.SetText (subjectTextField, "")
    GUI.SetActive (titleTextField)
    close : file
    close : temp
    File.Delete ("a:/names.txt")     %deletes names
    File.Rename ("a:/temp.txt", "a:/names.txt")     %rename temp to names
end AddBook
%---------Called When User Selects Add Records------------------------------------------------%
%When the user hits [enter] key, the program skips to the next textbox
procedure titleEntered (text : string)
    GUI.SetSelection (firstnameTextField, -1, -1)
    GUI.SetActive (firstnameTextField)
end titleEntered
procedure firstnameEntered (text : string)
    GUI.SetSelection (lastnameTextField, -1, -1)
    GUI.SetActive (lastnameTextField)
end firstnameEntered
procedure lastnameEntered (text : string)
    GUI.SetSelection (valueTextField, -1, -1)
    GUI.SetActive (valueTextField)
end lastnameEntered
procedure valueEntered (text : string)
    GUI.SetSelection (genreTextField, -1, -1)
    GUI.SetActive (genreTextField)
end valueEntered
procedure genreEntered (text : string)
    GUI.SetSelection (keywordTextField, -1, -1)
    GUI.SetActive (keywordTextField)
end genreEntered
procedure keywordEntered (text : string)
    GUI.SetSelection (subjectTextField, -1, -1)
    GUI.SetActive (subjectTextField)
end keywordEntered
procedure subjectEntered (text : string)
    GUI.SetSelection (titleTextField, -1, -1)
    GUI.SetActive (titleTextField)
end subjectEntered
procedure AddRecords
    %Text Fields
    titleTextField := GUI.CreateTextFieldFull (100, 355, 275, "",
        titleEntered, GUI.INDENT, main, 0)
    firstnameTextField := GUI.CreateTextFieldFull (100, 325, 125, "",
        firstnameEntered, GUI.INDENT, main, 0)
    lastnameTextField := GUI.CreateTextFieldFull (250, 325, 125, "",
        lastnameEntered, GUI.INDENT, main, 0)
    valueTextField := GUI.CreateTextFieldFull (100, 280, 275, "",
        valueEntered, GUI.INDENT, main, 0)
    genreTextField := GUI.CreateTextFieldFull (100, 250, 275, "",
        genreEntered, GUI.INDENT, main, 0)
    keywordTextField := GUI.CreateTextFieldFull (100, 220, 275, "",
        keywordEntered, GUI.INDENT, main, 0)
    subjectTextField := GUI.CreateTextFieldFull (100, 190, 275, "",
        subjectEntered, GUI.INDENT, main, 0)
    %Labels
    var titleLabel := GUI.CreateLabelFull (90, 357, "Title", 0, 0,
        GUI.RIGHT, title)
    var authornameLabel := GUI.CreateLabelFull (90, 327, "Author's Name", 0, 0,
        GUI.RIGHT, title)
    var firstnameLabel := GUI.CreateLabelFull (180, 307, "First Name", 0, 0,
        GUI.RIGHT, small)
    var lastnameLabel := GUI.CreateLabelFull (340, 307, "Last Name", 0, 0,
        GUI.RIGHT, small)
    var valueLabel := GUI.CreateLabelFull (90, 282, "Book Value", 0, 0,
        GUI.RIGHT, title)
    var genreLabel := GUI.CreateLabelFull (90, 252, "Genre", 0, 0,
        GUI.RIGHT, title)
    var keywordLabel := GUI.CreateLabelFull (90, 222, "Keyword", 0, 0,
        GUI.RIGHT, title)
    var subjectLabel := GUI.CreateLabelFull (90, 192, "Subject", 0, 0,
        GUI.RIGHT, title)
    %Add Book Button
    var AddButton := GUI.CreateButton (200, 130, 100, "Add Book", AddBook)
end AddRecords
%---------Called When User Selects View Records------------------------------------------------%
procedure DoNothing
end DoNothing
procedure SortButtons
    var titlesort := GUI.CreateButton (490, 370, 150, "Title", DoNothing)
    var firstnamesort := GUI.CreateButton (490, 340, 150, "Author's First Name", DoNothing)
    var lastnamesort := GUI.CreateButton (490, 310, 150, "Author's Last Name", DoNothing)
    var valuesort := GUI.CreateButton (490, 280, 150, "Value", DoNothing)
    var genresort := GUI.CreateButton (490, 250, 150, "Genre", DoNothing)
    var keywordsort := GUI.CreateButton (490, 220, 150, "Keyword", DoNothing)
    var subjectsort := GUI.CreateButton (490, 190, 150, "Subject", DoNothing)
end SortButtons
procedure ViewRecords

    var textBox : int         %Textbox
    var line : string         %Line
    %Open File
    open : file, "a:/names.txt", get
    % Create the title label and text box.
    textBox := GUI.CreateTextBoxFull (20, 10, 460, 420,
        GUI.INDENT, main)
    % Read the file and place it in the text box.
    loop
        exit when eof (file)
        get : file, line : *
        GUI.AddLine (textBox, line)
    end loop
    close : file         % Close the file.
    SortButtons
end ViewRecords
/*##############################################################################################
 ########################### MAIN PROGRAM ######################################################
 #############################################################################################*/
%---------File Menu----------------------------------------------------------------------------%
first := GUI.CreateMenu ("File")
menuItem (1) := GUI.CreateMenuItem (menuName (1), MenuSelected)         %New
menuItem (2) := GUI.CreateMenuItem (menuName (2), MenuSelected)         %Open
menuItem (3) := GUI.CreateMenuItem (menuName (3), MenuSelected)         %Close
menuItem (4) := GUI.CreateMenuItem (menuName (4), MenuSelected)         %---
menuItem (5) := GUI.CreateMenuItem (menuName (5), GUI.Quit)         %Quit
%---------Records Menu-------------------------------------------------------------------------%
second := GUI.CreateMenu ("Records")
menuItem (6) := GUI.CreateMenuItem (menuName (6), AddRecords)         %Add Records
menuItem (7) := GUI.CreateMenuItem (menuName (7), ViewRecords)         %View Records
menuItem (8) := GUI.CreateMenuItem (menuName (8), MenuSelected)         %Sort Records
%---------About Menu---------------------------------------------------------------------------%
third := GUI.CreateMenu ("About")
menuItem (9) := GUI.CreateMenuItem (menuName (9), MenuSelected)         %About LDS
menuItem (10) := GUI.CreateMenuItem (menuName (10), MenuSelected)         %User Manual
loop
    exit when GUI.ProcessEvent
end loop
%GUI.SetText (widgetID,"") Clear textfiled text
%GUI.ClearText (boxname)  Clear Text Box text
%(GUI.GetText (Num1TextField)) Get text from a text field


Thanks in Advance
Sponsor
Sponsor
Sponsor
sponsor
JSBN




PostPosted: Sun Jun 15, 2003 9:38 am   Post subject: (No subject)

code:
put : temp, repeat ("-", 30) + "Book #", entrynum , repeat ("-", 30)

Is giving me brobelems as entrynum has not been declared
Coog03




PostPosted: Sun Jun 15, 2003 11:56 am   Post subject: (No subject)

Yeah I was trrying to work out a way to use an array of records to keep track of the entry number so I coiuld possibly have a delete feauture.. Forgot to take thatr piece of code out, if anyone knows how to keep track of the entry number tell me...just take that part out, keep the dashed lines tho
Andy




PostPosted: Sun Jun 15, 2003 3:11 pm   Post subject: (No subject)

u should attatch something as long as this instead of wasting bandwith
PaddyLong




PostPosted: Sun Jun 15, 2003 3:26 pm   Post subject: (No subject)

Coog... depending on how the file is written (if it is always written using your program) you should look into the read/write parameters for the open command ... they will allow you to read and write entire records into/from your file... however the data is stored in a binary format, so you couldn't just make a file using notepad or whatever (you can view the file with notepad and kind of read the information, but some of it will appear to be incorrect... but when you use the read command in turing it will read the information correctly)

so then if you did do that, at the start of your program you could load the entire file into your array of records with a loop and like exit when eof
Coog03




PostPosted: Sun Jun 15, 2003 9:40 pm   Post subject: (No subject)

ok thats sweet, now I have to figure out how to take the contents of the file and sort it by whatever field the user wants...and by tomorrow morning (its 10:11pm) here

any help on this prob
PaddyLong




PostPosted: Sun Jun 15, 2003 10:03 pm   Post subject: (No subject)

do you know any sort algorithms?

probably the most common (and easiest to understand) one is something like this...
code:

var somearray : array 1 .. 10 of int
var temp : int

%load some random values into the array
for q : 1 .. 10
    somearray (q) := Rand.Int (0, 100)
end for

%the actual sorting..
%essentially compares each element of the array with each element occuring after it
for q : 1 .. 10
    for w : q + 1 .. 10
        if somearray (w) >= somearray (q) then %if the element after it is greater than it
            temp := somearray (q) %store what was at position q (the lower value in this case)
            somearray (q) := somearray (w) %move element w (greater value) to position q
            somearray (w) := temp %put what was at position q (lower value) into position w
        end if
    end for
end for

%output the now sorted array
for q : 1 .. 10
    put somearray (q)
end for


also I am pretty sure that you can check if a string is greater than another one as well (probably looks at the ascii reference values)
Coog03




PostPosted: Sun Jun 15, 2003 10:09 pm   Post subject: (No subject)

I'm using a bubble sort thats the only one I know
code:
procedure Sort
var n := upper (Data)
    for i : 1 .. n - 1
        var sorted := true
        for decreasing j : n - 1 .. i
            if Data (j).title > Data (j + 1).title then
                const temp := Data (j);
                Data (j) := Data (j + 1);
                Data (j + 1) := temp
                sorted := false
            end if
        end for
        exit when sorted
    end for       
end Sort


I can't get it to take the values from the file sort them and output them into a file.

I tried using read / write but how do I put that text into a text box? if not I'll use the pu / get combo...this is due tommorrow and having it done by then would be good
Sponsor
Sponsor
Sponsor
sponsor
PaddyLong




PostPosted: Sun Jun 15, 2003 10:25 pm   Post subject: (No subject)

read and write uses binary... so each of the strings you are reading must have a specified length

then you just do something like
code:

var Data : flexible array 1 .. 0 of
    record
        title : string (30)
        firstname : string (25)
        lastname : string (25)
        value : string (10)
        genre : string (15)
        keyword : string (10)
        subject : string (20)
    end record

procedure readFile
    var file : int
    var currIndex : int := 1
    open : file, "somefile", read
    loop
        exit when eof (file)
        new Data, currIndex
        read : file, Data (currIndex)
        currIndex += 1
    end loop
end readFile

procedure writeFile
    var file : int
    open : file, "somefile", write
    for q : 1 .. upper (Data)
        write : file, Data (q)
    end for
end writeFile


of course if you wanted to switch to read/write from an ascii database, you would need to write up a quick program that takes in all of the things from your current database using get and then rewrite them using write
Coog03




PostPosted: Sun Jun 15, 2003 10:37 pm   Post subject: (No subject)

got any idea on how to sort using your method of reading and writing
PaddyLong




PostPosted: Sun Jun 15, 2003 10:48 pm   Post subject: (No subject)

that method will just load it into your array as regular text... it will just be binary in the file

so just like check Data(someindex).title or whatever instead of somearray(someindex)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: