Computer Science Canada

Help on programing a program that formats strings!

Author:  camilito [ Mon Mar 21, 2005 4:04 pm ]
Post subject:  Help on programing a program that formats strings!

heres the deal ...
im trying to do a program that takes an input string, increases it to a specified length by catenating the required number of characters (the character is also specified) to either the left or the right of the input string, and catenates the result to whatever is already in the output textbox.

the pic that i put here is just to show what it should look like ater the first click in the "cantenate it" button ... and then when the user inputs new data and and makes a second click of this button..

as u can see this pictures is just to show what im trying to du....but it doesnt work properly..

care to post?

Author:  Brightguy [ Mon Mar 21, 2005 6:38 pm ]
Post subject:  Re: Help on programing a program that formats strings!

Something like this:
VisualBASIC:
Private Sub Command1_Click()
    strOrig = Text1.Text
    intLength = Text2.Text
    strChar = Text3.Text
   
    If Combo1.Text = "right" Then
        Text4.Text = Text4.Text & strOrig & String(intLength - Len(strOrig), strChar)
    ElseIf Combo1.Text = "left" Then
        Text4.Text = Text4.Text & String(intLength - Len(strOrig), strChar) & strOrig
    End If
End Sub

Author:  camilito [ Wed Mar 23, 2005 10:20 am ]
Post subject: 

strOrig = Text1.Text
intLength = Text2.Text
strChar = Text3.Text


do these have to be declared ...?.... will there be a problem if the intLenght is not changed to an integer? (I cannot get it to work..)

thanks for the post..

Author:  camilito [ Wed Mar 23, 2005 3:31 pm ]
Post subject: 

If the size is less than the length of the input string the string should be truncated ...how du i du that?


The Click event of the Catenate It button should be one statement only, that calls a function. The arguments of the function will be values obtained from properties of the appropriate control objects. There will be four agruments one of them being an integer... <---help?

8)

Author:  Brightguy [ Thu Mar 24, 2005 10:59 pm ]
Post subject:  Re: Help on programing a program that formats strings!

Something like this:
VisualBASIC:
Private Function StringOutput(strInput As String, intLength As Integer, strChar As String, strComboChoice As String) As String
    If Len(strInput) < intLength Then
        If strComboChoice = "right" Then
            StringOutput = strInput & String(intLength - Len(strInput), strChar)
        ElseIf strComboChoice = "left" Then
            StringOutput = String(intLength - Len(strInput), strChar) & strInput
        End If
    Else
        If strComboChoice = "right" Then
            StringOutput = Left(strInput, intLength)
        ElseIf strComboChoice = "left" Then
            StringOutput = Right(strInput, intLength)
        End If
    End If
End Function

Call the function like this (except use the properties from the textbox/combobox):
VisualBASIC:
StringOutput("jump up and down", 20, "!", "right")

Also, if you're worried that someone will enter a non-integer into the length textbox, use a function like the Val function (removes non-numbers in a string).

Author:  camilito [ Fri Mar 25, 2005 11:31 pm ]
Post subject:  ummm...

thangu so much...you have been really helpful ... Very Happy
lol but you forgot one thing...that when I press the catenate it comand butom again...any previous text should be catenated to the new text...
it can be easily fixed...just by looking at the first example u showed me.. again...thangu so much 8)

now i was wondering how to write a Validate event for the text box containing the size of the padded string, and to ensure that focus shifts from control to control appropriately during interaction with the interface. ..
care to sugest?
8)

Author:  Brightguy [ Sat Mar 26, 2005 12:32 pm ]
Post subject:  Re: Help on programing a program that formats strings!

You should add the previous text in the catenate event, not in the function. (I think that's what the description wants.)

As for shifting the focus... something like this:
VisualBASIC:
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = Asc(vbCrLf) Then
        Text2.SetFocus
        Text2.SelStart = Len(Text2.Text)
    End If
End Sub

Author:  camilito [ Sat Mar 26, 2005 1:41 pm ]
Post subject:  okay...

umm..okay how would i make the Clear button leave focus with the input string textbox, for example...
8)

Author:  camilito [ Sat Mar 26, 2005 2:40 pm ]
Post subject:  umm...

i was thinking of doing something like this :
code:

Private Sub txtLenght_Validate(Cancel As Boolean)
    If IsNumeric(txtLenght) Then
        Cancel = False
    Else
        txtLenght.SetFocus
        Cancel = True
    End If
End Sub

what this will du is keep the user from putting a string in the lenght padded field....and it will not let the user continue until they input an integer..
how can i integrate what u posted ...int0 this statement to ensure that focus shifts from control to control appropriately during interaction with the interface?

Author:  Brightguy [ Sun Mar 27, 2005 6:34 am ]
Post subject:  Re: Help on programing a program that formats strings!

In the clear button click event, set the focus to the input string textbox.

If you don't want any non-numbers displayed in the textbox, you could do something like this:
VisualBASIC:
Private Sub Text1_Change()
    Text1.Text = Val(Text1.Text)
End Sub

Author:  camilito [ Wed Mar 30, 2005 8:33 am ]
Post subject:  ok well..

thank you ... u really helped me...
u ARE a Bight guy.. 8)


: