By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,757 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,757 IT Pros & Developers. It's quick & easy.

Assigning a value to radio buttons

P: 22
Hi Guys,
Iam new to vb2005 and programming and I could really use some help. I have finished my book on vb2005 and i cant find what I am looking for,

Essentially I want to create a form with 3 columns, down each of these colums will be about 20 radio buttons. I need to assign a value to each radio button such as( -4 thur +4) at the bottom of each column I need it to add them up for a final score. Any advice would be great!

Thanks in advance
Apr 29 '07 #1
Share this Question
Share on Google+
41 Replies


Dököll
Expert 100+
P: 2,364
Hi Guys,
Iam new to vb2005 and programming and I could really use some help. I have finished my book on vb2005 and i cant find what I am looking for,

Essentially I want to create a form with 3 columns, down each of these colums will be about 20 radio buttons. I need to assign a value to each radio button such as( -4 thur +4) at the bottom of each column I need it to add them up for a final score. Any advice would be great!

Thanks in advance
Greetings and salutations, jpmcafee!

I am not familiar with VB Express just yet. I did pick up this link for you. See what it does:

http://books.google.com/books?id=24_...EDxrii4jetN_q8

Good luck with the project!

Dököll
Apr 29 '07 #2

P: 22
Bump,
Anyone other advice?
Apr 29 '07 #3

Expert 5K+
P: 8,434
What have you attempted so far? Which part of it is giving you trouble?

(Don't be too impatient, we're all in different timezones and only volunteering when we can spare the time.)
Apr 30 '07 #4

P: 22
To be honest the whole concept is driving me nuts. I can use vb2005 to draw the radios; I just need to be able to assign the value to it and add it up at the bottom.and I have no clue how to do this. Iam not a student just a dabler so to say. But what i am trying to accomplish is to have 4 verticle rows of radio buttons(approx 30 radios in each row) each will have its own question. I would like to be able to assign them a number value of -4 through +4 depending on there importance and then have it add up each row at the bottom.

Any help would be great.
May 1 '07 #5

Dököll
Expert 100+
P: 2,364
To be honest the whole concept is driving me nuts. I can use vb2005 to draw the radios; I just need to be able to assign the value to it and add it up at the bottom.and I have no clue how to do this. Iam not a student just a dabler so to say. But what i am trying to accomplish is to have 4 verticle rows of radio buttons(approx 30 radios in each row) each will have its own question. I would like to be able to assign them a number value of -4 through +4 depending on there importance and then have it add up each row at the bottom.

Any help would be great.
It looks like it should work, have you read the information included?

If you remove references to checkboxes in the code (there's actually a bit of code included), you should be golden.

If all else fails, build a pseudocode to have an idea what you need to achieve...I'll just bet a light should come on eventually. You'll get it, no worries:-)

In a bit!
May 2 '07 #6

Expert 5K+
P: 8,434
To be honest the whole concept is driving me nuts. I can use vb2005 to draw the radios; I just need to be able to assign the value to it and add it up at the bottom.and I have no clue how to do this. Iam not a student just a dabler so to say. But what i am trying to accomplish is to have 4 verticle rows of radio buttons(approx 30 radios in each row) each will have its own question. I would like to be able to assign them a number value of -4 through +4 depending on there importance and then have it add up each row at the bottom.
This sounds like a job for checkboxes rather than radio buttons. The idea of radio buttons is to allow you to choose one out of a set of options. If you want each item to be selected or de-selected individually, you're talking about checkboxes.

If you want each column to allow one item to be selected (which would be a job for radio buttons) then you should be able to achieve it by placing each column of radio buttons into a separate container, such as a frame.
May 2 '07 #7

Dököll
Expert 100+
P: 2,364
This sounds like a job for checkboxes rather than radio buttons. The idea of radio buttons is to allow you to choose one out of a set of options. If you want each item to be selected or de-selected individually, you're talking about checkboxes.

If you want each column to allow one item to be selected (which would be a job for radio buttons) then you should be able to achieve it by placing each column of radio buttons into a separate container, such as a frame.
That's not a bad idea at all. I'll start with radio button's first. Here's a quick step, pretty basic:

Form

Expand|Select|Wrap|Line Numbers
  1.  
  2. If Option1 = True Then 'first button on frame on its own
  3.     MinusOne = 1 'you are adding value to this button
  4. ElseIf Option2 = True Then 'second button on frame on its own
  5.     PlusOne = 2 'you are adding value to this button
  6. End If
  7.  
  8.  
  9. If MinusOne = 1 Then
  10.     you can make somehting happen here for - value...
  11. ElseIf PlusOne = 2
  12.     do somehting else here specific to + value...
  13. End If
  14.  
  15.  
  16.  
  17. End Sub
  18.  
  19.  
  20.  
I do not see why VB 2005 would be too far from this. Again, checkboxes should be the choice here if you have a number instances. Above code can be modified to use checkboxes instead while ignoring the frames.
May 2 '07 #8

SammyB
Expert 100+
P: 807
Hi Guys,
Iam new to vb2005 and programming and I could really use some help. I have finished my book on vb2005 and i cant find what I am looking for,

Essentially I want to create a form with 3 columns, down each of these colums will be about 20 radio buttons. I need to assign a value to each radio button such as( -4 thur +4) at the bottom of each column I need it to add them up for a final score. Any advice would be great!

Thanks in advance
This is easy and quick to do. Since you are a novice, just follow these instructions exactly and after it works, do it again like you want it. Since this is just a demo, we are going to keep it simple: two columns of four checkboxes (as Killer said, use checkboxes) with two textboxes at the bottom for the sum:
  1. Create a new VB windows app
  2. Add eight CheckBox controls to the form and arrange them into two columns.
  3. Add a textbox at the bottom of each column.
  4. Change the names of the controls in the first column to A1, A2, A3, A4. The numbers are not important, the code will only look at the first letter.
  5. Change the names of the controls in the first column to B1, B2, B3, B4
  6. We will keep the "value" of each textbox in the Tap property and to make sure that it is working, we will use the same number in the Text property. We will not use the Text property in the code, so in the actual app, you can make the text whatever you want.
  7. So, change the Tag and Text property of each control. You can keep it simple, 1 , 2, 3, 4, or use any numbers you want. Just make sure that for a single control, the Text & Tag property are the same.
  8. Using the mouse, lasso all of the checkboxes so that they are all selected. Do not select the Textboxes. If lassoing is difficult for you, just click on the first and Ctrl-click on the rest.
  9. After all of the Checkboxes are selected, in the Properties window, click on the lightning bolt, so that events are shown.
  10. Double-click on the CheckedChanged text. That will setup a single event for all of the checkboxes.
  11. Add code:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub B4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B4.CheckedChanged, B3.CheckedChanged, B2.CheckedChanged, B1.CheckedChanged, A4.CheckedChanged, A3.CheckedChanged, A2.CheckedChanged, A1.CheckedChanged
  2.         Dim s1 As Integer = 0, s2 As Integer = 0
  3.         For Each c As Control In Me.Controls
  4.             If TypeOf c Is CheckBox Then
  5.                 Dim chk As CheckBox = c
  6.                 If chk.Checked Then
  7.                     If chk.Name.StartsWith("A") Then
  8.                         s1 += chk.Tag
  9.                     ElseIf chk.Name.StartsWith("B") Then
  10.                         s2 += chk.Tag
  11.                     End If
  12.                 End If
  13.             End If
  14.         Next c
  15.         TextBox1.Text = s1.ToString()
  16.         TextBox2.Text = s2.ToString()
  17.     End Sub
The pseudocode would be:
Expand|Select|Wrap|Line Numbers
  1. Whenever a checkbox is checked or unchecked:
  2. init the sums to zero
  3. For every control on the form:
  4. If the control is a checkbox and the checkbox is checked then
  5.      If the checkbox's name starts with A, then add its Tab to the first sum
  6.      If the checkbox's name starts with B, then add its Tab to the 2nd sum
  7. After looping thru all of the controls, put the sums in the TextBoxes
  8.  
May 2 '07 #9

P: 22
What is the pseudocode? and where would it go if anywhere... Again thank you all for your help.
May 2 '07 #10

SammyB
Expert 100+
P: 807
What is the pseudocode? and where would it go if anywhere... Again thank you all for your help.
:D the pseudocode is just to help you understand the actual code so that you can change it to do what you want.
May 2 '07 #11

P: 22
Can I say idot Gosh...... Thank you I guess I sound Stupid...
May 2 '07 #12

Expert 5K+
P: 8,434
Can I say idot Gosh...... Thank you I guess I sound Stupid...
I'd say you sound like someone who hasn't done this before. :)

Experience is what makes the difference. In a couple of years you'll probably laugh at anyone who suggests you ever had trouble with this.
May 2 '07 #13

Expert 5K+
P: 8,434
...follow these instructions exactly and after it works ...
Isn't it great to see such confidence? :D
May 2 '07 #14

P: 22
Thats Awesome..... Tell Me somthing it is possible to have a combo box or sometype of drop down boxes with choices that have values assigned (along the same lines as this?. I was thinking it would save space on the form. So another words in a label I could ask a question. In the drop down they could select the right answer and then have it add up at the end.

Instead of having a check box for each answer? Thanks Again
May 2 '07 #15

SammyB
Expert 100+
P: 807
Thats Awesome..... Tell Me somthing it is possible to have a combo box or sometype of drop down boxes with choices that have values assigned (along the same lines as this?. I was thinking it would save space on the form. So another words in a label I could ask a question. In the drop down they could select the right answer and then have it add up at the end.

Instead of having a check box for each answer? Thanks Again
You could do that, but not with Tag. Can you give us a better idea of what you are trying to do so we can make better suggestions for the user interface?
May 2 '07 #16

P: 22
Sure,
What I have is an employment information sheet. This is used for fraud cases. Essentially when you have several suspects that you are unaware of who actually comitted a crime, this is a tool to assist you in identifying who might most likley be the suspect. Currently we have this on paper form. I would like to create and application to do the same.

So what the paper looks like is:
This sheet has approx 8 verticle rows, at the top is a place to put the suspects name, this is to identify the subjects of the crime. Now down this row is a series of questions that are posed, these are realted to the individual. Such as position, Fulltime/parttime, tenure, age, etc. Now each one of the possible answers are waited based on the relivenace such as for Full Time/ Partime: if they are partime they would get a check mark in that column because that would indicate a need for money, more so than a fulltime employee.

Each one of these questions are along the same line. I have 24 questions in total. However on paper it is easy to check each of these boxes. In an application, I will have to provide these answers and weight them with a value I would think. At the bottom we simply count the check marks and the individual with the most check marks is who we would concentrate on first.

I hope this helps. Let me know your thoughts.

Josh
May 2 '07 #17

Dököll
Expert 100+
P: 2,364
Can I say idot Gosh...... Thank you I guess I sound Stupid...
You're doing fine, good luck with project...
May 3 '07 #18

P: 22
So Iam following up. I think I got away for this to work. I created a tabbed menu to start. The theory behind this is each tab will represent one suspect. As I need to add more suspects, I have created a button to add a new tab. So it will create a new tab each time clicked. Each of these tabs will have the same questions within the tab index. Now comes the tricky part. As before we used checkboxes and made a tage reference of A1-A5 and B1-B5 for each checkbox this is how we were able to sum them up. What i need to be able to accomplish now is a couple of things;

1. to be able to dynamically create a new tab with the same questions on each tabbed page ( I can create a new tab now I just need to figure out how to have the same questions on each page).
2. Have the application recognize it is a new tabbed page and apply a new tag value such as C1, D1, E1 etc.
3. Create a summary page that will pull in all of these results and display them in a text box.

If anyone can steer me in the right direction I will love you forever... :) Thank you all for your support.

J.
May 3 '07 #19

SammyB
Expert 100+
P: 807
1. to be able to dynamically create a new tab with the same questions on each tabbed page ( I can create a new tab now I just need to figure out how to have the same questions on each page).
A tabpage is a container, just like a form, so to addup a page you just do foreach control in pgA1.Controls

You can create the controls on a page yourself: just look how VB does it. You can see this code by clicking the button on top of the Solution Explorer that says "Show All Files" (2nd button), and then click on the + in from of your form. Right-click on the Form.Designer.vb and view-code.

Here's a simple example, http://msdn2.microsoft.com/en-us/lib...55(VS.71).aspx. But, you need to add bunches, so you will need to have an array for each of your controls. Here's Microsoft's NOT simple solution, http://msdn2.microsoft.com/en-us/lib...00(VS.71).aspx. I would work thru both of these samples and understand them before continuing.

But, not following my own advice, I would (1)make a tabpage as a template - set to invisible, (2)have a generic list for each type of control, and (3)for a new tab page just iterate thru the template, adding a copy of each control to the appropriate list. Well, that's overwhelming: enough for today. If I get time, I'll try to make a demo.

==============================================

Totally changing the design: You could make all of this into a TreeView. Each subject would be a root node, the questions would be the second level, and the responses the third level. It would be infinitely easier to code.
Attached Images
File Type: jpg Tree.JPG (9.0 KB, 373 views)
May 3 '07 #20

P: 22
Man you are great,

I did post a new topic afterwards with your second idea and I think I will try that.. somehow. So up to this point, I have created a tabbed form with two buttons. I will place about 10 tabs accross the top. I guess my next step would be to :

1. create the checkboxe form on each tab like before
2. find a away to hide the tabs (2-10) unless a button is pushed which should make each tab appear as the button is pushed.
3. create results page at the end as well that will be invisible and only shown when the results button is pushed.

So what I could use if anyone knows is a work around to make tabs appear and dissapear as a button is pushed.

I guess this is where I will start so any help would be great.. .Thanks again sammy.



A tabpage is a container, just like a form, so to addup a page you just do foreach control in pgA1.Controls

You can create the controls on a page yourself: just look how VB does it. You can see this code by clicking the button on top of the Solution Explorer that says "Show All Files" (2nd button), and then click on the + in from of your form. Right-click on the Form.Designer.vb and view-code.

Here's a simple example, http://msdn2.microsoft.com/en-us/lib...55(VS.71).aspx. But, you need to add bunches, so you will need to have an array for each of your controls. Here's Microsoft's NOT simple solution, http://msdn2.microsoft.com/en-us/lib...00(VS.71).aspx. I would work thru both of these samples and understand them before continuing.

But, not following my own advice, I would (1)make a tabpage as a template - set to invisible, (2)have a generic list for each type of control, and (3)for a new tab page just iterate thru the template, adding a copy of each control to the appropriate list. Well, that's overwhelming: enough for today. If I get time, I'll try to make a demo.

==============================================

Totally changing the design: You could make all of this into a TreeView. Each subject would be a root node, the questions would be the second level, and the responses the third level. It would be infinitely easier to code.
May 3 '07 #21

SammyB
Expert 100+
P: 807
So what I could use if anyone knows is a work around to make tabs appear and dissapear as a button is pushed.
sammy.
Oh bother, I forgot that Bill made it next to impossible -- you have to remove/add it to the TabPage's Controls collection. Here's the code for a form with a single TabPage, TabPage1 (mine had 3 tabs) and a button BtnAdd:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private cPages As New List(Of TabPage)()
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         ' Save each tab in cPages
  5.         For i As Integer = 0 To TabControl1.TabCount - 1
  6.             cPages.Add(TabControl1.TabPages(i)) 'Save the tab
  7.         Next i
  8.         ' Remove all but First to make them invisible
  9.         For i As Integer = TabControl1.TabCount - 1 To 1 Step -1
  10.             TabControl1.Controls.RemoveAt(i)
  11.         Next i
  12.     End Sub
  13.     Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
  14.         TabControl1.Controls.Add(cPages(TabControl1.TabCount))
  15.         If TabControl1.TabCount = cPages.Count Then BtnAdd.Enabled = False
  16.     End Sub
  17. End Class
May 4 '07 #22

P: 22
Thanks you so where and how do I place this code? Thanks

Oh bother, I forgot that Bill made it next to impossible -- you have to remove/add it to the TabPage's Controls collection. Here's the code for a form with a single TabPage, TabPage1 (mine had 3 tabs) and a button BtnAdd:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private cPages As New List(Of TabPage)()
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         ' Save each tab in cPages
  5.         For i As Integer = 0 To TabControl1.TabCount - 1
  6.             cPages.Add(TabControl1.TabPages(i)) 'Save the tab
  7.         Next i
  8.         ' Remove all but First to make them invisible
  9.         For i As Integer = TabControl1.TabCount - 1 To 1 Step -1
  10.             TabControl1.Controls.RemoveAt(i)
  11.         Next i
  12.     End Sub
  13.     Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
  14.         TabControl1.Controls.Add(cPages(TabControl1.TabCount))
  15.         If TabControl1.TabCount = cPages.Count Then BtnAdd.Enabled = False
  16.     End Sub
  17. End Class
May 5 '07 #23

P: 22
I got it This is Awesome Thanks! Iam trying right now to add 8 pages to it if your around do you have any advice. Thanks


Thanks you so where and how do I place this code? Thanks
May 5 '07 #24

P: 22
HI,
Sammy,
I got most of everything figured out. I used the following code to add up the checkboxes but it is not adding it up for some reason. I tested it with 4 checkboxes and its fine. Any advice?

Expand|Select|Wrap|Line Numbers
  1.  Private Sub A1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A9.CheckedChanged, A8.CheckedChanged, A7.CheckedChanged, A6.CheckedChanged, A5.CheckedChanged, A48.CheckedChanged, A47.CheckedChanged, A46.CheckedChanged, A45.CheckedChanged, A44.CheckedChanged, A43.CheckedChanged, A42.CheckedChanged, A41.CheckedChanged, A40.CheckedChanged, A4.CheckedChanged, A39.CheckedChanged, A38.CheckedChanged, A37.CheckedChanged, A36.CheckedChanged, A35.CheckedChanged, A34.CheckedChanged, A33.CheckedChanged, A32.CheckedChanged, A31.CheckedChanged, A30.CheckedChanged, A3.CheckedChanged, A29.CheckedChanged, A28.CheckedChanged, A27.CheckedChanged, A26.CheckedChanged, A25.CheckedChanged, A24.CheckedChanged, A23.CheckedChanged, A22.CheckedChanged, A21.CheckedChanged, A20.CheckedChanged, A2.CheckedChanged, A19.CheckedChanged, A18.CheckedChanged, A17.CheckedChanged, A16.CheckedChanged, A15.CheckedChanged, A14.CheckedChanged, A13.CheckedChanged, A12.CheckedChanged, A11.CheckedChanged, A10.CheckedChanged, A1.CheckedChanged
  2.         Dim s1 As Integer = 0
  3.         For Each c As Control In Me.Controls
  4.             If TypeOf c Is CheckBox Then
  5.                 Dim chk As CheckBox = c
  6.                 If chk.Checked Then
  7.                     If chk.Name.StartsWith("A") Then
  8.                         s1 += chk.Tag
  9.                     End If
  10.                 End If
  11.             End If
  12.         Next c
  13.         TextBox1.Text = s1.ToString()
  14.     End Sub
May 6 '07 #25

SammyB
Expert 100+
P: 807
HI,
Sammy,
I got most of everything figured out. I used the following code to add up the checkboxes but it is not adding it up for some reason. I tested it with 4 checkboxes and its fine. Any advice?
That's because you do not have any checkboxes on the form! (They're in TabPage1, etc.) I guess it was a very good thing to put them into cPages in the FormLoad, because now you can use them with an index. In other words, for summing TabPage1, you need to make the loop:
For Each c As Control in TabPage1.Controls
but since these pages are in cPages, you just need to do:
Expand|Select|Wrap|Line Numbers
  1. For Each c As Control In cPages(0).Controls
for the first tab.
May 6 '07 #26

P: 22
That worked Perfectly.... Thank you I owe you big time. Iam starting to get the hang of this...
May 6 '07 #27

P: 22
Ok,
Now I am at a point to make a results page. What I have is 11 tabbed pages on these tabbed pages I have 3 text boxes with a series of check boxes.
Now the sum of these check boxes is put into one text box. The other two textboxes provides for a name and position. What I need on this results page is to diplay in order by the highest sum the name, position and score they recieved. Now the trick is there are 11 tabbed pages sometimes all of them will be used and sometime not.

What I did so far is to create 11 text boxes on a new tabbed page (labled results) this wil provide room for the Name; then i made another colume of 11 for the position and another 11 for the score.

Does this seem like I am on the right trail?

Now where I could use help is to recognize which tab pages have results to pull in to the results page, and have it display the corresponding name and position to that score in a numerical order by highest to lowest score.

Thanks Guys,
May 6 '07 #28

SammyB
Expert 100+
P: 807
Ok,
Now I am at a point to make a results page. What I have is 11 tabbed pages on these tabbed pages I have 3 text boxes with a series of check boxes.
Now the sum of these check boxes is put into one text box. The other two textboxes provides for a name and position. What I need on this results page is to diplay in order by the highest sum the name, position and score they recieved. Now the trick is there are 11 tabbed pages sometimes all of them will be used and sometime not.

What I did so far is to create 11 text boxes on a new tabbed page (labled results) this wil provide room for the Name; then i made another colume of 11 for the position and another 11 for the score.

Does this seem like I am on the right trail?

Now where I could use help is to recognize which tab pages have results to pull in to the results page, and have it display the corresponding name and position to that score in a numerical order by highest to lowest score.

Thanks Guys,
You doing great! For now, I would just keep it simple and whatever you change in the CheckedChange event also change on the summary page. Something like:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub A1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A9.CheckedChanged, and all the rest :D
  2.         Dim s1 As Integer = 0
  3.         For Each c As Control In cPages(0).Controls
  4.             If TypeOf c Is CheckBox Then
  5.                 Dim chk As CheckBox = c
  6.                 If chk.Checked Then
  7.                     If chk.Name.StartsWith("A") Then
  8.                         s1 += chk.Tag
  9.                     End If
  10.                 End If
  11.             End If
  12.         Next c
  13.         TxtTotal1.Text = s1.ToString()
  14.         TxtSummaryTotal1.Text = s1.ToString()
  15.         TxtSummaryName1.Text = TxtName1.Text
  16.     End Sub
May 6 '07 #29

P: 22
You doing great! For now, I would just keep it simple and whatever you change in the CheckedChange event also change on the summary page. Something like:
Expand|Select|Wrap|Line Numbers
  1.     Private Sub A1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A9.CheckedChanged, and all the rest :D
  2.         Dim s1 As Integer = 0
  3.         For Each c As Control In cPages(0).Controls
  4.             If TypeOf c Is CheckBox Then
  5.                 Dim chk As CheckBox = c
  6.                 If chk.Checked Then
  7.                     If chk.Name.StartsWith("A") Then
  8.                         s1 += chk.Tag
  9.                     End If
  10.                 End If
  11.             End If
  12.         Next c
  13.         TxtTotal1.Text = s1.ToString()
  14.         TxtSummaryTotal1.Text = s1.ToString()
  15.         TxtSummaryName1.Text = TxtName1.Text
  16.     End Sub

Sammy,
That looks good, however it looks like its just adding up all the checkboxes and displaying it on one page. I was actually thinking of like a multi dimensional array or somthing. Becuase I need it to

Each tabbed page diplays a Name.Box, PositionBox, and a Result Box.
the result box is the sum of checkboxes for that tabbed page remember.

What I need is for the summary page to recognize and sort by the highest sum.
Display those results with the following information from each tabbed page(again in order by the sum, Highest to lowest).
Name - Postion- Score(result)

Just to show you what I did ( I created a Results tab in a tool strip to show the results page)

The I did this

Expand|Select|Wrap|Line Numbers
  1.     Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
  2.         Dim addresultstab As Boolean
  3.  
  4.         For i As Integer = 0 To TabControl1.TabCount - 1
  5.             If TabControl1.TabPages(i).Text = "Results" Then
  6.                 addresultstab = False
  7.                 Exit For
  8.             Else
  9.                 addresultstab = True
  10.             End If
  11.         Next i
  12.         If addresultstab = True Then
  13.             TabControl1.Controls.Add(cPages(11))
  14.         End If
  15.         'add results to results tab
  16.  
  17.  
  18.         rtbResults.Text = "Employee Information Results" & vbCrLf
  19.         rtbResults.Text = rtbResults.Text & "--------------------------------" & vbCrLf
  20.         rtbResults.Text = rtbResults.Text & vbCrLf
  21.         If NameBox1.Text <> "" Then
  22.             rtbResults.Text = rtbResults.Text & NameBox1.Text & " (" & PoBox1.Text & "): " & ResultsBox1.Text & vbCrLf
  23.         End If
  24.  
  25.         If NameBox2.Text <> "" Then
  26.             rtbResults.Text = rtbResults.Text & NameBox2.Text & " (" & PoBox2.Text & "): " & ResultsBox2.Text & vbCrLf
  27.         End If
  28.  
  29.         If NameBox3.Text <> "" Then
  30.             rtbResults.Text = rtbResults.Text & NameBox3.Text & " (" & PoBox3.Text & "): " & ResultsBox3.Text & vbCrLf
  31.         End If
  32.     End Sub
However now I cant get it to sort it by the result(sum) value...... I need help!!
May 6 '07 #30

SammyB
Expert 100+
P: 807
Too busy now, but check out the ListView control, Details view, I think that you can sort by any column.
May 6 '07 #31

P: 22
That worked out good, It was a little difficult finding where to place the code so I hope I got it. The only thing I question is that this shows two columns I wonder if I have to do anything to accomodate a third. Alot of the code shows

Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections
  2. Imports System.Windows.Forms
  3.  
  4. Public Class ListViewColumnSorter
  5.     Implements System.Collections.IComparer
  6.  
  7.     Private ColumnToSort As Integer
  8.     Private OrderOfSort As SortOrder
  9.     Private ObjectCompare As CaseInsensitiveComparer
  10.  
  11.     Public Sub New()
  12.         ' Initialize the column to '0'.
  13.         ColumnToSort = 0
  14.  
  15.         ' Initialize the sort order to 'none'.
  16.         OrderOfSort = SortOrder.None
  17.  
  18.         ' Initialize the CaseInsensitiveComparer object.
  19.         ObjectCompare = New CaseInsensitiveComparer()
  20.     End Sub
  21.  
  22.     Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
  23.         Dim compareResult As Integer
  24.         Dim listviewX As ListViewItem
  25.         Dim listviewY As ListViewItem
  26.  
  27.         ' Cast the objects to be compared to ListViewItem objects.
  28.         listviewX = CType(x, ListViewItem)
  29.         listviewY = CType(y, ListViewItem)
  30.  
  31.         ' Compare the two items.
  32.         compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text)

Do I have to add anything for a third column to be figured in
like

listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)
listviewZ = CType(z, ListViewItem)

? or should I be ok......
May 6 '07 #32

SammyB
Expert 100+
P: 807
That worked out good, It was a little difficult finding where to place the code so I hope I got it. The only thing I question is that this shows two columns I wonder if I have to do anything to accomodate a third. Alot of the code shows

Expand|Select|Wrap|Line Numbers
  1. Imports System.Collections
  2. Imports System.Windows.Forms
  3.  
  4. Public Class ListViewColumnSorter
  5. Implements System.Collections.IComparer
  6.  
  7. Private ColumnToSort As Integer
  8. Private OrderOfSort As SortOrder
  9. Private ObjectCompare As CaseInsensitiveComparer
  10.  
  11. Public Sub New()
  12. ' Initialize the column to '0'.
  13. ColumnToSort = 0
  14.  
  15. ' Initialize the sort order to 'none'.
  16. OrderOfSort = SortOrder.None
  17.  
  18. ' Initialize the CaseInsensitiveComparer object.
  19. ObjectCompare = New CaseInsensitiveComparer()
  20. End Sub
  21.  
  22. Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
  23. Dim compareResult As Integer
  24. Dim listviewX As ListViewItem
  25. Dim listviewY As ListViewItem
  26.  
  27. ' Cast the objects to be compared to ListViewItem objects.
  28. listviewX = CType(x, ListViewItem)
  29. listviewY = CType(y, ListViewItem)
  30.  
  31. ' Compare the two items.
  32. compareResult = ObjectCompare.Compare(listviewX.SubItems(ColumnToSort).Text, listviewY.SubItems(ColumnToSort).Text)

Do I have to add anything for a third column to be figured in
like

listviewX = CType(x, ListViewItem)
listviewY = CType(y, ListViewItem)
listviewZ = CType(z, ListViewItem)

? or should I be ok......
I usually do the Column stuff with the form designer's Column collection, but here is a sample of adding them in code plus (more importantly) sample code to sort by column when you click on the header. It's a lot more messy than I thought: I was hoping for a SortColumn property, but this looks like an excellent example. You'll be able to apply it to your situation. http://support.microsoft.com/kb/319399
May 7 '07 #33

P: 22
I usually do the Column stuff with the form designer's Column collection, but here is a sample of adding them in code plus (more importantly) sample code to sort by column when you click on the header. It's a lot more messy than I thought: I was hoping for a SortColumn property, but this looks like an excellent example. You'll be able to apply it to your situation. http://support.microsoft.com/kb/319399
Sammy,
Thank you that is the code I used and worked. However its giving me grief with 3 columns.... Not quite sure what to do.
May 7 '07 #34

SammyB
Expert 100+
P: 807
Sammy,
Thank you that is the code I used and worked. However its giving me grief with 3 columns.... Not quite sure what to do.
This is what I did:
  1. Add a ListView. In it's properties change the Name to LvResults and the View to Details.
  2. Now add the columns. Again in the ListView properties, click on the ... button for the Columns properties. For each column, press Add and enter the header text. I added three: Name, Score, Tab
  3. On my Tab Pages I made the name TextBoxes TxtName1, TxtName2, etc. and the score TextBoxes TxtScore1, TxtScore2, etc.
  4. I stole the ListViewColumnSorter class without changes from http://support.microsoft.com/kb/319399
  5. I just put the Results tab on TabControl1 so there's an extra if in my outer loop:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private cPages As New List(Of TabPage)()
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         ' Save each tab in cPages
  5.         For i As Integer = 0 To TabControl1.TabCount - 1
  6.             cPages.Add(TabControl1.TabPages(i)) 'Save the tab
  7.         Next i
  8.         ' Remove all but First & Last to make them invisible
  9.         For i As Integer = TabControl1.TabCount - 2 To 1 Step -1
  10.             TabControl1.Controls.RemoveAt(i)
  11.         Next i
  12.     End Sub
  13.     Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
  14.         TabControl1.Controls.Add(cPages(TabControl1.TabCount - 1))
  15.         If TabControl1.TabCount = cPages.Count Then BtnAdd.Enabled = False
  16.     End Sub
  17.     Private Sub CB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.CheckedChanged, A1Page2.CheckedChanged
  18.         Dim i As Integer = 1
  19.         LvResults.Items.Clear()
  20.         For Each pg As TabPage In TabControl1.TabPages
  21.             If pg.Text <> "Results" Then
  22.                 Dim sum As Integer = 0
  23.                 Dim TbName As TextBox = Nothing, TbTotal As TextBox = Nothing
  24.                 For Each c As Control In pg.Controls
  25.                     If TypeOf c Is CheckBox Then
  26.                         Dim chk As CheckBox = c
  27.                         If chk.Checked Then
  28.                             If chk.Name.StartsWith("A") Then
  29.                                 sum += chk.Tag
  30.                             End If
  31.                         End If
  32.                     ElseIf TypeOf c Is TextBox Then
  33.                         If c.Name.StartsWith("TxtName") Then
  34.                             TbName = c
  35.                         ElseIf c.Name.StartsWith("TxtTotal") Then
  36.                             TbTotal = c
  37.                         End If
  38.                     End If
  39.                 Next c
  40.                 TbTotal.Text = sum.ToString()
  41.                 Dim row As New ListViewItem(New String() {TbName.Text, TbTotal.Text, i.ToString()})
  42.                 LvResults.Items.Add(row)
  43.                 i = i + 1
  44.             End If
  45.         Next pg
  46.         Dim LvSorter As New ListViewColumnSorter()
  47.         LvSorter.Order = SortOrder.Ascending
  48.         LvSorter.SortColumn = 1
  49.         LvResults.ListViewItemSorter = LvSorter
  50.         LvResults.Sort()
  51.     End Sub
  52. End Class
Now, we need to start over in an object-oriented fashon and make a good, maintaintable, two-tier solution. Are you up to it?
May 7 '07 #35

P: 22
OK,
So how should we do this. Iam up for anything, should I add this code or wait? not sure what you mean by an object orientated enviroment

This is what I did:
  1. Add a ListView. In it's properties change the Name to LvResults and the View to Details.
  2. Now add the columns. Again in the ListView properties, click on the ... button for the Columns properties. For each column, press Add and enter the header text. I added three: Name, Score, Tab
  3. On my Tab Pages I made the name TextBoxes TxtName1, TxtName2, etc. and the score TextBoxes TxtScore1, TxtScore2, etc.
  4. I stole the ListViewColumnSorter class without changes from http://support.microsoft.com/kb/319399
  5. I just put the Results tab on TabControl1 so there's an extra if in my outer loop:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private cPages As New List(Of TabPage)()
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         ' Save each tab in cPages
  5.         For i As Integer = 0 To TabControl1.TabCount - 1
  6.             cPages.Add(TabControl1.TabPages(i)) 'Save the tab
  7.         Next i
  8.         ' Remove all but First & Last to make them invisible
  9.         For i As Integer = TabControl1.TabCount - 2 To 1 Step -1
  10.             TabControl1.Controls.RemoveAt(i)
  11.         Next i
  12.     End Sub
  13.     Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
  14.         TabControl1.Controls.Add(cPages(TabControl1.TabCount - 1))
  15.         If TabControl1.TabCount = cPages.Count Then BtnAdd.Enabled = False
  16.     End Sub
  17.     Private Sub CB_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.CheckedChanged, A1Page2.CheckedChanged
  18.         Dim i As Integer = 1
  19.         LvResults.Items.Clear()
  20.         For Each pg As TabPage In TabControl1.TabPages
  21.             If pg.Text <> "Results" Then
  22.                 Dim sum As Integer = 0
  23.                 Dim TbName As TextBox = Nothing, TbTotal As TextBox = Nothing
  24.                 For Each c As Control In pg.Controls
  25.                     If TypeOf c Is CheckBox Then
  26.                         Dim chk As CheckBox = c
  27.                         If chk.Checked Then
  28.                             If chk.Name.StartsWith("A") Then
  29.                                 sum += chk.Tag
  30.                             End If
  31.                         End If
  32.                     ElseIf TypeOf c Is TextBox Then
  33.                         If c.Name.StartsWith("TxtName") Then
  34.                             TbName = c
  35.                         ElseIf c.Name.StartsWith("TxtTotal") Then
  36.                             TbTotal = c
  37.                         End If
  38.                     End If
  39.                 Next c
  40.                 TbTotal.Text = sum.ToString()
  41.                 Dim row As New ListViewItem(New String() {TbName.Text, TbTotal.Text, i.ToString()})
  42.                 LvResults.Items.Add(row)
  43.                 i = i + 1
  44.             End If
  45.         Next pg
  46.         Dim LvSorter As New ListViewColumnSorter()
  47.         LvSorter.Order = SortOrder.Ascending
  48.         LvSorter.SortColumn = 1
  49.         LvResults.ListViewItemSorter = LvSorter
  50.         LvResults.Sort()
  51.     End Sub
  52. End Class
Now, we need to start over in an object-oriented fashon and make a good, maintaintable, two-tier solution. Are you up to it?
May 7 '07 #36

Expert 5K+
P: 8,434
So how should we do this. Iam up for anything, should I add this code or wait? not sure what you mean by an object orientated enviroment
Just a note - the expression is "object-oriented", not "orientated".
May 7 '07 #37

SammyB
Expert 100+
P: 807
Just a note - the expression is "object-oriented", not "orientated".
No excuse...except I was already an hour late for work and TheScripts is scriptless for a spell check button, so I got a little dis-orientatated!
May 8 '07 #38

Expert 5K+
P: 8,434
No excuse...except I was already an hour late for work and TheScripts is scriptless for a spell check button, so I got a little dis-orientatated!
It wasn't you who got it wrong, silly.
May 8 '07 #39

SammyB
Expert 100+
P: 807
It wasn't you who got it wrong, silly.
Gee, should I have read the thread? Guess I need reorientation.
May 8 '07 #40

SammyB
Expert 100+
P: 807
OK,
So how should we do this. Iam up for anything, should I add this code or wait? not sure what you mean by an object orientated enviroment
Oh, definitely make the few changes that you need to your code: it's always wonderful to have something that works! Plus, you'll learn about the ListView: it's my favorite control.

Now, object oriented: look at wikipedia, http://en.wikipedia.org/wiki/Object-...ed_programming. In our case, what objects do we have? We have a suspect! What properties does this suspect have? A name, a score, a rank, and a list of answers to questions. So, we should start with this object. Let's just start over with a new Windows Application. Just put the add button on the new form and we will use that to create our suspect objects.

To create an object, we need to create a class. The class is like a cookie-cutter and the object is like a cookie. So, after adding the button, use the Project, Add class menu to create a class called Suspect.vb.

Now, read the help tutorial about creating a class (Help, Index, classes [Visual Basic], about classes) (or on-line at http://msdn2.microsoft.com/en-us/lib...01(VS.80).aspx) and then see if you can create some code for the Suspect class. Do not worry about the user interface at this point, we'll do that later and auto-magically attach it to the class.

But, I do have questions: (1) what is the rank; and (2) for a particular question is there one one answer?
May 8 '07 #41

P: 22
OK Rank I think is what you are refering to is in order from highest to lowest based on the numerical score.
The questions have 2 answers minimum some have 3 or so.
May 9 '07 #42

Post your reply

Sign in to post your reply or Sign up for a free account.