Graphical representation of data - vb.net

I am totally new to the graphical representation of the data. I want to make a progress report of students, based on marks achieved each year.
For example, in year 2005 marks were 750. in 2006 780 in 2007 800
I want to show it graphically. Could any body give me code example?
Thanks a lot.

Use a label and set the Label.Width = intValue where intValue is the value you want to show (you can divide it by a factor to make sure it stays within a certain bound). They you an play around with lable colors or backgrounds for different value ranges. E.g.
assume valuelist is the list of values
Dim graphicallabels(n) As Label
For i As Integer = 0 To valuelist.length
graphicallabels(i) = New Label
With graphicallabels(i)
.Location = New Point(0, i * 2 * graphicallabels(i).Height)
.Width = valuelist(i)
End With
If valuelist(i)> 50 Then
graphicallabels(i).BackColor = Color.Green
Else
graphicallabels(i).BackColor = Color.Red
End If
Next
Note: untested code
HTH

Related

How to add list boxes and text boxes in Excel VBA Userforms dynamically as needed?

I do not know if this is appropriate to place in stackoverflow despite being VBA related--particularly in the UserForms area, however, I cannot visualize at all how i'm going to code this.
My Excel worksheet is shown below:
The UserForm I created to input the data is as shown below:
However, what I want to achieve is similar to how QuickBooks does it where the Amount Due is automatically distributed among the Expense Accounts without having to input them 1 by 1 as shown in the first image (My excel worksheet). Also, when there are more expense accounts than usual (e.g. 10 expense accounts, Quickbooks will automatically add new rows for that purpose). A sample is shown below:
My main issue is that I do not know how to let UserForms dynamically add more rows if I need to do so. It can be automatically add rows when all previous rows are filled or something as shown in the image below:
Let n be the number of expense accounts
So from having to input n*2 (or in my case 4 values):
Telephone/Insurance Expense: 40,000
Cash in Bank 40,000
Water Expense: 40,000
Cash in Bank: 40,000
I can to simplify it to n+1 inputs (or in my case 3 inputs):
Amount Due: 80,000
Telephone/Insurance Expense: 40,000
Water Expense 40,000
A lot of examples and tutorials how to add controls dynamically can be found, for example
how-to-create-controls-dynamically-at-runtime
adding-controls-to-a-frame-in-an-excel-userform-with-vba
vba-userform-basics-add-controls-dynamically-at-run-time
However, depending on the maximum number of lines, maybe it is a better solution to create all the controls at design time, set the Visible-property to false and change it to true when needed.
You can add controls based on the label click in the UserForm code.
You'll have to maintain a count of rows in order to utilize that to position them neatly and to move this label from your example down (otherwise it'll be hidden behind the textboxes you add).
Sample code for the UserForm:
Private rowCount As Integer 'To keep track of rows.
Private Sub UserForm_Initialize()
rowCount = 1
End Sub
Private Sub LabelAddRow_Click() 'Event handler: Add row on click of label.
Dim tb As Control
rowCount = rowCount + 1
Set tb = Me.Controls.Add("Forms.TextBox.1", "textBox" & rowCount & "left", True) 'Add left TextBox.
With tb 'Position and size it:
.Top = 25 + (rowCount * 25) + 10
.Left = 25
.Height = 25
.Width = 100
End With
Set tb = Me.Controls.Add("Forms.TextBox.1", "textBox" & rowCount & "right", True) 'Same for the right one:
With tb
.Top = 25 + (rowCount * 25) + 10
.Left = 150
.Height = 25
.Width = 100
End With
LabelAddRow.Top = LabelAddRow.Top + 25 'Move the label down.
End Sub
You might want to add something to the height of the UserForm too, to make it grow with the added controls. Also you might want to reposition other controls that are below the TextBoxes.
If you want to automatically add rows when the last box has been filled out, you're going to need to add some events to the newly added controls, however that's not the main issue listed in the question.
Edit: The above is obviously based on your last image, since that's the "quick win"; dealing with the events of dynamically added TextBoxes will add additional complexity. If you decide to take that route, I suggest to have a go at this first and post new questions as they come along.

I have 29 textboxes and 29 labels and lots of (IF)'s

i have 29 textBoxes and 29 labels
how can i make it easier instead of writning all 29's every time
and there is 29 (IF)
Label1.ForeColor = c2
Label2.ForeColor = c1
Label3.ForeColor = c2
' There is lot of texts and labels and (IF) condition
For those cases usually there are some controls that you need to change and other that need to be static. If that is the case you can use a List(T) to store the elements you want to change and then as #FreeMan stated use a For Each loop
For your case the code would be something similar to this(please note this is an idea and you may need to tweak it):
Dim lblList as New List(Of Label)({Label1, Label2, Label3})
'Then you loop the List to assign the values. Even can include if statements
For Each lbl As Label In lblList
lbl.ForeColor = c1
Next
Please give it a try and let me know your comments

Resize pivot chart when selecting different less/more values

When creating the pivot chart using VBA, I set the size of the chart depending on the number of different values that I have in the chart. With pivot charts you have the option to select only some values of the chart. So for example if I have this chart:
And then I select only 2 I get this:
This is too big and sometimes it can be even bigger. What I would like is to resize it automatically when a user select less so that it automatically become smaller. So I would like it to be something like this:
Is there any way to change the width automatically using VBA?
You may want to take a look at these: resize (mrexcel) & Count number of series (stackoverflow)
In these sources there are some code snippets for getting the number of series in a chart and resizing charts to a fit to a range in the worksheet
Im by no means an expert coder, but with the code and concepts in the above you may be able to do something like this to a given ChartObject:
'get the number of series in a given ChartObject:
numberOfSeries = ChartObject.Chart.SeriesCollection.Count
'you can use this to specify a width in cells/as a range. For the purpose of this example I
'eventually want the chart to cover 1 cell width for each series, + 1.
myChartWidth = numberOfSeries + 1
'Give the chart a name. Smarter people may supply a solution that reference the ChartObject
'directly, but I am not that clever
ChartObject.Name = "Your Chart"
'I will use A1 as reference for setting width, change as you see fit
Sheet1.Shapes("Your Chart").Width = Sheet1.Range(Sheet1.Cells(1, 1), Sheet1.Cells(1, myChartWidth)).Width
'I assume you want to fix the height, adjust to your requirements
Sheet1.Shapes("Your Chart").Height = Sheet1.Range("A1:A10").Height
'If you want to also place the chart top left corner in the in the top left of A1, you can do the following
Sheet1.Shapes("Your Chart").Left = Sheet1.Cells(1, 1).Left
Sheet1.Shapes("Your Chart").Top = Sheet1.Cells(1, 1).Top
You probably want to place and size it differently, but I hope the ideas or the linked sources can get you closer to a solution
Finally I found a solution using the change event. So when I select different values I check how many values I have and then resize my chart depending on the values quantity.
Here is an example of what I used (I have 2 different pivot tables in my sheet)
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo GetOut
If Target.PivotTable.Name = "PivotTable1" Then
Sheets("PerPublication").Shapes(1).Width = (Target.PivotTable.RowRange.Count * 50) + 100
End If
If Target.PivotTable.Name = "PivotTable2" Then
Sheets("PerPublication").Shapes(2).Width = (Target.PivotTable.RowRange.Count * 40) + 40
End If
GetOut:
If Err.Description <> "" Then
Err.Clear
End If
End Sub

Visual Basic Data Grid View - Count Average

Okay, so I've got a datagrid view DataGridView1 which is something like the below example.
Name Points
Jack 15
zack 19
Cody 05
I want to be able to count the average of all of the points, However the amount of points will be dynamic and change from time to time. So my solution must be able to work even if there's just two numbers and when there's upwards of 20.
I have been looking a round for a way of doing this, but most posts are only relevant if the amount of 'points' are static. And a lot of the solutions appear to be written in C++, which isn't too much use to a newbie coder like myself on Visual Basic.
So could anyone help me out here?
I would force it to draw a nice extra line in the data grid, by using a union select and a blanks for each column in the sqldatabind. This would add a nice row to the dataset be formatted as a totals row.. You may need to number the rows so your totals row is last most because of ordering. add the term 'Total' as a control in the results or handle it another way.
After that its just a bit of tweaking when your grid draws the row..
something like ...
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim dRow As GridViewRow = sender
If dRow.RowType = DataControlRowType.DataRow Then
If dRow.Cells(0).Text = "Total" Then
Dim rx As Integer
Dim TotalValue As Double = 0
For rx = 0 To GridView1.Rows.Count - 2
TotalValue += CDbl(GridView1.Rows(rx).Cells(1).Text)
Next
dRow.Cells(1).Text = FormatNumber(TotalValue, 2)
End If
End If
End Sub

How can i get a large number of text boxs to subtract from each other

I'm trying to make a program where a user inputs numbers into a subtraction equation and the program tells them if they are right or wrong and what the correct answer is in a label. There are 20 different equations with 3 text boxes each. The first two text boxes are for the two numbers that are being subtracted and the third text box is the answer. I declared them into a array but I can't figure out how make them subtract. The code i have so far is:
Dim i As Integer
Dim txtNumber1() As TextBox = {txt1Number1, txt2Number1, txt3Number1, txt4Number1, txt5Number1, txt6Number1, txt7Number1, txt8Number1, txt9Number1, txt10Number1, txt11Number1, txt12Number1, txt13Number1, txt14Number1, txt15Number1, txt16Number1, txt17Number1, txt18Number1, txt19Number1, txt20Number1}
Dim txtNumber2() As TextBox = {txt1Number2, txt2Number2, txt3Number2, txt4Number2, txt5Number2, txt6Number2, txt7Number2, txt8Number2, txt9Number2, txt10Number2, txt11Number2, txt12Number2, txt13Number2, txt14Number2, txt15Number2, txt16Number2, txt17Number2, txt18Number2, txt19Number2, txt20Number2}
Dim txtAnswer() As TextBox = {txt1Answer, txt2Answer, txt3Answer, txt4Answer, txt5Answer, txt6Answer, txt7Answer, txt8Answer, txt9Answer, txt10Answer, txt11Answer, txt12Answer, txt13Answer, txt14Answer, txt15Answer, txt16Answer, txt17Answer, txt18Answer, txt19Answer, txt20Answer}
Dim intAnswer() As Integer
For i = 0 To txtNumber1.Length - 1
intAnswer(i) = txtNumber1(i) - txtNumber2(i)
Next
I also can't figure out how i would make each answer display into a label. I think it would be some like
If intAnswer(0) = txtAnswer(0) Then
Me.lblAnswer1.Text = "Correct:" & intAnswer(0)
Else
Me.lblAnswer1.Text = "Incorrect:" & intAnswer(0)
End If
But I'm not sure how i would loop that to make it do all 20 labels, or would i just need to have it 20 different times, one for each label.
Thanks for the help.
Best to create a user control with 3 labels and 3 textboxes on each. Then you only need to code this much, and wrap this logic in a loop to repeat as many times as you want. Basically, narrow down your problem to "I only have 1 equation", solve it using this approach, the rest is as easy as using adding a loop to your code.