Hi experts,
Im Ron and i have just recently started using VB and have written a small application for sending SMSes.

Code:
Imports System.Net
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        'Dim strUrl As String = "xxxxxxxxxxxxxx?user=Username:Password&senderID=xxx&receipientno=xxxx&msgtxt=This is a testxxxx API&state=4"
        Dim strUrl As String = "xxxxxxxxxuser=xxx:xxxxx&senderID=xxxxx&receipientno= " & TextBox3.Text & "&msgtxt=" & TextBox4.Text & "&state=" & TextBox5.Text & ""
        Dim request As WebRequest = HttpWebRequest.Create(strUrl)
        Dim response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse)
        Dim s As Stream = DirectCast(response.GetResponseStream(), Stream)
        Dim readStream As New StreamReader(s)
        Dim dataString As String = readStream.ReadToEnd()
        RichTextBox1.Text = dataString.ToString
        response.Close()
        s.Close()
        readStream.Close()
    End Sub
    Private Sub Button2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim mybut As New OpenFileDialog
        mybut.Filter = "Text Files (*.txt)|*.txt"
        mybut.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        If mybut.ShowDialog <> Windows.Forms.DialogResult.OK Then Exit Sub
        Dim myfile As String = mybut.FileName
        Dim reader As New System.IO.StreamReader(myfile)
        TextBox4.Text = reader.ReadToEnd
    End Sub



    Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox3.TextChanged

    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox4.TextChanged

    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox5.TextChanged

    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RichTextBox1.TextChanged

    End Sub


    Private Sub Label4_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Label4.Click

    End Sub

    Private Sub Label5_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Label5.Click

    End Sub

    Private Sub Label6_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Label6.Click

    End Sub

    Private Function strUrl() As String
        Throw New NotImplementedException
    End Function

End Class

What the code currently does: Button2 Selects a txt file and dumps its contents in the message box.
the Recipient No. needs to be entered manually.
Status needs to be entered as 4.
Now once button 1 is clicked once the rest of the details are furnished, an SMS is sent to the recipient number with the
message box contents as the message.
confirmation of message delievery is sent to the Rich text box.
the SMS sending is already tested and i have been able to manually send messages using the application.

What I require: the process to be automated,
eg once i click a Start Button the program runs and performs the following tasks:
it selects ALL the text files in a particular folder with the text files named as the number to be messaged (eg 99124124124.txt, 923523423.txt etc)
the name of the txt document is fed to the recipient number box which was created while the messages are fed into the message box respectively and the button 1 ie send should be used once the
details are filled in automatically once the valid details are filled in.
these txt files have to be sent on a user defined basis (eg once a day/twice a day the same txt file should be sent to the same number.and this has to be done for that particular text file) (user maybe able to select the numbers using a checkbox etc)
also a STOP button will be needed to ensure that the user can stop the sending of SMS to a particular number (eg stop sending sms to 9212323423) which should not affect the other
numbers.

Is this possible and could some expert guide me with a brief picture of how the code should look like?

Recommended Answers

All 16 Replies

I'll help change your button2 then:

 Dim f As String() = Directory.GetFiles("YourTextFilesPath", "*.txt")
 For Each dir In dirs
     ...
     'Do anything you want with f.FileName
     ...
 Next

thanks for the quick reply ,
could you give me a brief sketch of the code to assign the value of f.FileName into my recipient number box which is textbox 3 in the code ive posted,
while simultaneously my streamreader function dumps the actual text of the text file into textbox 4 (which it does in the code ive writtem) and to automatically send the SMS,deal with each of the multiple files that would be present in the dir, sending the SMS to those numbers one at a time each ,on a daily basis? (which is currently done by manually clicking Button 1 after selecting the txt file manually using button 2).
i apologise if i ask newbie questions, it is because i am one .

Actually it's quite simple.
The f in the those code lines is itself a .Filename, so you only need to fill f to your TextBox3, SMS information into TextBox4. I'll give you some hints to make it easier, it'll work right away but might need some fixes to avoid marginal errors.

Dim f As String() = Directory.GetFiles("YourTextFilesPath", "*.txt")
For Each f In f()
    Dim SMSdtl As String
    Dim txtReader As New System.IO.StreamReader(f)
    Do While txtReader.Peek() <> -1
            SMSdtl = SMSdtl & txtReader.ReadLine() & vbNewLine
    Loop
    Textbox3.Text = f
    Textbox4.Text = SMSdtl
Next

i believe f in those code lines is not the filename but the PATH to that filename along with the filename and the extention appended to it,
thus , where i would need 912412412.txt file to show as 912412412 in text box 3, it would show the path eg. C:/Users/desktop/012412412.txt.txt, which doesnt serve my purpose as i need only the filename .
Also
For Each f In f()

is returning a "number of indices is less than the number of dimensions of the indexed array." error
which i have been unable to understand. Could you please provide some hints as to what should i do regarding the same.

I believe .FileName in Vb.Net is a name with a path, but that doesn't matter. If you couldn't use For Each then For Next, and if you have a .FileNam then remove the path. Here's your code then, since it finished, mark this question as solved if you found your answer:

        Dim Path As String = "Your Path Here, with a "\" at the end"
        Dim file() As String = Directory.GetFiles(Path.Trim, "*.txt")
        For i As Integer = 0 To file.Length - 1
            Dim SMSdtl As String
            Dim txtReader As New System.IO.StreamReader(file(i))
            Do While txtReader.Peek() <> -1
                SMSdtl = txtReader.ReadToEnd.ToString.Trim
            Loop
            TextBox4.Text = SMSdtl.ToString.Trim
            TextBox1.Text = file(i).Replace(Path.Trim, "").Trim
        Next

thanks for the reply,
the code you provided was giving me a filename with the extention twice for some reason (eg 9214124.txt.txt). With a little bit of research ,i managed to remove it using a substring ()
so instead of giving me 9123214124.txt.txt now i am getting the desired number in texbox 3.
But one issue remains and that is the automation of the application.
at the moment i still have to click on button1 to send the SMS which i dont want.
I require that the application ,once i click button 2 ,will automatically send the SMS by using button 1 and move on to the next text file in the folder. (right now it is stuck on the first file and isnt sending the SMS because button 1 isnt being pressed)
so forexample if there are 2 files in the folder 9235423523.txt and 9234234234.txt, the application is pasting the first .txt file in textbox 3 and its contents in textbox 4, but is not getting sent and not moving on to the next file ie 9234234234.txt.
Is there any way to introduce a timer functionality with a value of say 1 day into the application that uses button 1 to send the SMS once a day to each of the numbers?

COuld you give me a brief idea of how i should go about it?

(i could also PM you the current code to help you understand what im trying to say , if you so desire.)

You can simulate a button click with code by using Button1.PerformClick

Thanks, that worked !
what is currently happening: the SMS app is selecting the first text file 912312312.txt and filling the respective text boxes and SMSing the number correctly but is stopping after the first SMS is sent and doesnt move on to the other text files. (so the other text file 912423423.txt isnt being read or SMSd to.
What i require: the above mentioned things to be done once a day to ALL the .txt SMS numbers in that particular folder
How can i go about achieveing that?
Is it possible to use a timer to calculate a whole DAY's interval?

A timer should work. it will allow an interval that big(86400000)

is it possible to get the interval range from the user with the help of a radio button box where the user can select from maybe 1 day / 12 hrs / 48 hrs interval?
and can the timer be dynamically programmed to use that particular user - defined interval?
if so, can you give me a brief sketch of the required code to apply that user defined timer to send my SMSes to each of the numbers at that particular chosen interval?

Here's some code that should help. This assumes radiobuttons named with this pattern, "rb12"(last 2 characters to represent the number of hours for the interval)

    Dim TimerInterval As Int32 = 0

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Tie all the radiobuttons to one handler
        For Each rb As RadioButton In Me.Controls.OfType(Of RadioButton)()
            AddHandler rb.CheckedChanged, AddressOf rb_CheckedChanged
        Next            
        Timer1.Interval = 43200000 ' Set to 12 hrs initially
        Timer1.Start()            
    End Sub

    ' This will change the interval and re-start the timer according to the radiobutton checked
    Private Sub rb_CheckedChanged(sender As System.Object, e As System.EventArgs)
        Dim ClickedRb As RadioButton = DirectCast(sender, RadioButton)
        If ClickedRb.Checked Then
            TimerInterval = Int32.Parse(rb12.Name.Substring(rb12.Name.Length - 2)) * 3600000 ' The number of milliseconds in one hour
        End If
        Timer1.Stop()
        Timer1.Interval = TimerInterval
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        'Call your routine to send the SMS here.
    End Sub

For stopping and starting call the timer1 start and stop methods in the click events handlers of 2 buttons.

If you want to change the start time you'll have to set the inital interval to the start of the next time period and a set a boolean to true. In the tick event handler check for the boolean. If it's true, set it false and change the interval to the regular interval the user wants.

Thanks for the reply, tinstaafl
I actually thought against using radiobuttons and allowing the user to change the timer interval,as it unneccessarily complicated things and allows the user to screw up things .
Instead I have decided to let the user just set the time :hrs: :minutes: and the program will run when the time matches the time entered by the user.
For ex. if the time entered by the user is 14hrs and 00 minutes, it will send the sms when the time is the same
i esentially used a timer interval of 1 minute to ensure that the app checks the time once a minute and compares it with the user given time and sends the SMS.
THe code is given below:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim time As Date = Date.Now
            Dim currhour As Integer
            Dim currminute As Integer
            Dim ReportHour As Integer
            Dim ReportMinute As Integer
            currhour = time.Hour
            currminute = time.Minute
            ReportHour = " & TextBox1.Text & "
            ReportMinute = " & TextBox2.Text & "
            If currhour = ReportHour AndAlso currminute = ReportMinute Then
                Button2.PerformClick()
            End If
        End Sub

if i use an actual integer value for ReportHour= and ReportMinute=,
it works fine and sends an SMS at that given time.
The problem is , when i try to get that integer value from the user who enters it into Textbox1 and 2,
it throws an invalid cast exception.
I think it may not allow for a string to be converted to an integer. If so could you provide me a brief sketch of the code to enable the user to enter the required time in hr min format through textbox1 and textbox2, and Send the value to the ReportHour and ReportMinute as shown in the code above?

You could use a DateTimePicker control to display only the hour and minutes. With the scroll buttons active the user can use those or type the value in. Then ReportHour = DateTimePicker1.Value.Hour and ReportMinute = DateTimePicker1.Value.Minute. You might want a button to signify the time is set and to disable the datetimepicker to prevent accidental changes.

Here's the properties of the DateTimePicker to set:

        CustomFormat = "hh:mm"
        Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Format = System.Windows.Forms.DateTimePickerFormat.Custom
        ShowUpDown = True
        Value = New Date(2013, 6, 27, 12, 0, 0, 0)

Thanks, that worked.
i also included a button to enable / disable the datetimepicker as u mentioned and my app seems to be working fine!

i used CustomFormat = "HH:mm" to make it 24 hr instead of 12 hr and that seems to be working out ok at the moment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.