-
Jan 1st, 2021, 06:32 PM
#41
Thread Starter
Lively Member
Re: Control Array?
Maybe you could replace both of these msgs with identical ones that are commented. I really don't understand any of it.
-
Jan 1st, 2021, 07:28 PM
#42
Re: Control Array?
Originally Posted by vb6tovbnetguy
I do know that my solution is simple. I just fill listboxes and use zorder to bring the needed one to the front.
I have been using that for years and it's never caused me a problem.
OK. Please tell me how the other solution would be implemented?
If you're implying that your 40 groups of controls are stacked and only the group at the top of the z-order is displayed at any one time, then you're definitely doing it wrong. You only need 1 group, not 40.
-
Jan 1st, 2021, 07:28 PM
#43
Re: Control Array?
Ok. See if this helps...
Code:
Public Class Form11
'this is a 2D jagged array (an array of arrays)
'there are 3 fixed content arrays
Dim items(2)() As String
Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'first with 5 string elements
items(0) = New String() {"one", "two", "three", "four", "five"}
'second with 3 string elements
items(1) = New String() {"six", "seven", "eight"}
'last with 4 string elements
items(2) = New String() {"nine", "ten", "eleven", "twelve"}
'simulate button click on Button1
Button1.PerformClick()
End Sub
Private Sub Buttons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
'clear the listbox
ListBox1.Items.Clear()
'array of existing buttons
Dim buttons As Button() = New Button() {Button1, Button2, Button3}
'find which button was clicked
Dim index As Integer = Array.IndexOf(buttons, DirectCast(sender, Button))
'add correct array to listbox items
ListBox1.Items.AddRange(items(index))
'loop through existing radiobuttons, if any exist
For Each rb As RadioButton In Me.Controls.OfType(Of RadioButton).ToList
'remove the CheckedChanged handler
RemoveHandler rb.CheckedChanged, AddressOf RadioButtons_CheckedChanged
'remove the radiobutton
rb.Dispose()
Next
'self explanatory y location on the form
Dim yPosition As Integer = 110
'loop through listbox items
For Each s As String In ListBox1.Items
'create new radiobutton and set some of its properties
'and add the CheckedChanged handler
Dim rb As New RadioButton
rb.Text = s
rb.Location = New Point(175, yPosition)
'add the new radiobutton to the form
Me.Controls.Add(rb)
AddHandler rb.CheckedChanged, AddressOf RadioButtons_CheckedChanged
'increment the y location on the form, for the next radiobutton (if there is another)
yPosition += rb.Height + 6
Next
End Sub
Private Sub RadioButtons_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
' Dynamic CheckedChanged handler
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 1st, 2021, 08:26 PM
#44
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by Niya
If you're implying that your 40 groups of controls are stacked and only the group at the top of the z-order is displayed at any one time, then you're definitely doing it wrong. You only need 1 group, not 40.
You guys are so funny. Most of you are telling me I'm doing it wrong. What does wrong mean? I think if it works then it's not wrong. The way I've done it works, and if I had tried to do it any other way it wouldn't have worked because this is the only way I understand. I'm sure there are better ways of doing it and as I learn those ways I'll put them into practice.
Right now I AM considering using 1 group of 4 listboxes and storing the data in arrays. That seems like (based on the feedback I'm getting) it would be better, and I shouldn't have to learn that much more than I currently know in order to implement it.
Wait. I just realized that the only way I can select items is for them to be in listboxes. I guess I'm no longer considering using arrays to hold the data.
-
Jan 1st, 2021, 08:38 PM
#45
Re: Control Array?
The items are stored in the arrays, then displayed in the listbox on demand. From there you can select any item you want... There are methods that can be used for storing selected items. What happens after selecting items? Do they go straight to print? I can help you if you explain clearly...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 1st, 2021, 08:46 PM
#46
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by .paul.
The items are stored in the arrays, then displayed in the listbox on demand. From there you can select any item you want... There are methods that can be used for storing selected items. What happens after selecting items? Do they go straight to print? I can help you if you explain clearly...
They go to print when I click the print button. Before that I save all the items in all the listboxes and their selected status in 2 separate text files.
In the msg were I explained the program in detail I did mention that I only print selected items. Didn't mention that it gets saved, but that should go without saying.
-
Jan 1st, 2021, 09:16 PM
#47
Re: Control Array?
Try this. It follows the steps you described, but uses a CheckedListBox as it's a more intuitive selection process...
Code:
Public Class Form11
'this is a 2D jagged array (an array of arrays)
'there are 3 fixed length arrays
Dim items(2)() As String
'this stores selected items
Dim selectedItems As New List(Of String)
'for printing
Private WithEvents pd As New Printing.PrintDocument
Private ppd As PrintPreviewDialog
'printed page y coordinate
Dim yPosition As Integer
'for multipage printing
Dim startAt As Integer
Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'first with 5 string elements
items(0) = New String() {"one", "two", "three", "four", "five"}
'second with 3 string elements
items(1) = New String() {"six", "seven", "eight"}
'last with 4 string elements
items(2) = New String() {"nine", "ten", "eleven", "twelve"}
'simulate button click on Button1
Button1.PerformClick()
End Sub
Private Sub Buttons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
'get the selected items from the CheckedListBox
Dim selitems() As String = CheckedListBox1.CheckedItems.Cast(Of String).ToArray
'store the selected items
selectedItems.AddRange(selitems)
'clear the listbox
CheckedListBox1.Items.Clear()
'array of existing buttons
Dim buttons As Button() = New Button() {Button1, Button2, Button3}
'find which button was clicked
Dim index As Integer = Array.IndexOf(buttons, DirectCast(sender, Button))
'add correct array to listbox items
CheckedListBox1.Items.AddRange(items(index))
'loop through existing radiobuttons, if any exist
For Each rb As RadioButton In Me.Controls.OfType(Of RadioButton).ToList
'remove the CheckedChanged handler
RemoveHandler rb.CheckedChanged, AddressOf RadioButtons_CheckedChanged
'remove the radiobutton
rb.Dispose()
Next
'self explanatory y location on the form
Dim yPosition As Integer = 110
'loop through listbox items
For Each s As String In CheckedListBox1.Items
'create new radiobutton and set some of its properties
'and add the CheckedChanged handler
Dim rb As New RadioButton
rb.Text = s
rb.Location = New Point(175, yPosition)
'add the new radiobutton to the form
Me.Controls.Add(rb)
AddHandler rb.CheckedChanged, AddressOf RadioButtons_CheckedChanged
'increment the y location on the form, for the next radiobutton (if there is another)
yPosition += rb.Height + 6
Next
End Sub
Private Sub RadioButtons_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
' Dynamic CheckedChanged handler
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
'get the selected items from the CheckedListBox
Dim selitems() As String = CheckedListBox1.CheckedItems.Cast(Of String).ToArray
'store the selected items
selectedItems.AddRange(selitems)
startAt = 0
'this will print to a preview dialog
'to print to your printer
'replace these 4 lines with...
'pd.print
ppd = New PrintPreviewDialog
ppd.Document = pd
ppd.WindowState = FormWindowState.Maximized
ppd.ShowDialog()
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pd.PrintPage
'loop through stored items, printing one page at a time
For x As Integer = startAt To selectedItems.Count - 1
If x = startAt Then
'if start of new page
yPosition = e.MarginBounds.Top
End If
'draw to printer graphics object
e.Graphics.DrawString(selectedItems(x), Me.Font, Brushes.Black, e.MarginBounds.Left, yPosition)
'increment the y location for the next item
yPosition += 18
'if we've reached the bottom of the page
If yPosition > e.MarginBounds.Bottom Then
'request printpage runs again
e.HasMorePages = True
'set starting item for new page
startAt = x + 1
'break out of loop
Exit For
End If
'next iteration
Next
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 1st, 2021, 09:19 PM
#48
Re: Control Array?
In btnPrint, it retrieves any selected items that weren't caught when you changed lists...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 1st, 2021, 10:29 PM
#49
Re: Control Array?
Originally Posted by vb6tovbnetguy
If that is so then why is there so much jibberish out there where it comes to control arrays?
Because there are so many people like you who want to use VB.NET but want to use it like VB6, so there are lots of people trying to hack VB.NET to make it work that way. If you just learned how to use VB.NET properly then none of this would be an issue. The fact that you keep talking about control arrays is part of the problem. Control arrays are not a thing in VB.NET so get over it. I've already explained how to handle events for multiple controls so, if that's what you want to do, you already know how to do it. You can create an array of controls in code but there's no reason to do that beyond the same reasons that you might create an array of Integers or Strings. There are plenty of people in this thread who want to help you write good VB.NET code. The reason that there is animosity is because you keep trying to get us to help you write bad code. As someone who spends a lot of time answering questions here and on other sites because I want to help people be the best developer they can be, I can tell you that it's very difficult to make yourself help someone do the wrong thing. When you get right down to it, we're here for us, not you. We're here because it feels good to help someone write good code. It doesn't feel good to help someone write bad code, hence we don't want to do it.
-
Jan 1st, 2021, 10:30 PM
#50
Re: Control Array?
Just to throw in yet another bit of confusion, you'll want to look at the List(of T) rather than arrays pretty quickly. Arrays are useful if you have a fixed set of items, but arrays can't change size. Yeah, you may have used Redim or Redim Preserve, but what that is doing is creating a new array, and, if you use Preserve, copying the data from the old to the new. This isn't efficient. It wasn't efficient in VB6, either, but you didn't have a choice. The List(of T) greatly improves that. Technically, it does the same thing, since there's an array at the heart of a List, but it handles all the resizing on demand, and does it far more efficiently.
The List has .Add, Insert, Remove, and RemoveAt methods, so you can add and remove items at will. You can also make them arrays with .ToArray, and arrays into Lists with .ToList, though you generally want to just make them Lists and leave them as such.
Arrays were all you had in VB6, Lists are what you will use more often in .NET simply because you can change the size of them more efficiently and more easily.
My usual boring signature: Nothing
-
Jan 1st, 2021, 11:42 PM
#51
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by jmcilhinney
Because there are so many people like you who want to use VB.NET but want to use it like VB6, so there are lots of people trying to hack VB.NET to make it work that way. If you just learned how to use VB.NET properly then none of this would be an issue. The fact that you keep talking about control arrays is part of the problem. Control arrays are not a thing in VB.NET so get over it. I've already explained how to handle events for multiple controls so, if that's what you want to do, you already know how to do it. You can create an array of controls in code but there's no reason to do that beyond the same reasons that you might create an array of Integers or Strings. There are plenty of people in this thread who want to help you write good VB.NET code. The reason that there is animosity is because you keep trying to get us to help you write bad code. As someone who spends a lot of time answering questions here and on other sites because I want to help people be the best developer they can be, I can tell you that it's very difficult to make yourself help someone do the wrong thing. When you get right down to it, we're here for us, not you. We're here because it feels good to help someone write good code. It doesn't feel good to help someone write bad code, hence we don't want to do it.
I never asked anyone to write any code for me. I came on here and asked a question, which you answered. After that people asked questions about my project and it morphed into a monster with people trying to force me to do things their way instead of mine. I did a couple of times asked for explanation of suggestions, but I didn't want code. The code that was written I won't use because I don't understand it.
I don't want people mad at me because I will surely have questions in the future, but it would be best if it stopped at giving the answer. My programming will only get better as I learn more over time not with others writing my code for me.
If you want to give a suggestion, then just a command and syntax. As I was writing my last code I was having to use 2 loops to add a 2 digit number to a string(1 for 0-9 and another for over 9). Then I ran across the command "Format". That allowed me to use 1 loop that was more streamlined than before.
And FYI I am trying to learn VB.net. I am reading a book "Teach Yourself Visual Basic 2010" by James Foxall.
Now I have a question for you.
Originally Posted by jmcilhinney
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim clickedButton = DirectCast(sender, Button)
MessageBox.Show("You clicked " & clickedButton.Name)
End Sub
What does the second line do? Is it part of the "Control Array", or something else? Please explain?
Thanks
Last edited by vb6tovbnetguy; Jan 1st, 2021 at 11:52 PM.
-
Jan 2nd, 2021, 12:00 AM
#52
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by Shaggy Hiker
Just to throw in yet another bit of confusion, you'll want to look at the List(of T) rather than arrays pretty quickly. Arrays are useful if you have a fixed set of items, but arrays can't change size. Yeah, you may have used Redim or Redim Preserve, but what that is doing is creating a new array, and, if you use Preserve, copying the data from the old to the new. This isn't efficient. It wasn't efficient in VB6, either, but you didn't have a choice. The List(of T) greatly improves that. Technically, it does the same thing, since there's an array at the heart of a List, but it handles all the resizing on demand, and does it far more efficiently.
The List has .Add, Insert, Remove, and RemoveAt methods, so you can add and remove items at will. You can also make them arrays with .ToArray, and arrays into Lists with .ToList, though you generally want to just make them Lists and leave them as such.
Arrays were all you had in VB6, Lists are what you will use more often in .NET simply because you can change the size of them more efficiently and more easily.
How do I initialize or dimension a global 2 dimensional array that would be accessible by all procedures? I have found info online, but when I try them they don't work.
I haven't looked up lists. Do they work the same way?
Thanks
Last edited by vb6tovbnetguy; Jan 2nd, 2021 at 12:11 AM.
-
Jan 2nd, 2021, 12:10 AM
#53
Re: Control Array?
Originally Posted by vb6tovbnetguy
Why would I throw out a very simple solution that I have already implemented to go with something very complex?
Originally Posted by wes4dbt
As I asked earlier, why are you rewriting this in VB .Net. If you like it the way it is then just solve your printing problem. But if you want to learn VB .Net than don't try and force VB .Net to be VB6.
Originally Posted by vb6tovbnetguy
I never asked anyone to write any code for me. I came on here and asked a question, which you answered. After that people asked questions about my project and it morphed into a monster with people trying to force me to do things their way instead of mine. I did a couple of times asked for explanation of suggestions, but I didn't want code. The code that was written I won't use because I don't understand it.
I don't want people mad at me because I will surely have questions in the future, but it would be best if it stopped at giving the answer. My programming will only get better as I learn more over time not with others writing my code for me.
If you want to give a suggestion, then just a command and syntax. As I was writing my last code I was having to use 2 loops to add a 2 digit number to a string(1 for 0-9 and another for over 9). Then I ran across the command "Format". That allowed me to use 1 loop that was more streamlined than before.
And FYI I am trying to learn VB.net. I am reading a book "Teach Yourself Visual Basic 2010" by James Foxall.
Now I have a question for you.
What does the second line do? Is it part of the "Control Array", or something else? Please explain?
Thanks
it casts the type Object sender to type Button
Originally Posted by vb6tovbnetguy
How do I initialize or dimension a global 2 dimensional array that would be accessible by all procedures? I have found info online, but when I try them they don't work.
Thanks
For true global, Add a module to your code and declare as i showed you with the items()() array that i used in the example i posted. I'd also recommend a parallel Boolean array to track selected in all of the lists. With the second array, you can restore checked selected items when returning to a list (of strings)
Code:
Module Module1
'this is a 2D jagged array (an array of arrays)
'there are 3 fixed length arrays
Public items(2)() As String
'this is a parallel jagged array to hold selections
Public selected(2)() As Boolean
End Module
Code:
Private Sub Form11_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'first with 5 string elements
items(0) = New String() {"one", "two", "three", "four", "five"}
selected(0) = New Boolean() {False, False, False, False, False}
'second with 3 string elements
items(1) = New String() {"six", "seven", "eight"}
selected(1) = New Boolean() {False, False, False}
'last with 4 string elements
items(2) = New String() {"nine", "ten", "eleven", "twelve"}
selected(2) = New Boolean() {False, False, False, False}
'simulate button click on Button1
Button1.PerformClick()
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 2nd, 2021, 12:19 AM
#54
Re: Control Array?
That's just how i'd do things in a program such as yours. If you think we're trying to force you into things that are unnecessary and not what you're comfortable with, you might as well take your vb6 project and go get printing help in the classic vb forum...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 2nd, 2021, 12:48 AM
#55
Re: Control Array?
If you want to learn more about where to declare variables, it’s called scope...
https://docs.microsoft.com/en-us/dot...-of-a-variable
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 2nd, 2021, 01:14 AM
#56
Re: Control Array?
Originally Posted by vb6tovbnetguy
What does the second line do?
Specifically, it casts the sender parameter, which is declared as type Object, as type Button. The sender parameter in an event handler is always a reference to the object that raised the event. Because, as we have explained, a single method can be used to handle events for multiple objects, sender is always declared as type Object so that events of objects of different types be handled, e.g. you might handle the SelectedIndexChanged event of a ComboBox and the TextChanged event of a TextBox with the same method. As is always the case, if you want to access members of an object then you need a reference of a type that has those members. In my example, I use the Name property but the Object class has no Name property. As the object raising the event will always be a Button, it is safe to cast the sender as type Button in order to access that Name property. As Name is a property of the Control class, so all controls have that property, it would have been sufficient to cast as type Control. If different types of controls were raising the event then it would have been necessary to cast as type Control, so it would work for all controls.
Originally Posted by vb6tovbnetguy
Is it part of the "Control Array", or something else?
There is no frickin' control array!
-
Jan 2nd, 2021, 07:41 AM
#57
Re: Control Array?
What is it with VB6 programmers and Control Arrays? Even when I was a VB6 programmer I thought it was a garbage concept. Why do people fall so hard in love with it? Every time a VB6 programmer switches to .Net, it's always the issue that seems to cause the most waves.
-
Jan 2nd, 2021, 08:26 AM
#58
Re: Control Array?
Originally Posted by Niya
What is it with VB6 programmers and Control Arrays? Even when I was a VB6 programmer I thought it was a garbage concept. Why do people fall so hard in love with it? Every time a VB6 programmer switches to .Net, it's always the issue that seems to cause the most waves.
Creating an array in code is apparently an overly arduous task but only VB6 developers have ever noticed. I have to say, I really don't know what is that they use them for so much because it's actually not all that common that I find myself needing an array of controls. There's obviously the event thing but, even when it's pointed out that that is irrelevant in VB.NET, many of them still go on about control arrays like it was an organ or a limb that doing without will be almost impossible.
-
Jan 2nd, 2021, 09:24 AM
#59
Re: Control Array?
@vb6tovb.Net
why not use a Treeview and load all your Shoppingcart Items from a Database.
see the Image I loaded the Data from the sample Database Northwind
from that Treeview you can then select items and add that to your shoppingcart
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.
-
Jan 2nd, 2021, 09:43 AM
#60
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by jmcilhinney
vb.net Code:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim clickedButton = DirectCast(sender, Button)
MessageBox.Show("You clicked " & clickedButton.Name)
End Sub
I was just looking at this code and started wondering how I use it to refer to members of other groupings.
In VB6 it was so easy. Just use the index of the button clicked. Reference radiobutton(index) and listbox(index). How would I do that with this code?
Thanks
-
Jan 2nd, 2021, 09:54 AM
#61
Re: Control Array?
Originally Posted by vb6tovbnetguy
In VB6 it was so easy.
You're welcome to go back to VB6 and do just that then. I'm outta here.
-
Jan 2nd, 2021, 10:08 AM
#62
Re: Control Array?
I don't think any of us WOULD reference the other controls, because I don't think any of us would use other controls. Personally, I would have one set of controls, but then hold the information behind the scenes, and only use the interface elements to display the part I wanted to display, certainly not to hold anything. It would JUST be display.
However, I also recognize that I'd be doing that because of....yet another code construct that wasn't as much of a thing in VB6. You talk about muli-dimensional arrays. I'm pretty sure that I understand what you are thinking, and I've done the same thing in VB6, but multi-D arrays weren't great in VB6 and they are almost never used in .NET. The problem with arrays like that is that YOU had to do most of the work involving them. For example, you couldn't readily resize them, and often had to do so in a fairly manual fashion by doing the sizing and moving old to new. Another issue with them was that they required you to keep straight, to some extent, what was found in each part. Sure, they were easy to reference, but that was the ONLY thing they had going for them. They turned into a major pain if you needed to hold different types, or more than just two items.
Classes and structures existed in VB6, but you could write a pretty big, and good, program without using either one. That's not the case in .NET. Everything is a class. Even a form is a class (note that all your code is part of a Class definition in the code page of any form you have). In your case, a class will work better....mostly. You could have a simple class:
Code:
Public Class YourClass
Public ItemName As String
Public Selected As Boolean
End Class
Then you could have a list of that class:
Code:
Public mySet As New List(of YourClass)
Now you don't need a multidimensional array at all. Each item holds both the name and the selection state. You could readily go beyond that, too, because it seems likely that you'd want a quantity field in there, so the class might become:
Code:
Public Class YourClass
Public ItemName As String
Public Selected As Boolean
Public Quantity As Integer
End Class
This is why multidimensional arrays are rarely found in .NET. They are always a pain to work with, and it gets worse if you understand how they are laid out in memory (which is what makes them a pain to work with, for that matter). With the importance of classes in .NET, almost every use of multidimensional arrays has gone away. Rather than trying to keep track of what each dimension meant, you just create an object that has all the properties you want, then you can create a single dimensional array (or a List) of that object type. Single dimensional arrays were always easier to work with than multidimensional arrays in either VB6 or .NET.
Of course, the class I showed there isn't really well made. Generally, it is better practice not to have public members as I showed, but to use properties to expose otherwise private members. That has gotten easier and easier over the years, to the point that the class could now be written:
Code:
Public Class YourClass
Public Property ItemName As String
Public Property Selected As Boolean
Public Property Quantity As Integer
End Class
Which would be more conventional. You can go FAR further with a class, too, such as writing out getter and setters for the properties such that you can do other things with them, add constructors to make it easier to create them, and add decorations to facilitate writing them out.
However, it also occurs to me that the data you have makes more sense in a datatable, and that could have as many fields as you wanted, too. Then there wouldn't be an array, one dimensional or otherwise. With Dataviews, you can subdivide that in any way you wanted to with single lines, but that's a whole different area.
My usual boring signature: Nothing
-
Jan 2nd, 2021, 10:13 AM
#63
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by jmcilhinney
You're welcome to go back to VB6 and do just that then. I'm outta here.
I can't go back to VB6. It won't work on my computer. I'm sorry that all the other craziness went on, but this was my original question and the only thing I need to know. If none of you guys will answer this question, then all of this was for nothing.
-
Jan 2nd, 2021, 10:42 AM
#64
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by ChrisE
@vb6tovb.Net
why not use a Treeview and load all your Shoppingcart Items from a Database.
see the Image I loaded the Data from the sample Database Northwind
from that Treeview you can then select items and add that to your shoppingcart
That sounds interesting. I'll have to look into it.
Thanks
-
Jan 2nd, 2021, 11:10 AM
#65
Re: Control Array?
If you want to continue with .NET, you have to forget most VB6 habits. Learn about user controls, different layout panels (TableLayoutPanel, FlowLayoutPanel) which are using different layout engine that places controls automatically in different way and how to create own events and how to use them by manually adding event handlers in code.
User controls will help you visually group the items in single visual control which you can place in your forms and panels (including the layout panels mentioned above). If you change color or font in user control then all instances will be automatically updated.
Start thinking in objects of your user interface - you don't just show text box + combo box + radio button + button. This is single object representing single item in your grocery list. If you create your user control for single object (grocery item) and put all these controls (radio, combo, text, button) then you are representing single object. And .NET is modeled by using objects for everything. You will create your class for single grocery item, when reading from database you can create object instance of your user control and then set the grocery item. In your user control you expose public method to set the item and this method will also know how to show the information - put item name in the text box, if the item is selected then change the radio button to be selected, expose your own events that you can handle in your main form where you add one or more instances in FlowLayoutPanel and you can have pretty good looking list if you designed visually your user control.
Later, if you want to add picture of the item, you don't have to add 40 picture boxes. Add the picture as property in your grocery item class, add reading of the image from the database, and finally re-design visually your user control to fit the picture with the other controls and set the image of the picture box.
If you want to show list of grocery items for another purpose, e.g. to have "favorites" feature, then you just use same user control you created. This time user favorites are read from database (e.g. 8 items) and you fill in another FlowLayoutPanel.
The transition of this kind of thinking (which is also available in VB6 but people don't want to do it that way) will take time. It is not 5 minutes to create 3x40 controls in the form designer and use the index in event handlers. But it will help design better apps and make life easier if you continue with .NET development.
-
Jan 2nd, 2021, 11:55 AM
#66
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by Shaggy Hiker
Code:
Public Class YourClass
Public ItemName As String
Public Selected As Boolean
End Class
Then you could have a list of that class:
Code:
Public mySet As New List(of YourClass)
Now you don't need a multidimensional array at all. Each item holds both the name and the selection state. You could readily go beyond that, too, because it seems likely that you'd want a quantity field in there, so the class might become:
Code:
Public Class YourClass
Public ItemName As String
Public Selected As Boolean
Public Quantity As Integer
End Class
This is why multidimensional arrays are rarely found in .NET.
Code:
Public Class YourClass
Public Property ItemName As String
Public Property Selected As Boolean
Public Property Quantity As Integer
End Class
.
Thanks for this information. I understand it and will use it in my project.
Right now my idea is to store the data in whatever way I store it and move it to list boxes as I need it and then delete all the information that is stored and then manipulate the data in the list box and then move the data back to storage when I'm done with it.
Last edited by vb6tovbnetguy; Jan 2nd, 2021 at 12:00 PM.
-
Jan 2nd, 2021, 02:23 PM
#67
Re: Control Array?
Datatables are particularly easy to store/recover from XML files. They have WriteXml and ReadXml methods, so storing and reading is as easy as providing a file name (and path), but you sure wouldn't want to be printing that.
My usual boring signature: Nothing
-
Jan 2nd, 2021, 03:57 PM
#68
Re: Control Array?
Originally Posted by vb6tovbnetguy
I was just looking at this code and started wondering how I use it to refer to members of other groupings.
In VB6 it was so easy. Just use the index of the button clicked. Reference radiobutton(index) and listbox(index). How would I do that with this code?
Thanks
I think the points that people have made are valid and you should learn VB.Net and take advantage of it.
Redesigning your application with VB.Net in mind, and essentially writing it from scratch, but with improved approaches to how to accomplish it, is a good thing and will be worth the effort if you have the time.
But, I've had the case where I had a VB6 application that I wanted to port to run in the VB.Net environment, but with minimal changes to the existing code, as the logic and methodology has already been worked out and in use for years, and I would like to port it in hours, not days or weeks.
I've also had the case where I did want to sit, and based on lessons learn, rewrite the application in VB.Net, with improvements in the usability and capability of the application. This second case can take a long time, depending on the complexity of the original application.
But for the first case, where I would like to modify the code as little as possible for a quick port, then what I've done for the control arrays is a combination of some of the things that jmc and others have shown. In particular, the point you bring up in the quote.
Basically there are two things to do. Actually there are different ways to go about it, but this is what I did.
1. Set the .Tag property of the controls to the Index value you want them to be.
2. Create arrays of references to those controls in the order you have them indexed.
That way, in the event handler, you get the Index value from the tag property to use as needed, and you can use that index to index into the arrays of controls to access the other controls.
As an example, I just added four buttons (but you can add more or less) to a form, and a corresponding four radiobuttons to the form.
Then this code initializes the Array of controls to hold references to the controls, and then does a loop to set the tags to the index value.
When you click on the button, you will see the corresponding radio button be set.
Code:
Public Class Form1
Private Buttons() As Button
Private RadButtons() As RadioButton
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Buttons = {Button1, Button2, Button3, Button4}
RadButtons = {RadioButton1, RadioButton2, RadioButton3, RadioButton4}
For i As Integer = 0 To UBound(Buttons)
Buttons(i).Tag = i
RadButtons(i).Tag = i
Next
End Sub
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
Dim btn = DirectCast(sender, Button)
Dim index = CInt(btn.Tag)
RadButtons(index).Checked = True
End Sub
End Class
p.s. To generate the Click event handler for the buttons, I selected all the buttons in the IDE, then in the Properties window, selected the Events list (clicked on the lighting bolt menu button) and then double clicked on the click event.
This generates the event handler sub and add the handles clause for all the buttons selected. It tends to add the button event references in reverse order, but the order doesn't matter.
The name of the sub is created based on one of the selected controls and I usually rename the sub to be more general, i.e. the Sub was originally called Button4_Click, but I renamed it to Buttons_Click since it handles all the buttons in the Buttons array.
p.p.s. Also, in this example since the btn isn't further reference in the handler, we can just fetch the index from a temporary cast of the sender object.
If later you do need to access the button, you could always use Buttons(Index) to get to it.
Code:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
Dim index = CInt(DirectCast(sender, Button).Tag)
RadButtons(index).Checked = True
End Sub
Last edited by passel; Jan 2nd, 2021 at 04:15 PM.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jan 2nd, 2021, 06:07 PM
#69
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by passel
But, I've had the case where I had a VB6 application that I wanted to port to run in the VB.Net environment, but with minimal changes to the existing code, as the logic and methodology has already been worked out and in use for years, and I would like to port it in hours, not days or weeks.
Basically there are two things to do. Actually there are different ways to go about it, but this is what I did.
1. Set the .Tag property of the controls to the Index value you want them to be.
2. Create arrays of references to those controls in the order you have them indexed.
Code:
Public Class Form1
Private Buttons() As Button
Private RadButtons() As RadioButton
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Buttons = {Button1, Button2, Button3, Button4}
RadButtons = {RadioButton1, RadioButton2, RadioButton3, RadioButton4}
For i As Integer = 0 To UBound(Buttons)
Buttons(i).Tag = i
RadButtons(i).Tag = i
Next
End Sub
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
Dim btn = DirectCast(sender, Button)
Dim index = CInt(btn.Tag)
RadButtons(index).Checked = True
End Sub
End Class
p.s. To generate the Click event handler for the buttons, I selected all the buttons in the IDE, then in the Properties window, selected the Events list (clicked on the lighting bolt menu button) and then double clicked on the click event.
Thank you very much for this awesome explanation and code. I was able to understand it well, and expand upon it. I added 4 listboxes, stacked them up and added a number to each so I would know which was on top.
Code:
Public Class Form1
Private Buttons() As Button
Private RadButtons() As RadioButton
Private LBox() As ListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Buttons = {Button1, Button2, Button3, Button4}
RadButtons = {RadioButton1, RadioButton2, RadioButton3, RadioButton4}
LBox = {ListBox1, ListBox2, ListBox3, ListBox4}
For i As Integer = 0 To UBound(Buttons)
Buttons(i).Tag = i
RadButtons(i).Tag = i
LBox(i).Tag = i
Next
End Sub
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button4.Click, Button3.Click, Button2.Click, Button1.Click
Dim btn = DirectCast(sender, Button)
Dim index = CInt(btn.Tag)
RadButtons(index).Checked = True
End Sub
Private Sub RadioButtons_checkedchanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
Dim RB = DirectCast(sender, RadioButton)
Dim index = CInt(RB.Tag)
LBox(index).BringToFront()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Last edited by Shaggy Hiker; Jan 2nd, 2021 at 08:19 PM.
-
Jan 2nd, 2021, 08:04 PM
#70
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by passel
But, I've had the case where I had a VB6 application that I wanted to port to run in the VB.Net environment, but with minimal changes to the existing code, as the logic and methodology has already been worked out and in use for years, and I would like to port it in hours, not days or weeks.
Are you able to port over the UI, or do you have to rebuild it by hand?
-
Jan 2nd, 2021, 08:23 PM
#71
Re: Control Array?
I don't believe there is any way to port over the UI, though I could be wrong about that. Back in 2008 there was a converter included in VS. It was never very good, which is why it was dropped, but it would take a stab at converting things over. I don't remember it working all that well.
Forms in VB6 were a bit weird, behind the scenes. Forms in .NET are just code like any other code. If you'd like to see it, you can find it in the .designer.vb file associated with each form. It doesn't HAVE to be there, and prior to 2005 it was just a region in with the rest of your code. You could write everything in there by hand and it would work just as well, assuming you didn't screw it up. If you look at that file, you'll see there's a fair amount of code, so screwing it up wouldn't be all that difficult.
This difference between the two languages is pretty significant, which makes the conversion problematic.
By the way, code should be wrapped in [CODE][/CODE] tags, which you can do by pressing the # button and putting the code between the resulting tags, or add the code, highlight it, then press the # button. There is also the VB button, but I'm not sure how that's working, now.
My usual boring signature: Nothing
-
Jan 2nd, 2021, 08:41 PM
#72
Re: Control Array?
Originally Posted by vb6tovbnetguy
Are you able to port over the UI, or do you have to rebuild it by hand?
Being you have a lot of controls, then yes, I think you will need to rebuilt it by hand. In my case it wasn't a big chore because I didn't have a lot of controls in the VB6 version of the application. I only had a few buttons and a couple of control arrays, and the control arrays only had the initial base control. All the rest of the controls in the control array were created (i.e. Loaded) at startup and then laid out on the form by code.
The .Net code essentially did the same thing. I had the two initial controls that I wanted located where I wanted them placed on the form, and the startup code created the rest of the controls and laid them out on the form using the same layout logic that the VB6 code had used.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jan 2nd, 2021, 10:25 PM
#73
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by Shaggy Hiker
Datatables are particularly easy to store/recover from XML files. They have WriteXml and ReadXml methods, so storing and reading is as easy as providing a file name (and path), but you sure wouldn't want to be printing that.
What is an XML file?
-
Jan 2nd, 2021, 10:35 PM
#74
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 3rd, 2021, 09:17 AM
#75
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by .paul.
Once you create an "Object?" that holds several other bits of data of different types, can you save that to a text file like that, or do you have to break it up into it's constituent parts and then save those?
If you can save the object to disk, what would it look like?
EG Chicken + 5.98 + true
Thanks
-
Jan 3rd, 2021, 10:38 AM
#76
Re: Control Array?
There's no one answer to that. If you used a datatable, you'd end up with quite a bit of text to get to just the data you showed. First you'd have some tags for the datatable and schema, then you'd have a tag for the row, and within that a tag for each field. What this would mean that the for a single record with three fields holding "EG Chicken", "5.98", and "True", you'd probably end up with a file that would be many times that size. As you add records, the size increase would keep decreasing. XML is going to be roughly three times the size of the text you are saving, for the case you are showing, plus a fixed overhead. So, if you are saving about 20 Bytes per record, the XML file would be 60 x Number of records plus something like 200. The actual multiplier will depend on several factors.
This might sound like a lot, but if you think about it...it really isn't. You could store hundreds of thousands of records that way without it being an issue on a modern computer, so size doesn't matter. What MIGHT matter is that the resulting files would be easy to read/edit using Notepad, Wordpad, Excel, and so on. This could be good or it could be bad, depending on what was done and what was desired. What would not be the case is that you wouldn't read it the way you would read a list. The file would contain a whole bunch of tags <something>....</something>, so both reading and printing would be a bit ugly.
If you aren't going with the datatable, you might get a slightly smaller file. The overhead wouldn't be there, but some other overhead likely would be there. So, perhaps not the +200, but +100. Doing XML serialization on some object or collection of objects isn't quite as easy as serializing a datatable (which is just calling one method), but it isn't much harder. You also have the option to customize what gets saved when doing XML serialization, so you could leave one field out, if you wanted to. It's more work because you have more decisions about exactly what to store, but it's not a whole lot more work, and the file would end up being about the same size.
If you were to go that route, then JSON would also be an option. JSON objects stored to a text file would be smaller, and would also be readable/editable using something like Notepad or Wordpad, but they wouldn't read or print much more easily than XML. The only thing they really have going for them is that they are considerably smaller.
Finally, there would be binary serialization. That route creates files that you can't readily read, and can't edit at all. They would be considerably more compact, but you'd have to read them with the same code that wrote them. Binary serialized objects are simple, but the costs likely outweigh the benefits when it comes to storage. They include information about the assemblage that serialized them, and only that assemblage can deserialize them, which means that you end up with a file that only you can read....easily. It has all the nuisance of security without being totally secure.
My usual boring signature: Nothing
-
Jan 3rd, 2021, 11:12 AM
#77
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by Shaggy Hiker
Code:
Public Class YourClass
Public ItemName As String
Public Selected As Boolean
End Class
Then you could have a list of that class:
Code:
Public mySet As New List(of YourClass)
Code:
Public Class YourClass
Public ItemName As String
Public Selected As Boolean
Public Quantity As Integer
End Class
Code:
Public Class YourClass
Public Property ItemName As String
Public Property Selected As Boolean
Public Property Quantity As Integer
End Class
I don't care for datatables. I looked it up and created one and it seemed difficult. Now that you tell me how data would save from them I care for them less. I like to be able to jump into the saved file with notepad and easily modify it if nessessary.
I was thinking more of the above.
It was mentioned earlier so I thought people would know I meant this. It's too easy to forget people can't read my mind!!
Once you create an "Object?" that holds several other bits of data of different types, can you save that to a text file like that, or do you have to break it up into it's constituent parts and then save those?
If you can save the object to disk, what would it look like?
Chicken + 5.98 + true
Thanks
Last edited by vb6tovbnetguy; Jan 3rd, 2021 at 11:26 AM.
-
Jan 3rd, 2021, 01:04 PM
#78
Re: Control Array?
XML files are just text, so you can edit them with any text editor, such as notepad, but they are structured, hence the tags. This means that they aren't as easy to edit, for sure.
Once you get away from serialization (XML, binary, or JSON), then you have to do EVERYTHING yourself. There is no automatic way to write an object to a datafile in a fashion other than one of those, and each of those would be messy to some extent. If you are going to do it all yourself, then you could make it look however you wanted to. For example, you might have a method that takes one of those objects, takes each property, and puts it into a string such that it looked like:
"Chicken|5.98|True"
or anything else you wanted to see. You'd be doing all the work, you could make it do whatever you wanted. The point behind datatables is that you wouldn't have to do the work, but you'd also get the format that it writes (XML, most likely).
I'm not sure why you see datatables as difficult. It seems like the extent, for your problem, might look like this:
Code:
Dim dt As New Datatable
dt.Columns.Add("Category",GetType(String))
dt.Columns.Add("ItemName",GetType(String))
dt.Columns.Add("ItemPrice",GetType(Decimal))
dt.Columns.Add("Selected",GetType(Boolean))
then you'd add items to the table with:
Code:
Dim dr As DataRow = dt.NewRow
dt.Rows.Add(dr)
dr(0) = "Meat"
dr(1) = "Chicken"
dr(2) = 5.98D
dr(3) = True
The category field is just because you could then readily divide the datatable by category, if you wanted to:
Code:
dt.DefaultView.RowFilter("Category = 'Meat'")
I may have something a bit off in that, as I'm not sure about the single quotes in there, but it would be something like that. What that would do is to restrict the default DataView to include just those rows that have Meat as the category. You can create as many dataviews as you want, or change the filtering on the default.
Naturally, there are also at least two other ways to select subsets of rows from a datatable, both about as easy as the dataview approach.
My usual boring signature: Nothing
-
Jan 3rd, 2021, 04:48 PM
#79
Re: Control Array?
If I remember right you are using an Access database for storing your grocery items data. So I'm not sure the reasoning for also using a text, xml, json.....
Are you trying to eliminate the Access database? If not then just add a table to the database that holds the grocery list data.
-
Jan 4th, 2021, 09:39 AM
#80
Thread Starter
Lively Member
Re: Control Array?
Originally Posted by wes4dbt
If I remember right you are using an Access database for storing your grocery items data. So I'm not sure the reasoning for also using a text, xml, json.....
Are you trying to eliminate the Access database? If not then just add a table to the database that holds the grocery list data.
I'm not using a database. I don't know anything about those. I am using listboxes to hold my data. I have used arrays many times in the past so if I was going to change to something else to hold the data that wasn't currently in view, it would be an array. I don't know anything about datatables either, so don't want to use that.
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
|