12 Replies - 7663 Views - Last Post: 15 October 2012 - 11:23 AM Rate Topic: -----

#1 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

button click If empty then

Posted 14 October 2012 - 11:37 PM

I'm trying to enter a first and last name into a text box and string it to a label. If the txt box is empty I want it to return a message "Please enter a name" If the text box is not empty I want it to string to the label.
When I debug and hit the button, even though I have entered a name it pops up the message box, please enter a name and does not input the name into the label. I tried a couple different codes? What am I doing incorrectly? Please and thanks.

    Private Sub btnAddClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddClient.Click

        Dim txtFirstName As String
        Dim txtLastName As String

        txtFirstName = lblFirstName.Text
        txtLastName = lblLastName.Text
        'If TextBox1.Text Is DBNull.Value OrElse TextBox1.Text.Trim() = "" Then
        'MessageBox.Show("Please enter a value into Textbox1")
        'leave the sub and do not update anything  
        'Exit Sub
        'End If

        If txtFirstName = "" Then
            MsgBox("Please Enter a First Name", , "Error")
        Else
            lblFirstName.Text = Str(txtFirstName)
        End If

        If txtLastName = "" Then
            MsgBox("Please Enter a Last Name", , "Error")

        Else
            lblLastName.Text = Str(txtLastName)
        End If

    End Sub
End Class


Is This A Good Question/Topic? 0
  • +

Replies To: button click If empty then

#2 deanobravo  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 142
  • Joined: 02-January 12

Re: button click If empty then

Posted 15 October 2012 - 12:49 AM

Private Sub btnAddClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddClient.Click

 
       ' Dim txtFirstName As String
      '  Dim txtLastName As String

        If  TextBox1.Text.Trim() = "" or TextBox2.Text.Trim() = "" Then

            MessageBox.Show("Please enter a value into both text boxes")
       
        else

           lblFirstName.Text = textbox1.text
           lblLastName.Text = textbox2.text

        End If

      

    End Sub


This post has been edited by deanobravo: 15 October 2012 - 12:49 AM

Was This Post Helpful? 1
  • +
  • -

#3 IJET  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 93
  • View blog
  • Posts: 121
  • Joined: 14-October 12

Re: button click If empty then

Posted 15 October 2012 - 12:55 AM

^ Just swap

  lblFirstName.Text = textbox1.text


With

 label1.text = textbox1.text

Was This Post Helpful? 1
  • +
  • -

#4 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: button click If empty then

Posted 15 October 2012 - 01:42 AM

I put your code in and fixed the lablel1 but it's giving me an error that says TextBox2 not declared?


View Postdeanobravo, on 15 October 2012 - 02:49 AM, said:

Private Sub btnAddClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddClient.Click

 
       ' Dim txtFirstName As String
      '  Dim txtLastName As String

        If  TextBox1.Text.Trim() = "" or TextBox2.Text.Trim() = "" Then

            MessageBox.Show("Please enter a value into both text boxes")
       
        else

           lblFirstName.Text = textbox1.text
           lblLastName.Text = TextBox2.text

        End If

      

    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: button click If empty then

Posted 15 October 2012 - 01:48 AM

Nevermind, I figured it out, I needed to go to my properties menu and declare it there.
Was This Post Helpful? 0
  • +
  • -

#6 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: button click If empty then

Posted 15 October 2012 - 01:54 AM

Now I need to add a PO Number to this same Button?
Was This Post Helpful? 0
  • +
  • -

#7 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: button click If empty then

Posted 15 October 2012 - 02:02 AM

Value of Type String can not be converted To Systems.Windows.Forms.Lable? A line under the TextBox3?



    Private Sub btnAddClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddClient.Click

        If TextBox1.Text.Trim() = "" Or TextBox2.Text.Trim() = "" Or TextBox3.Text.Trim() = "" Then
            MessageBox.Show("Please enter a value into First, Last Name and PO Number Please")

        Else
            lblLastName.Text = TextBox1.Text
            lblFirstName.Text = TextBox2.Text
            lblPONumber = TextBox3.Text
        End If

    End Sub

This post has been edited by charyl: 15 October 2012 - 02:03 AM

Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 410
  • View blog
  • Posts: 1,449
  • Joined: 14-September 12

Re: button click If empty then

Posted 15 October 2012 - 02:26 AM

View Postcharyl, on 15 October 2012 - 02:32 PM, said:

Value of Type String can not be converted To Systems.Windows.Forms.Lable? A line under the TextBox3?



    Private Sub btnAddClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddClient.Click

        If TextBox1.Text.Trim() = "" Or TextBox2.Text.Trim() = "" Or TextBox3.Text.Trim() = "" Then
            MessageBox.Show("Please enter a value into First, Last Name and PO Number Please")

        Else
            lblLastName.Text = TextBox1.Text
            lblFirstName.Text = TextBox2.Text
            lblPONumber = TextBox3.Text
        End If

    End Sub


Try this and see if it works

lblPoNumber.Text= TextBox3.Text



regards,
Raghav

This post has been edited by raghav.naganathan: 15 October 2012 - 02:27 AM

Was This Post Helpful? 1
  • +
  • -

#9 charyl  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 324
  • Joined: 29-June 11

Re: button click If empty then

Posted 15 October 2012 - 05:09 AM

Here's the rest of my code: I need to string these to a lbl? I couldn't decide if I should string them to a text box or what I should do nor how to string properly
'charyl janney
'Ex Midterm
'Oct 14, 2012
'This program should display a Construction Estimate Gui and Save Functions


'Creates a txt file of account information.
Imports System.IO

Public Class ConstructionEstimates
    'Writes data to text file
    Dim fileWriter As StreamWriter

    'Create a new file in which accounts can be stored
    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim result As DialogResult
        Dim fileName As String

        'Display dialog so user can choose the name of the file to save
        Using fileChooser As New SaveFileDialog()
            result = fileChooser.ShowDialog()
            fileName = fileChooser.FileName  'get specified file name
        End Using               'Automatica call to fileCooser.Dispose() occureshere

        ' if user did not click Cancel
        If result <> Windows.Forms.DialogResult.Cancel Then
            Try
                'open or create file for writing
                fileWriter = New StreamWriter(fileName, True)

                'enable controls
                CloseToolStripMenuItem.Enabled = True
                ExistingToolStripMenuItem.Enabled = True
                CloseToolStripMenuItem.Enabled = True
                NewToolStripMenuItem.Enabled = True
                ExitToolStripMenuItem.Enabled = True
                SaveAsToolStripMenuItem.Enabled = True
                LastNameToolStripMenuItem.Enabled = True
                accountnumberTextBox.Enabled = True
                txtEstimate.Enabled = True

            Catch ex As IOException

                MessageBox.Show("Error Opening File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try


        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.txtDate.Text = DateTimePicker1.Text

    End Sub
    'Add an account to the file

    Private Sub btnAddClient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddClient.Click, Button2.Click

        If TextBox1.Text.Trim() = "" Or TextBox2.Text.Trim() = "" Or TextBox3.Text.Trim() = "" Then
            MessageBox.Show("Please enter a value into First, Last Name and PO Number Please")

        Else
            lblLastName.Text = TextBox1.Text
            lblFirstName.Text = TextBox2.Text
            lblPONumber.Text = TextBox3.Text
        End If

    End Sub



    Private Sub btnItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnItem.Click, btnClear.Click

        Dim txtItemNumber As String
        Dim txtDescription As String
        Dim txtQuantity As String
        Dim txtCost As Decimal
        Dim lblOutPut As String = ("Item Number" & vbTab & "Description" & vbTab & "Quantity" & vbTab & "Cost")


    End Sub



    Private Sub ExistingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub addAccountNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addAccountNumber.Click

        'determine whether TestBox Account field is empty
        If accountnumberTextBox.Text >= String.Empty Then
            'try to store record to file
            Try
                'get account number
                Dim accountNumber As Integer
                Convert.ToInt32(accountnumberTextBox.Text)

                If accountNumber <> 0 Then
                    'Write record data to file separting fields by commas
                    fileWriter.WriteLine(accountNumber & "," &
                                         Convert.ToDecimal(txtEstimate.Text))
                Else
                    MessageBox.Show("Invalid Account Number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Catch ex As IOException
                MessageBox.Show("Error Writing to File", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Catch ex As FormatException
                MessageBox.Show("Invalid Account Number Or Balance", "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try
        End If
        accountnumberTextBox.Clear()
        txtEstimate.Clear()

    End Sub
    'Close the currently openfile and disable controls
    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


        CloseToolStripMenuItem.Enabled = False
        ExistingToolStripMenuItem.Enabled = False
        NewToolStripMenuItem.Enabled = False
        ExitToolStripMenuItem.Enabled = False
        SaveAsToolStripMenuItem.Enabled = False
        LastNameToolStripMenuItem.Enabled = False
        accountnumberTextBox.Enabled = False
        txtEstimate.Enabled = False
    End Sub

    ' Exit the application

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Application.Exit()
    End Sub
    Sub CloseFile()
        If fileWriter IsNot Nothing Then
            Try
                fileWriter.Close()
            Catch ex As IOException
                MessageBox.Show("Error closing file", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error)
            Catch ex As Exception

            End Try
        End If

    End Sub

   

  
End Class



Was This Post Helpful? 0
  • +
  • -

#10 torind_2000  Icon User is offline

  • D.I.C Regular

Reputation: 57
  • View blog
  • Posts: 293
  • Joined: 22-August 11

Re: button click If empty then

Posted 15 October 2012 - 06:48 AM

View Postdeanobravo, on 15 October 2012 - 03:49 AM, said:

        If  TextBox1.Text.Trim() = "" or TextBox2.Text.Trim() = "" Then



Just curious, but why did you use the .trim() method instead of String.Empty?

and OP, I prefer to use labels when displaying information. But it's all about what you like, it's your program :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#11 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: button click If empty then

Posted 15 October 2012 - 08:22 AM

Charyl you need to keep the fundamentals of object oriented programming fresh in your mind, when you're programming. Part of the issue you're having in most of your threads lately seems to be attributed to not checking data types. For example
lblPONumber = TextBox3.Text
Here you tried to make a label(object) equal the string property of a textbox. So remember to check before you press that enter key and try to run it! :D
Was This Post Helpful? 0
  • +
  • -

#12 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: button click If empty then

Posted 15 October 2012 - 10:55 AM

View Posttorind_2000, on 15 October 2012 - 06:48 AM, said:

Just curious, but why did you use the .trim() method instead of String.Empty?


I'm not deanobravo, but I would imagine he used .trim to trap the case where the TextBox.Text contained one or more whitespace characters.
Was This Post Helpful? 0
  • +
  • -

#13 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: button click If empty then

Posted 15 October 2012 - 11:23 AM

View Postcharyl, on 14 October 2012 - 11:37 PM, said:

I'm trying to enter a first and last name into a text box and string it to a label. If the txt box is empty I want it to return a message "Please enter a name" If the text box is not empty I want it to string to the label.
When I debug and hit the button, even though I have entered a name it pops up the message box, please enter a name and does not input the name into the label. I tried a couple different codes? What am I doing incorrectly? Please and thanks.


First, a few tips for you. When you are asking for help, Many of us will use Visual Studio to make a form and place a few controls on it, then write some code and do a few tests. We do this because it is all to easy to make a small error that we don't notice until the code is actually run. Bearing this in mind, you might want to remember that we will often use the 'default' names for controls, which will very often be different names than the ones you chose. The bottom line is that whatever code you are given as an example, is exaclty that; an example. It's up to you to see how it applies to your situation, and to make any adjustments to fit your circumstances.

As well, when you get advice on something (like putting a TextBox.Text value into a Label.Text for example), try to understand WHY that code is different than the code you tried, and also, how that code applies to the same type of operation for other values.

The best way to learn programming is to study WHY things work the way they do. Once you understand the fundamental ideas behind the language, you'll be well on the way to being a programmer, and the rest is simply a matter of applying that understanding to the process. Analysis of a task and planning software to complete it is a skill that becomes easier when the basics are understood.

I also wanted to mention something about your initial code. I am not sure you really understood why it didn't work.

Quote

        txtFirstName = lblFirstName.Text
        txtLastName = lblLastName.Text


If your names for controls are indicative of the type of control, then you are assigning the text property of the labels into the strings. I doubt the label .Text properties have anything in them yet, and that's why


You probably don't have any text in those labels yet, because that's where you want to place the TextBox.Tezt strings. That's why the MessageBoxes came up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1