...

View Full Version : Help with Assignment Please...



Sherby
12-28-2012, 03:14 PM
Graphical User Interface
Design for Windows Forms
OVERVIEW
You now have the basic GUI skill to make the GroceryApp
project a multiple-form application. This project will assess
your understanding of using controls, menus, and handling
multiple forms.
Make sure that you follow all directions completely and verify
your results before submitting the project. Remember to
include all required components in your solution or you won’t
receive full credit.
YOUR PROJECT
In the graded project for Lesson 3, you added grocery classes
to the GroceryApp project. In this project, you’ll add a form
to allow users to add grocery items to the shopping basket.
The form will also use menus. You’ll also set the startup form
and manage the login form window. The output of this project
will be used in the next graded project.
INSTRUCTIONS
1. In Visual Studio, load the GroceryApp project that you
completed in Lesson 3.
2. Add a new form to the project named GroceryItemForm.
3. Add controls to GroceryItemForm in the following layout:
Label controls, two TextBox controls, a NumericUpDown
control, a ComboBox control, a GroupBox component, a
RichTextBox control and a Button control.
4. Modify the design-time properties of the form and its
controls using the following table:
5. Add the option E&xit to the Application menu.
6. Add the options A&dd and &View in the Basket menu.
7. Save your work on GroceryItemForm.vb.
8. Open the design view of LoginForm.vb.
9. Select LoginForm. Set the AcceptButton and
CancelButton properties to the buttons btnLogin and
btnCancel, respectively.
10. Select txtPassword. Set the PasswordChar property to *.
11. Set the TabIndex property for the txtUsername and
txtPassword controls to 0 and 1, respectively.
12. Save these changes on LoginForm.vb.
13. Open the design view of GroceryItemForm.vb.
14. In the form’s Load event, display the login form modally.

I am a bit lost starting right here…..

15. In the Click event of the btnAddToBasket button, create
a GroceryItem object using the values from the controls
and add it to the basket variable. Remember the basket
variable is the GroceryBasket collection.
a. Make sure all controls except txtScanNumber contain
a value.
b. Set the value of the txtScanNumber control using the
following code:
txtScanNumber.Text = _
txtBrandName.Text.Substring(0, 3) & “1019”
c. Instantiate the GroceryItem class, using the control
values.
d. Use the following expression to set the Aisle property
[Enum].Parse(GetType(Aisle), lstAisle.Text)
This expression converts the text into an Aisle
enumeration.
e. Add the GroceryItem object to the basket variable.
f. Make sure to clear the content of all controls.
16. In the Click event of the Exit menu item, end the application.
17. Have the btnAddToBasket_Click method handle the
Click event of AddToolStripMenuItem as well.
18. In the Click event of the View menu item, display all of
the items in the basket variable in an informational message
box. You need only display the Aisle, ScanNumber,
and BrandName properties. See Assignment 9 for help.
19. Set GroceryItemForm as the startup form.

Here is my code


Public Class GroceryItemForm

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

End Sub

Private Sub GroceryItemForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create an instance of LoginForm.
Dim frmLogin As New LoginForm

' Display the login form in modal style.
frmLogin.ShowDialog()
End Sub

Private Sub btnAddToBasket_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddToBasket.Click,
AddToolStripMenuItem.Click

Dim objGroceryItem As New GroceryItemForm

objGroceryItem.lblBrandName.Text = "&Name"
objGroceryItem.lblScanNumber.Text = "&Scan"
objGroceryItem.lblPrice.Text = "&Price"
objGroceryItem.lblAisle.Text = "Bakery"
objGroceryItem.grpDescription.Text = "&Description"
objGroceryItem.btnAddToBasket.Text = "A&dd to Basket"
txtScanNumber.Text = _
txtBrandName.Text.Substring(0, 3) & 1019
objGroceryItem.lblAisle = [Enum].Parse(GetType(GroceryItem.AisleType), lstAisle.Text)




End Sub


Private Sub ViewToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ViewToolStripMenuItem.Click
MessageBox.Show("An item is successfully added")
End Sub

Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Close()
End Sub



End Class

alykins
12-28-2012, 06:16 PM
ok, we can see the assignment, and can see the code you've worked on so far, but what is it you are having problems with. What is your question? What part is not working or do you need clarified?

Also has the instructor gone over debugging methods and break points? Using the watch window and the immidiate window? These are critical in figuring out what problems are in code.

More than willing to help you, but definitely need to know "what" your question is exactly.

Sherby
12-28-2012, 07:16 PM
Starting at #15 not too sure i have a full understanding of what the control values are. I have uploaded a scan of the controls i'm working with. Also when i run the app i get a warning that reads: ArgumentOutOfRangeException was unhandled - Index and length must refer to a location within the string. Parameter name: length at the line txtScanNumber.text = _
txtBrandName.Text.Substring(0, 3) & 1019.

This is an online degree so i am basically on my own. I have also emailed the school asking for some assistance but have not received a reply.

Thanks for your help, appreciate it......

alykins
12-28-2012, 09:22 PM
ok that error means that the txtBrandName.Text was probably less than 4 characters long. You need to ensure that it has at least 4 chars and then appens the numbers; you are alos missing quotes around the numbers
This code


txtScanNumber.Text = _
txtBrandName.Text.Substring(0, 3) & "1019"

is probably easier to be read like this...


txtScanNumber.Text = txtBrandName.Text.Substring(0,3) & "1019"


the substring looks at a peice of the string with the supplied parameters... for example


Dim strA As String = "Oatmeal"
Dim strB As String = strA.Substring(0,3)
Dim strC As String = strA.Substring(0,3) & "555"
Dim strD As String = "the cat " & "and dog"

'values of all of these...
'strA: Oatmeal
'strB: Oatm
'strC: Oatm555
'strD: the cat and dog


the '_' is a way to put what you want on a new line but still same statement...
ex


Dim s As String = _
"apples"


It can also be used to concatonate strings, but then you need to also use the ampersand


Dim s As String = _
"apples" _
& " make healthy" _
& " snacks"


I think it would be helpful if you post your groceryitem class as well. for part A you will need to make a function to verify that none of those fields are null. I might be on over the weekend to check back, but give it an honest go and do some simple tutorials. Make some play projects and try out different things. It seems to me you just need a little bit more familiarity with the language and how to use it as well as the base classes themselves.

Sherby
12-29-2012, 08:24 PM
Thank you Alykins. I'm going to hit at it again and see what happen. I'll definately let you know how it goes...

Sherby
12-29-2012, 08:44 PM
Here's the code for the GroceryItem Class.....



Public Class GroceryItem
' Member variables for scan number, brand name, description,
' price and aisle.
Private intScanNumber As Integer ' Holds unique serial code
Private strBrandName As String ' Name of item
Private strDescription As String ' Decription of item
Private dblPrice As Double ' Cost of the item
Private strAisle As String ' Where the item is located

' ScanNumber property
Public ReadOnly Property ScanNumber() As Integer
Get
' Variable to hold the Number
Dim intScanNumber As Integer
Return intScanNumber
End Get
End Property

' BrandName property procedure
Public Property BrandName() As String
Get
Return strBrandName
End Get
Set(ByVal value As String)
strBrandName = value
End Set
End Property

' Description property procedure
Public Property Description() As String
Get
Return strDescription
End Get
Set(ByVal value As String)
strBrandName = value
End Set
End Property

Public Property Price() As Double
Get
Return dblPrice
End Get
Set(ByVal value As Double)
dblPrice = value
End Set
End Property
Public Property Aisle() As String
Get
Return strAisle
End Get
Set(ByVal value As String)
strAisle = value
End Set
End Property

Public Enum AisleType
Bakery
CannedGood
Drinks
Deli
DryGoods
FrozenFood
Produce
End Enum

' Constructor
Public Sub New(ByVal ScanNumber As Integer)
Me.intScanNumber = ScanNumber
End Sub

' Constructor
Public Sub New(ByVal ScanNumber As Integer, ByVal BrandName As String, ByVal Price As Double)
Me.intScanNumber = ScanNumber
Me.strBrandName = BrandName
Me.dblPrice = Price
End Sub


End Class

Sherby
01-02-2013, 02:40 PM
Ok, so i'm still not understanding 15e. Add the groceryitem object to the basket variable. I have gotten the txtscannumber and [Enum[.Parse(GetType(Aisle), lstAisle.text) expressions to work, but still stuck at instruction e.

alykins
01-03-2013, 02:57 PM
You need to look into collections and generic lists and adding to them. I don't even see a 'basket variable' in your code. I made this quick example. If you create a new project (console app), delete everything from the default page, and copy/paste everything from here it should debug just fine and you will see what is going on (or maybe just reading through the code will be good enough)


Module Module1

Sub Main()
Try


Dim myAnimals As New List(Of Animals)


Console.WriteLine("This is an example of adding objects to a collecion, which {0}" _
& "is what you need to learn how to do. Right now the collection 'myAnimals'{0}" _
& "has {1} members in it. Let's go through and add a few shall we?", vbCrLf, myAnimals.Count)
Console.ReadLine()

'This adds a member without any paramerters using the Sub New() constructor
myAnimals.Add(addAnimalsNoParms())


Console.WriteLine("Now the collection has {0} members, each with a set of{1}" _
& "properties set to default values. Let's see what they are...{1}" _
& "Species: {2}{1}" _
& "Age: {3}{1}" _
& "Eats: {4}{1}" _
& "Name: {5}{1}", myAnimals.Count, vbCrLf, myAnimals(0).Species, myAnimals(0).Age, myAnimals(0).Eats, myAnimals(0).Name)
Console.ReadLine()

Console.WriteLine("Now let's see another way too add{0}members... (and set properties of the first member){0}*Look at the code to see this*", vbCrLf)
Console.ReadLine()

myAnimals(0).Species = "Cow"
myAnimals(0).Age = 25
myAnimals(0).Eats = Animals.EatType.herb
myAnimals(0).Name = "Buttercup"

'this way does a straight add to the collection using the function
myAnimals.Add(addAnimalsWithParms("Turtle", 12, Animals.EatType.omni, "Apple"))

'this way creates and Animals object, sets it's properties, and then adds it to the collection
Dim _animal As New Animals
_animal.Species = "Dog"
_animal.Age = 7
_animal.Eats = Animals.EatType.omni
_animal.Name = "Spot"
myAnimals.Add(_animal)

'this way creates an animal using the constructor accepting parameters and adds it (same addition as above, but different substantiation)
Dim _animal2 As New Animals("Cat", 3, Animals.EatType.carn, "Purrs")
myAnimals.Add(_animal2)

'this way adds it as it's being substantiated
myAnimals.Add(New Animals("Buzzard", 17, Animals.EatType.cari, "Buzzy"))

Console.WriteLine("You can then see that each is created and has values set.{0}" _
& "Total members of collection 'myAnimals' = {1}{0}", vbCrLf, myAnimals.Count)
Console.ReadLine()

WriteList(myAnimals)
Console.WriteLine("{0}Tada!", vbCrLf)
Console.ReadLine()

Catch ex As Exception
Console.WriteLine("{0}{0}{0}{1}", vbCrLf, ex.Message)
Console.ReadLine()
End Try
End Sub

Private Function addAnimalsNoParms() As Animals
Return New Animals()
End Function

Private Function addAnimalsWithParms(ByVal s As String, ByVal a As Integer, ByVal e As Animals.EatType, ByVal n As String) As Animals
Return New Animals(s, a, e, n)
End Function

Private Sub WriteList(ByVal animalList As List(Of Animals))
For Each a As Animals In animalList
Console.WriteLine("Animal at index of: {0} is Named: {1}, Age: {2}, Eats: {3}, and is a {4}", animalList.IndexOf(a), a.Name, a.Age, a.Eats, a.Species)
Next
End Sub
End Module

Public Class Animals

Public Enum EatType
herb 'herbivore
carn 'carnivore
omni 'omnivore
cari 'carion
Unknown
End Enum

Private _species As String
Private _age As Integer
Private _eats As EatType
Private _name As String

Sub New()
Species = "Unknown"
Age = -1
Eats = EatType.Unknown
Name = "Unknown"
End Sub

Sub New(ByVal s As String, ByVal a As Integer, ByVal e As EatType, ByVal n As String)
Species = s
Age = a
Eats = e
Name = n
End Sub

Public Property Species As String

Get
Return _species
End Get

Set(ByVal value As String)
_species = value
End Set

End Property

Public Property Age As Integer

Get
Return _age
End Get

Set(ByVal value As Integer)
_age = value
End Set

End Property

Public Property Eats As EatType

Get
Return _eats
End Get

Set(ByVal value As EatType)
_eats = value
End Set

End Property

Public Property Name As String

Get
Return _name
End Get

Set(ByVal value As String)
_name = value
End Set

End Property

End Class

AceInfinity
01-20-2013, 01:57 PM
GroceryItem is a class, and you need to create a class instance of GroceryItem to add to the basket collection.


Make sure all controls except txtScanNumber contain
a value.

Check if each control's text or value property is not Null or empty or the default value for that type i'm assuming. (Unless a default value is permissible for use, but that's better known by yourself I would think, since you've had more visibility to this assignment. If you're just checking for Null, there's no point because the controls from the list i'd assume you'd be using here can't contain a null value. They don't have nullable types, or the values that are strings, get converted from Null to String.Empty upon initialization, so compare with ""/String.Empty in that case.)

NOTE: For this to work:

txtBrandName.Text.Substring(0, 3)

txtBrandName.Text has to be a Length of 3 or greater.


Make sure to clear the content of all controls.

You can loop through the controls on the form container to do this instead of setting them all individually.



Starting at #15 not too sure i have a full understanding of what the control values are. I have uploaded a scan of the controls i'm working with. Also when i run the app i get a warning that reads: ArgumentOutOfRangeException was unhandled - Index and length must refer to a location within the string. Parameter name: length at the line txtScanNumber.text = _
txtBrandName.Text.Substring(0, 3) & 1019.

This is an online degree so i am basically on my own. I have also emailed the school asking for some assistance but have not received a reply.

Thanks for your help, appreciate it......

For a textbox, the "value" should be the Text property, and same with a few other controls, but some also have a Value property, which will hold the value in a more appropriate type for the represented "display" for that control, such as the NumericUpDown control, where Value holds a numeric value type, and instead of a string, for the Text property which also exists.



ok that error means that the txtBrandName.Text was probably less than 4 characters long. You need to ensure that it has at least 4 chars and then appens the numbers

This is wrong... From index 0 and a length of 3 for the second param, it can be less than 4 characters long, but no less than 3. The second param defines the length of the string, so that's the Integer value that we can't exceed here.

OP if you didn't see an error from not having quotes around your numbers, that value is being implicitly cast over to a string to be used as a string in that Text property, so I would suggest using Option Strict On at the very very top of your code to see any other locations where this is being done. Implicit type casting is bad on performance and therefore is also bad habit.


the substring looks at a peice of the string with the supplied parameters... for example


Dim strA As String = "Oatmeal"
Dim strB As String = strA.Substring(0,3)
Dim strC As String = strA.Substring(0,3) & "555"
Dim strD As String = "the cat " & "and dog"

'values of all of these...
'strA: Oatmeal
'strB: Oatm
'strC: Oatm555
'strD: the cat and dog


Your output values would be wrong, Substring 0, and a length of 3 should yield a string value consisting of 3 characters, not 4. strB should be "Oat", and strC should be "Oat555".


the '_' is a way to put what you want on a new line but still same statement...
ex


Dim s As String = _
"apples"


It can also be used to concatonate strings, but then you need to also use the ampersand


Dim s As String = _
"apples" _
& " make healthy" _
& " snacks"


Not necessarily, the _ cannot be used to concatenate strings, the ampersand is used to concatenate the strings, just to clarify so OP doesn't get the wrong idea as he's still learning.

alykins, your last code seems alright :), with the exception fo a few coding convention standards. You should only prefix variable identifiers with a "_" if they are a field, as this is usually that kind of indicator. I use "_" for my private fields specifically and public ones are for the most part Properties that I don't have prefixed with "_". I see the Animals class takes this into consideration but inside of your main Sub it shouldn't have things like that. Also, there's no need for this function:

Private Function addAnimalsWithParms(ByVal s As String, ByVal a As Integer, ByVal e As Animals.EatType, ByVal n As String) As Animals
Return New Animals(s, a, e, n)
End Function

When it's really just an added function call for no reason, if you can provide the params to use the overloaded constructor for the Animals class, then why not just create the instance directly for use? Adding an extra function call, understand, just takes extra time for no reason here.

@Sherby - What is the basket variable type? List(Of T)?

edit: Looking at the GroceryItem class a few things stuck out for me:

' ScanNumber property
Public ReadOnly Property ScanNumber() As Integer
Get
' Variable to hold the Number
Dim intScanNumber As Integer
Return intScanNumber
End Get
End Property

You don't want to be Dim'ing intScanNumber here, otherwise, it will always be returning 0 and since it's ReadOnly you can't change that at all. The internal scope here will take prescedence over that field variable with the same name.

With ReadOnly stuff in this class, it seems they want you, (unless you did this on your own,) to adopt a more OOP-like structure.

My advice to you as well is to avoid having similar variable names where possible:

Public Sub New(ByVal ScanNumber As Integer)
Me.intScanNumber = ScanNumber
End Sub

You already have a ReadOnly property named ScanNumber. This won't affect anything here since you're setting the backing value (which isn't used because of the Dim you have in that property Get anyways), to ScanNumber, but if you were setting the actual property itself, using ScanNumber over Me.ScanNumber could result in having issues.

Another thing i'll point out; Constructor chaining. You have this:

' Constructor
Public Sub New(ByVal ScanNumber As Integer)
Me.intScanNumber = ScanNumber
End Sub

' Constructor
Public Sub New(ByVal ScanNumber As Integer, ByVal BrandName As String, ByVal Price As Double)
Me.intScanNumber = ScanNumber
Me.strBrandName = BrandName
Me.dblPrice = Price
End Sub

Same variable setting in both. Take this for instance, chaining to a "master Constructor":


Public Sub New(ID as Integer)
Me.New(ID, "") 'This will call the other Constructor, but provide a default value that you want for the other param.
End Sub

Public Sub New(ID As Integer, Name As String)
_ID = ID
_Name = Name
End Sub

Just example code so i'm not rewriting the class for you.

Sherby
02-13-2013, 05:10 AM
Thank you guys for the guidance. I figured it out and got 100% on my project.

alykins
02-13-2013, 02:00 PM
congrats



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum