Results 1 to 34 of 34

Thread: Help with coding in Visual Basic

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Help with coding in Visual Basic

    I am trying to write the code for a conference:

    1 person costs $695.00
    2-4 people costs $545
    5-8 people costs $480
    8 or more costs $395

    If the company has been before they get a 15% discount and no company can have more than 16 people. The program asks that the user enters valid data.?? (error msg boxes)

    Could someone show me how to do this?

  2. #2
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    A way for you to start this (assignment?) would be to create an integer variable for number of people, decimal for cost, and a boolean variable for if a company has attended before.

    You may want to receive input from the user with a textbox, a checkbox, and a button, and display the results in a label.

    From the textbox would be the count of users, the checkbox if they've attended before. Within the button, you would want either Select Case or if you haven't learned this for your class (i'm assuming), use nested IF statements to assign a value to your cost variable. And also, check for the selection of the checkbox. If it is, assign cost as 85% of itself and display results. If not, do not apply the discount.

  3. #3
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with coding in Visual Basic

    I think you are going to have to provide just a bit more information then, I want to make a program do this...... How do I do it?

    With that said I feel your pain as starting out and not knowing were to begin. Sounds like you have an idea and not sure where to go next?

    Check out www.homeandlearn.co.uk vb.net for beginners, it will introduce you to basic concepts of inputting data, validating and calculating as well as "IF" statements, checkboxes and more...

    If > this many people then> this price etc....

    Then write out hte steps on paper and tackle each one at a time, example

    1. Place to enter data
    2. Validate Data
    3. Calculate

    etc...

    If I am wrong and you have some code going already and just stuck in a particular area go ahead and post the code as to what you have so far, its also a good idea to let people knopw what version of VB you are working with.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with coding in Visual Basic

    you could easily make your program just using a datagridview + a little code.

    some of the code is a bit tricky, to handle the combobox + checkbox changing:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim loading As Boolean = True
    4.     Dim values() As Integer = {0, 695, 545, 545, 545, 480, 480, 480, 480, 395, 395, 395, 395, 395, 395, 395, 395}
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Dim dgvComboboxes As DataGridViewComboBoxColumn = DirectCast(DataGridView1.Columns(1), DataGridViewComboBoxColumn)
    8.         dgvComboboxes.DataSource = New String() {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"}
    9.         loading = False
    10.     End Sub
    11.  
    12.     Private Sub DGVComboIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    13.         Dim cb As ComboBox = DirectCast(sender, ComboBox)
    14.         DataGridView1(3, DataGridView1.CurrentCell.RowIndex).Value = If(CBool(DataGridView1(2, DataGridView1.CurrentCell.RowIndex).Value) = True, ((CInt(cb.SelectedItem) * values(CInt(cb.SelectedItem))) * 0.85).ToString("c2"), (CInt(cb.SelectedItem) * values(CInt(cb.SelectedItem))).ToString("c2"))
    15.     End Sub
    16.  
    17.     Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
    18.         If Not loading Then
    19.             If e.ColumnIndex = 2 Then
    20.                 DataGridView1(3, e.RowIndex).Value = If(CBool(DataGridView1(2, e.RowIndex).Value) = True, ((CInt(DataGridView1(1, e.RowIndex).Value) * values(CInt(DataGridView1(1, e.RowIndex).Value))) * 0.85).ToString("c2"), (CInt(DataGridView1(1, e.RowIndex).Value) * values(CInt(DataGridView1(1, e.RowIndex).Value))).ToString("c2"))
    21.             End If
    22.         End If
    23.     End Sub
    24.  
    25.     Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    26.         If DataGridView1.CurrentCell.ColumnIndex = 2 Then
    27.             DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    28.         End If
    29.     End Sub
    30.  
    31.     Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    32.         ' Attempt to cast the EditingControl to a ComboBox.
    33.         Dim cb As ComboBox = TryCast(e.Control, ComboBox)
    34.         If cb IsNot Nothing Then
    35.             RemoveHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
    36.             AddHandler cb.SelectedIndexChanged, AddressOf DGVComboIndexChanged
    37.         End If
    38.     End Sub
    39.  
    40. End Class
    Attached Images Attached Images  

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Thanks to you that responded...

    Paul,

    That was a little in depth for the class Im in. I am in my 1st class of Visual Basic 2010 Express.

    We are just doing strings, decimals, If, ElseIF and now radio buttons. I just seem to get confused when it comes to actually writing the code.

    I have my # of employees set up in a text box
    I have the prices in labels
    and I have a radio button set up to push if your a returning company
    The total cost is set up as a label
    I have a calculate button
    I have a label to enter the amount of employees attending
    A text box where the company enters their name.

    Please Help

  6. #6
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Help with coding in Visual Basic

    Quote Originally Posted by mtndad69 View Post
    Thanks to you that responded...

    Paul,

    That was a little in depth for the class Im in. I am in my 1st class of Visual Basic 2010 Express.

    We are just doing strings, decimals, If, ElseIF and now radio buttons. I just seem to get confused when it comes to actually writing the code.

    I have my # of employees set up in a text box
    I have the prices in labels
    and I have a radio button set up to push if your a returning company
    The total cost is set up as a label
    I have a calculate button
    I have a label to enter the amount of employees attending
    A text box where the company enters their name.

    Please Help
    Post what you have so far:

    Here is a very basic general idea for radiobuttons again the website I posted will help you alot

    If RadioButton1.Checked= True Then

    If RadioButton1.Checked=True AND TextBox1.Text=#ofEmployees Then

    ElseIf .......

    Get the idea??

  7. #7
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    As you should be learning the code and not regurgitating it, I will give you a portion of what you would likely use for this assignment. Some of it is blank intentionally so you can learn this on your own.

    Code:
            Dim persons As Integer = NumericUpDown1.Value 'this could be a textbox if you chose, however you can have input validation problems without additional code.
            Dim cost As Decimal
    
            If persons = 1 Then
                cost = persons * 695
            ElseIf persons > 1 And persons <= 4 Then
                cost = persons * 545
            ' fill in the blank here
            End If
    
            If CheckBox1.Checked Then 'easily replaced with a radiobutton if that is your choice
            ' this is if the company has attended before, if so, change the value of cost here.
            End If
    
            Label1.Text = String.Format("{0:c}", cost) 'this displays the total cost to appear in normal currency format.

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Thanks again guys for chiming in on my request for help.

    I would just assume copy the code I do not want to be a programmer. This is just what I have to do to complete my school.

    I have no desire to program. I just want to get finished.

    Do I need to do the string and intergers and then do the code? I just don't know or desire to know all this mess. I transferred into this school and it is part of their program.

    I know that sounds nuts but, really I don't want to be a programmer. So If you could help me out that would be great.

    Victoria

  9. #9
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    Here is what you would be likely to use for a basic class and would cover the most likely used commands. The exception is that you may not have used String.Format yet, and if so, set the label text to something like: "$" & cost

    Code:
            Dim persons As Integer = NumericUpDown1.Value
            Dim cost As Decimal
    
            If persons = 1 Then
                cost = persons * 695
            ElseIf persons > 1 And persons <= 4 Then
                cost = persons * 545
            ElseIf persons > 4 And persons <= 8 Then
                cost = persons * 480
            ElseIf persons > 8 Then
                cost = persons * 395
            End If
    
            If CheckBox1.Checked Then
                cost *= 0.85
            End If
    
            Label1.Text = String.Format("{0:c}", cost)

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    I am getting a msg telling me that a non-shared member requires an object reference. It is telling me that where I have it Bold and Underlined. Can you help again?????

    Victoria


    Private Sub grpADSEConference_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpADSEConference.Enter

    Dim persons As Integer = NumericUpDown.Value
    Dim cost As Decimal

    If persons = 1 Then
    cost = persons * 695
    ElseIf persons > 1 And persons <= 4 Then
    cost = persons * 545
    ElseIf persons > 4 And persons <= 8 Then
    cost = persons * 480
    ElseIf persons > 8 Then
    cost = persons * 395
    End If

    If raddiscount.Checked Then
    cost *= 0.85
    End If

    lblcostforcompany.Text = String.Format("{0:c}", cost)
    End Sub
    End Class

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with coding in Visual Basic

    did you add a NumericUpDown control to your form?
    what is the name of your NumericUpDown control?

    Code:
    Dim persons As Integer = NumericUpDown.Value
    assuming it's NumericUpDown1:

    Code:
    Dim persons As Integer = NumericUpDown1.Value

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Also, the company gets a 15% discount if they have been before. On the reply you sent it has * 0.85 shouldn't it be 0.15???

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with coding in Visual Basic

    cost * 0.15 = 85&#37; discount
    cost * 0.85 = 15% discount

  14. #14

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Hi there,

    Now when I put the UpDown1 in to the code. This is what I get at the bottom of the page.

    Private Function NumericUpDown1() As Object
    Throw New NotImplementedException End Function

  15. #15
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    I put in a NumericUpDown control on my test project. It appears to me that you do not have one. Change that line to read TextBox1.Text instead and delete that private function you had created.

  16. #16

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Now it says conversion from type string to interger is not valid. When casting from a number it must be less than enfinity???? OMG....I could never do what you guys do.....

    Private Sub grpADSEConference_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpADSEConference.Enter
    Dim persons As Integer = txtnumberofemployees.Text

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with coding in Visual Basic

    dodgy method:
    vb Code:
    1. Dim persons As Integer = cint(txtnumberofemployees.Text)

    or:

    safe method:
    vb Code:
    1. Dim persons As Integer
    2. Integer.tryparse(txtnumberofemployees.Text, persons)

  18. #18
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    replace with:

    Code:
            Dim persons As Integer
            Integer.TryParse(txtnumberofemployees.Text, persons)
    edit: ahh you beat me to the punch =(

  19. #19

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    OK so now the stupid thing (Im the stupid one) won't calculate when I push calculate. I have the calculate button and a label box where the cost of employees is suppose to show up.

    Victoria

  20. #20
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    If you're using the .TryParse code, it will assign 0 to persons if you have something that can't be converted to a number. Is the label completely blank or showing $0?

  21. #21

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Ok Skor 13 I am sending you the whole thing so you can see it.....


    Public Class frmActiveDevelopersSkillEnhancement

    Private Sub grpADSEConference_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpADSEConference.Enter
    Dim persons As Integer
    Integer.TryParse(txtnumberofemployees.Text, persons)

    Dim cost As Decimal

    If persons = 1 Then
    cost = persons * 695
    ElseIf persons > 1 And persons <= 4 Then
    cost = persons * 545
    ElseIf persons > 4 And persons <= 8 Then
    cost = persons * 480
    ElseIf persons > 8 Then
    cost = persons * 395
    End If

    If raddiscount.Checked Then
    cost *= 0.85
    End If

    lblcostforcompany.Text = String.Format("{0:c}", cost)
    End Sub



    Private Sub frmActiveDevelopersSkillEnhancement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    End Class

  22. #22

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    I kindof need it in the simplest terms...This is a beginner class and I haven't even heard of typeparse or whatever it was....LOL

  23. #23
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    I tested the code just now and it works just fine. The only thing I can't check is the grpADSEConference.Enter event. What is grpADSEConference anyway? You just need to be sure that the code is being triggered after they input the data.

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with coding in Visual Basic

    the _enter event occurs when grpADSEConference gains focus.
    put the code in a button's click event

  25. #25

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    grpADSEConfere...Thats the name of the project.

    I am not getting a $ amount in my label box where it should go. It is $0.00

  26. #26

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Paul where do I put that and how I just posted the entire code. Sorry for not knowing

  27. #27
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with coding in Visual Basic

    add a button to your form, then in design view doubleclick it, + you'll navigate to a code window with this:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3. End Sub

    that's a button _click event for button1. put the code in there

  28. #28

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    button 1...just make a button??? On my other projects this is how the calculate button is set up....

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

    ' The calculate event handler calculates the gross pay, taxs due, and the net pay based on the
    ' hours worked, hourly pay, and checking one of the tax rate radio buttons.

    Dim strtxtHours As String
    Dim inttxtHours As Decimal
    Dim strtxtPayPerHour As String
    Dim inttxtPayPerHour As Decimal
    Dim declblTotalGrossPay As Decimal
    Dim declblTotalTaxDues As Decimal
    Dim declblNetPay As Decimal
    Dim decTaxRate As Decimal

    I don't know what you mean. Gosh I'm sorry...I just don't get this programming stuff.

    I have a calculate button on my project and a label box for where the amount is suppose to show up.

  29. #29
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    For many calculations, you use a button. The latest example you posted appears to be a button sub. Just do what .paul.'s last post says, select and cut all that code and paste it into the button sub.

  30. #30

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    Skor 13,

    I just don't know.... Here is what I have so far....Please tell me what to do...
    I don't know how or where to do or make it calculate...It is showing $0.00


    Private Sub grpADSEConference_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpADSEConference.Enter
    Dim persons As Integer
    Integer.TryParse(txtnumberofemployees.Text, persons)

    Dim cost As Decimal

    If persons = 1 Then
    cost = persons * 695
    ElseIf persons > 1 And persons <= 4 Then
    cost = persons * 545
    ElseIf persons > 4 And persons <= 8 Then
    cost = persons * 480
    ElseIf persons > 8 Then
    cost = persons * 395
    End If

    If raddiscount.Checked Then
    cost *= 0.15
    End If

    lblcostforcompany.Text = String.Format("{0:c}", cost)
    End Sub


    Private Sub frmActiveDevelopersSkillEnhancement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    grpADSEConference.Focus()
    End Sub


    End Class

  31. #31
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    Read post #27

  32. #32

    Thread Starter
    Member
    Join Date
    Jun 2011
    Posts
    40

    Re: Help with coding in Visual Basic

    I did read #27...I just don't get it. Please see what I sent you last and tell me where to put what. I can double click on the calculate button and go into design mode..what do I put in the code for the calculate button??

    Victoria

  33. #33
    Addicted Member
    Join Date
    Apr 2011
    Posts
    223

    Re: Help with coding in Visual Basic

    The design view is what your program looks like. The window, buttons, textboxes etc... When you double click on one of those objects, most cases will automatically open up the code window and either add or bring you to the click event for that object. From there, you need to paste all of that code that is within the Sub you posted into this new button click event.

  34. #34
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with coding in Visual Basic

    i answered your PM.

    i think you should make an effort + really try with your programming class instead of looking at it as something you're completely not interested in that you're only doing grudgingly as a means to an end.

    you might find you enjoy it if you weren't fighting it every step of the way + you would definitely learn more in all aspects + not just vb.net if your attitude was better

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width