-
Feb 22nd, 2021, 10:54 PM
#1
Thread Starter
Lively Member
Automation???
Some of you are already familiar with my attempts to write a GroceryList program. I wrote it a long time ago in VB6 using 40 Listboxes, 40 RadioButtons, and 40 Buttons. I got a lot of flack on this forum about using so many controls. Now I rewriting it in VB.net VS2019. I'm using 4 Listboxes, 40 RadioButtons, and 16 Buttons, and saving the items in general in List Arrays.
But here is the problem. I am not using automation because I don't know how. The program IS working, but it's getting really long because I have to make a subroutine for each list with each one just having a different number next to the word "List" and/or "Listbox". And I'm having to do this over and over!!! How do I make a subroutine that will handle each one with the important info sent as arguments?
Here is some examples of what I'm talking about.
Example 1
Code:
Dim list0 As New List(Of String)
Dim list1 As New List(Of String)
Dim list2 As New List(Of String)
Dim list3 As New List(Of String)
Dim list4 As New List(Of String)
Example 2
Code:
Dim objWriter As New StreamWriter("C:\Grocery List\GList2.TXT")
objWriter.WriteLine("00;*;" & RadioButton0.Text) '
For Each Item As String In list0
objWriter.WriteLine(Item) '
Next
objWriter.WriteLine("01;*;" & RadioButton1.Text) '
For Each Item As String In list1
objWriter.WriteLine(Item) '
Next
objWriter.WriteLine("02;*;" & RadioButton2.Text) '
For Each Item As String In list2
objWriter.WriteLine(Item) '
Next
Example 3
Code:
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
ListBox0.Items.Clear()
ListBox0.Items.AddRange(list1.ToArray)
Else
list1.Clear()
For X As Integer = 0 To ListBox0.Items.Count - 1
If ListBox0.GetSelected(X) Then
list1.Add("+" & ";" & ListBox0.Items.Item(X).ToString)
Else
list1.Add("-" & ";" & ListBox0.Items.Item(X).ToString)
End If
Next
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
ListBox0.Items.Clear()
ListBox0.Items.AddRange(list2.ToArray)
Else
list2.Clear()
For X As Integer = 0 To ListBox0.Items.Count - 1
If ListBox0.GetSelected(X) Then
list2.Add("+" & ";" & ListBox0.Items.Item(X).ToString)
Else
list2.Add("-" & ";" & ListBox0.Items.Item(X).ToString)
End If
Next
End If
End Sub
Thanks
-
Feb 22nd, 2021, 11:00 PM
#2
Re: Automation???
This is just a general programming principle. You write a single method that contains all the common functionality and then you add a parameter for all the values that may vary. For instance, if you have multiple String collections that you want to write to different files, you write a method that has parameters for the file path and the collection that opens the file and writes the items to it. You can then call that method as many times as you like, passing a different file path and collection each time.
vb.net Code:
Private Sub WriteListToFile(list As IEnumerable(Of String), filePath As String)
File.WriteAllLines(filePath, list)
End Sub
vb.net Code:
WriteListToFile(list1, filePath1)
WriteListToFile(list2, filePath2)
It's your job to work out what is common and what can vary.
-
Feb 23rd, 2021, 06:39 AM
#3
Thread Starter
Lively Member
Re: Automation???
 Originally Posted by jmcilhinney
[HIGHLIGHT=vb.net]
Private Sub WriteListToFile(list As IEnumerable(Of String), filePath As String)
The "Obj as object" is allowed as a parameter, but is giving me an error when I use Obj in line 5. "BC30574: Option Strict On disallows late binding"
Then how do I pass an object (List and Obj) as an argument when I call this sub?
Thanks
Code:
1. Private Sub SaveFile(LstNum As Integer, list As IEnumerable(Of String), Obj As Object)
2.
3. Dim objWriter As New StreamWriter("C:\Grocery List\GList2.TXT")
4.
5. objWriter.WriteLine(LstNum & ";*;" & Obj.Text) '
6. For Each Item As String In list
7. objWriter.WriteLine(Item) '
8. Next
Last edited by vb6tovbnetguy; Feb 23rd, 2021 at 06:44 AM.
-
Feb 23rd, 2021, 07:30 AM
#4
Re: Automation???
Why are you declaring that parameter as type Object in the first place? If you know that it has a Text property then you must know that it is a type that has a Text property, so why aren't you declaring it as that type?
-
Feb 23rd, 2021, 09:17 AM
#5
Re: Automation???
In otherwords... it looks like you're only using the .Text property anyways... which is probably a String... so declare the parameter as a String, and rather than pass in the object, passing the .Text property in the first place. If you need the whole object, or more than just the Text property, look at what you're passing in... all controls inherit from the Control object... which is where the .Text and some of hte common properties come from, including Name. So if you're looking for more than just Text, look at typing the parameter As Control instead.
-tg
-
Feb 23rd, 2021, 09:55 AM
#6
Thread Starter
Lively Member
Re: Automation???
 Originally Posted by jmcilhinney
Why are you declaring that parameter as type Object in the first place? If you know that it has a Text property then you must know that it is a type that has a Text property, so why aren't you declaring it as that type?
In this case the Obj stands for a RadioButton, and I have to change the text property of each one, and that name is loaded from the disk. If there is an easier way then please explain?
Thanks
-
Feb 23rd, 2021, 10:00 AM
#7
Thread Starter
Lively Member
Re: Automation???
 Originally Posted by techgnome
In otherwords... it looks like you're only using the .Text property anyways... which is probably a String... so declare the parameter as a String, and rather than pass in the object, passing the .Text property in the first place. If you need the whole object, or more than just the Text property, look at what you're passing in... all controls inherit from the Control object... which is where the .Text and some of hte common properties come from, including Name. So if you're looking for more than just Text, look at typing the parameter As Control instead.
-tg
OK, great. I didn't understand in the previous comment, but now I do. I will make that change and pass the string as reference to change the text property of the RadioButtons.
Thanks
-
Feb 23rd, 2021, 10:05 AM
#8
Re: Automation???
I'm not clear on where the Object comes from, either, but in your original post you show a series of CheckChanged handlers. In case you weren't aware of it, the sender argument IS the checkbox, so you can write this:
Code:
Dim theCheckBox = DirectCast(sender,CheckBox)
And now you have the checkbox that raised the event. Therefore, the same event handler can handle the event from several different controls. So, you can see that your event handlers for the event are similar (though not identical) for the different checkboxes. You could have just one handler if you could choose which list to work with.
For that piece, there are several ways to go, but one point you should be aware of is the .Tag property that all controls have. The .Tag property is essentially an 'extra' property that can hold ANYTHING. It's type Object, so you can put anything you want into it. For example, you could put the list that should be changed into it, or you could just put an integer (1, 2, etc) into it that indicated which list should be worked with. So, by thinking about what you might put into the .Tag property, you might come up with a way to relate the control to whatever it will need in the event handler. For example, if you put the list into the .Tag property, you could pull it back out:
Code:
Dim theCheckBox = DirectCast(sender,CheckBox)
Dim theListToUse = DirectCast(theCheckBox.Tag,List(of String))
A couple points: I'm guessing that the list is a List(of String). If that's not what it is, then make it whatever it should be. Also, you aren't putting the list in there, you are just putting a reference to the list in there, so the list is sitting out in memory somewhere, and you are putting the address of that object into the .Tag property so that you can get to it. You aren't making a copy of the list and putting that into the .Tag.
My usual boring signature: Nothing
 
-
Feb 23rd, 2021, 06:52 PM
#9
Re: Automation???
 Originally Posted by vb6tovbnetguy
OK, great. I didn't understand in the previous comment, but now I do. I will make that change and pass the string as reference to change the text property of the RadioButtons.
Clearly you don't understand if you think that will work. If you pass in a String then you have no access to the RadioButton, so how are you going to do anything to it. tg specifically said that passing in the String would only work if the String was all you needed but if you need more than that then you need a different type. If you need to change a property of an object then you need that object. The thing is that Text is a property of the Control class and is then inherited by every specific control type, e.g. RadioButton. If you need to change the text of a control then the parameter can be type Control, but if you only intend to ever pass in RadioButtons then it can be type RadioButton instead. Some people think that the parameter should be the most specific type possible based on how you intend to use the method and some think that it should be the least specific type possible based on what the method does.
-
Feb 23rd, 2021, 10:58 PM
#10
Thread Starter
Lively Member
Re: Automation???
This is not working properly. Am I on the right track? Any pointers?
Code:
Private Sub RBChanged(RB As RadioButton, Lst As IEnumerable(Of String), LN As String)
Dim StrArray(2) As String
Dim Checked As Boolean
Dim ItemNum As Integer
If RB.Checked = True Then 'RB = RadioButton
ListBox0.Items.Clear()
MessageBox.Show("Clear")
For Each Item As String In Lst 'Lst = List
StrArray = Item.Split(CChar(";"))
If StrArray(1) = "+" Then Checked = True Else Checked = False
ItemNum = ListBox0.Items.Add(StrArray(2))
If Checked Then ListBox0.SetSelected(ItemNum, True)
Next
Else
list0.Clear()
For X As Integer = 0 To ListBox0.Items.Count - 1
If ListBox0.GetSelected(X) Then
list0.Add(LN & ";+;" & ListBox0.Items.Item(X).ToString)
Else
list0.Add(LN & ";-;" & ListBox0.Items.Item(X).ToString)
End If
Next
End If
End Sub
Private Sub RadioButton0_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton0.CheckedChanged
RBChanged(RadioButton0, list0, "00")
End Sub
-
Feb 23rd, 2021, 11:06 PM
#11
Re: Automation???
 Originally Posted by vb6tovbnetguy
This is not working properly.
Perhaps you could elaborate instead of expecting us to work out stuff that you already know. What exactly do you expect to happen and what actually does happen? When debugged the code properly, EXACTLY where and how did the code behave differently to how you expected?
-
Feb 24th, 2021, 12:13 AM
#12
Thread Starter
Lively Member
Re: Automation???
Oops. Sorry about that.
Well first off this code does work or did at least as an event handler and before I made it into a subroutine. It's purpose is to swap items (and thier checked status) in and out of list arrays/listboxes when I change my selection using RadioButtons. When I do that the first change seems to work, but then after that it doesn't. In this case the Listbox is always Listbox0.
1st direct question. Is a List Array the same as IEnumerable(Of String)?
2nd direct question. Did I setup my arguments/parameters correctly?
I am commenting the code more to make it more readable.
Code:
Private Sub RBChanged(RB As RadioButton, Lst As IEnumerable(Of String), LN As String)
Dim StrArray(2) As String 'Used with Split.
Dim Checked As Boolean 'Used to check checked items.
Dim ItemNum As Integer 'Used to check checked items.
If RB.Checked = True Then 'RB = RadioButton
ListBox0.Items.Clear() 'Clears list0
For Each Item As String In Lst 'Lst = List
StrArray = Item.Split(CChar(";")) 'Assign the
If StrArray(1) = "+" Then Checked = True Else Checked = False
ItemNum = ListBox0.Items.Add(StrArray(2))
If Checked Then ListBox0.SetSelected(ItemNum, True)
Next
Else
list0.Clear()
For X As Integer = 0 To ListBox0.Items.Count - 1
If ListBox0.GetSelected(X) Then
list0.Add(LN & ";+;" & ListBox0.Items.Item(X).ToString)
Else
list0.Add(LN & ";-;" & ListBox0.Items.Item(X).ToString)
End If
Next
End If
End Sub
Private Sub RadioButton0_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton0.CheckedChanged
RBChanged(RadioButton0, list0, "00")
End Sub
-
Feb 24th, 2021, 01:02 AM
#13
Re: Automation???
 Originally Posted by vb6tovbnetguy
1st direct question. Is a List Array the same as IEnumerable(Of String)?
There's no such thing as a List Array. An array is an array, which is a fundamental data structure in .NET. A List(Of T) is a specific class that provides similar functionality to an array can also grow and shrink dynamically, whereas an array is of a fixed-size once created.
IEnumerable(Of T) is an interface that that many types implement, including arrays and List(Of T). Basically, any object that implements IEnumerable can be the source for a For Each loop and IEnumerable(Of T) is the generic version of that interface, much like List(Of T) is the generic equivalent of the ArrayList class.
If all you do with an object is enumerate it using a For Each loop then the reference you use to access it, i.e. the variable or parameter or whatever, doesn't need to be any more specific than IEnumerable or IEnumerable(Of T). That enables you to assign different types of objects and the code still work the same way.
 Originally Posted by vb6tovbnetguy
2nd direct question. Did I setup my arguments/parameters correctly?
I guess so. Do they do what you need done?
-
Feb 24th, 2021, 01:05 AM
#14
Re: Automation???
I'm still not really clear on what you're actually trying to achieve here. You got some useless comments in your code but none that actually explain why the code does what it does, which is what comments should be for. I'm guessing that the code code be written better than it is but with no clear idea of what it's actually supposed to do, who can really say? A full and clear explanation might be useful, which could include an example of what the two lists look like before and after checking and unchecking a particular RadioButton.
-
Feb 24th, 2021, 01:41 AM
#15
Re: Automation???
@JMC
If I remember it's regarding some Grocerylist, I gave him a sample with all the grocery items
seperated in a Treeview by Meatproducts;Dairy;Sweets...etc... to select from.
@VB6ToVBNetGuy
if you still want to use Listboxes, then use the Checkedlistbox
here a sample
Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
CheckedListBox1.CheckOnClick = True
'sample Data
For i = 8 To 17
CheckedListBox1.Items.Add(Format(i, "00") & ":00")
CheckedListBox1.Items.Add(Format(i, "00") & ":30")
Next
End Sub
Private Sub CheckedListBox1_ItemCheck(sender As Object, e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
'get number of checked items
Dim count As Integer = CheckedListBox1.CheckedItems.Count
If e.NewValue = CheckState.Unchecked Then
count -= 1
ElseIf e.NewValue = CheckState.Checked Then
count += 1
End If
Label1.Text = CheckedListBox1.Items.Count & " Items in List, there are (" & count & ") selected"
End Sub
Public Sub SaveData(ByVal FileName As String, ByVal CListBox As CheckedListBox)
'save the checked/unchecked status of the checkedlistbox
Dim sb As New System.Text.StringBuilder
For Index As Integer = 0 To CListBox.Items.Count - 1
sb.AppendLine(String.Format("{0};{1}",
If(CListBox.GetItemChecked(Index), True, False), CListBox.Items(Index)))
Next
IO.File.WriteAllText(FileName, sb.ToString)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
SaveData("D:\TestFolder\TestSave.txt", CheckedListBox1)
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Feb 25th, 2021, 08:22 AM
#16
Thread Starter
Lively Member
Re: Automation???
 Originally Posted by jmcilhinney
I'm still not really clear on what you're actually trying to achieve here. You got some useless comments in your code but none that actually explain why the code does what it does, which is what comments should be for. I'm guessing that the code code be written better than it is but with no clear idea of what it's actually supposed to do, who can really say? A full and clear explanation might be useful, which could include an example of what the two lists look like before and after checking and unchecking a particular RadioButton.
I'm sorry about that. I was so sleepy when I made that note that I couldn't think straight. I was just hoping that the amount of information that I put on it would be helpful.
The purpose of that code is to swap the information in the listbox0 from one list(of T) to the next as I select a new RadioButton.
As of now I have very little time as I have to get my day started. To make a long story short, I got it working perfectly, but now this really crazy error has popped up. There is no red line in to code when I go to look at the line it mentions. That line is just like all the rest and they are not getting this error.
ReadOnly list15 As New List(Of String)
Severity Code Description Project File Line Suppression State
Error BC32500 'GuidAttribute' cannot be applied because the format of the GUID '33d33c9781-0d0c-433330-b4533-dd754f07765a' is not correct. StructGList C:\Users\Owner\Documents\Visual Studio 2010\Projects\StructGList\StructGList\My Project\AssemblyInfo.vb 21 Active
-
Feb 25th, 2021, 06:54 PM
#17
Thread Starter
Lively Member
Re: Automation???
I looked it up, but I don't understand the answer. Does anyone here understand this?
BC32500: '<attribute>' cannot be applied because the format of the GUID '<number>' is not correct
07/20/2015
2 minutes to read
+7
A COMClassAttribute attribute block specifies a globally unique identifier (GUID) that does not conform to the proper format for a GUID. COMClassAttribute uses GUIDs to uniquely identify the class, the interface, and the creation event.
A GUID consists of 16 bytes, of which the first eight are numeric and the last eight are binary. It is generated by Microsoft utilities such as uuidgen.exe and is guaranteed to be unique in space and time.
Error ID: BC32500
To correct this error
Determine the correct GUID or GUIDs necessary to identify the COM object.
Ensure that the GUID strings presented to the COMClassAttribute attribute block are copied correctly.
Last edited by vb6tovbnetguy; Feb 25th, 2021 at 06:58 PM.
-
Feb 28th, 2021, 07:55 AM
#18
Thread Starter
Lively Member
Re: Automation???
 Originally Posted by Shaggy Hiker
Code:
Dim theCheckBox = DirectCast(sender,CheckBox)
Dim theListToUse = DirectCast(theCheckBox.Tag,List(of String))
A couple points: I'm guessing that the list is a List(of String). If that's not what it is, then make it whatever it should be. Also, you aren't putting the list in there, you are just putting a reference to the list in there, so the list is sitting out in memory somewhere, and you are putting the address of that object into the .Tag property so that you can get to it. You aren't making a copy of the list and putting that into the .Tag.
I need to save several controls in the tag. Can I do it like this?
Code:
Listbox0.tag=ListBox0;RadioButton3;List3;03
Private Sub RBChanged(LB As ListBox, RB As RadioButton, Lst As List(Of String), LN As String)
and then break it up using split, and then somehow use the controls that have the same name as the strings?
I am trying to move all the items from the listboxes to storage in the Lists before saving to disk as the save function only saves from the Lists. I already have a subroutine that does this, but it requires parameters in the form of certain controls to work.
Thanks
Last edited by vb6tovbnetguy; Feb 28th, 2021 at 10:37 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|