Click here to Skip to main content
16,004,564 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello there! I know it's my third question about charts in vb.net and it's annoying but I need it for my application. So I dropped a chart component from the toolbox and i added 1 series and 8 datapoints. I have 8 text boxes too. All I want to do is that when a user clicks a button, the pie chart values will be the ones from the textboxes! Please can you provide some coding , or an external site that explains that? I hope you understand me! Thanks!
Posted
Comments
Sergey Alexandrovich Kryukov 6-Nov-13 14:40pm    
As I can see, you have only one question right now. Probably, your previous questions have been deleted due to abuse reports. I can understand your frustration. However, it could have happened because your did not provide essential information helpful for helping you. If you really need help, you need to provide essential detail: show what did you want to achieve, what have you tried, what did you obtain, how it's different from what you hoped to achieve or why do you think it's wrong. In most cases, comprehensive but short code sample could be most useful.
—SA
Member 14912458 12-Aug-20 5:07am    
Chart1.Series("Total").Points.AddXY(RSC.Fields("C***Name").Value, RSC.Fields("A").Value.ToString)

1 solution

I voted your question a 5 because it made me laugh with your typo in the title. :) You should probably edit your title.
Actually, the way you wrote may seen appropriate at times.

Here is an quick example I threw together to show you one way to bind the TextBoxes to a pie chart. When I first read your question, the part of using a button did not register. So this example does not use a button to update the chart; the update is automatic.

To give this a go, start a new project and add a chart control and two TextBox controls to the form (you can add as many TextBoxes as you want later). Then right-click on the form and select "View Code". Replace the existing code with this and play around with it.

For reference, you may find the link [^]useful.

Imports Charting = System.Windows.Forms.DataVisualization.Charting
Imports ComponentModel = System.ComponentModel

Public Class Form1
   ' use a BindingList to signal list changes
   Private WithEvents Sectors As New ComponentModel.BindingList(Of SectorItem)

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ' setting text just for demo purposes
      TextBox1.Text = "10"
      TextBox2.Text = "5"

      ' define a piechart series
      Dim PieSeries1 As New Charting.Series("Pie1")
      PieSeries1.ChartType = Charting.SeriesChartType.Pie
      PieSeries1.CustomProperties = "PieLabelStyle=Disabled"

      Chart1.Series.Clear()                  ' reset the chart Series collection
      Chart1.Series.Insert(0, PieSeries1)    ' add PieSeries1 to Chart1

      ' add the SectorItems
      Sectors.Add(New SectorItem(TextBox1, "Sector 1"))
      Sectors.Add(New SectorItem(TextBox2, "Sector 2"))
      Sectors.ResetItem(0) ' force a ItemChanged event to set the Points DataBinding
   End Sub

   ''' <summary>
   ''' Updates the chart data each time any of the SectorItems changes due to a Name Change or the
   ''' databound TextBox.Text changes   
   ''' </summary>
   Private Sub sectors_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles Sectors.ListChanged
      If e.ListChangedType = ComponentModel.ListChangedType.ItemChanged Then
         Chart1.Series("Pie1").Points.DataBind(Sectors, "", "Value", "Label=Name")
      End If
   End Sub

   ''' <summary>
   ''' a helper class for binding a TextBox value to a chart.  
   ''' </summary>
   Private Class SectorItem
      ' implement INotifyPropertyChanged to signal changes to the property values
      Implements System.ComponentModel.INotifyPropertyChanged
      Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

      Public Sub New(ByVal tb As TextBox, ByVal Name As String)
         Me._Name = Name
         Double.TryParse(tb.Text, _Value)
         ' bind Value to the TextBox.Text Property
         ' You may want to use DataSourceUpdateMode.OnValidation instead of  DataSourceUpdateMode.OnPropertyChanged
         ' OnPropertyChanged fires on each change as you type in the TextBox
         tb.DataBindings.Add("Text", Me, "Value", True, DataSourceUpdateMode.OnPropertyChanged)
      End Sub

      Private Sub NotifyPropertyChanged(ByVal info As String)
         RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs(info))
      End Sub

      Private _Name As String
      Public Property Name() As String
         Get
            Return _Name
         End Get
         Set(ByVal value As String)
            ' only singal change if value actually changes
            Dim notify As Boolean = Not String.Equals(_Name, value)
            _Name = value
            If notify Then NotifyPropertyChanged("Name")
         End Set
      End Property 'Name

      Private _Value As Double
      Public Property Value() As Double
         Get
            Return _Value
         End Get
         Set(ByVal value As Double)
            ' only singal change if value actually changes
            Dim notify As Boolean = (_Value <> value)
            _Value = value
            If notify Then NotifyPropertyChanged("Value")
         End Set
      End Property 'Value
   End Class ' SectorItem
End Class
 
Share this answer
 
v2
Comments
Mike Vlast 7-Nov-13 11:19am    
Thanks a lot man!! I was searching that but I couldn't fins an answer. I'm thirteen so the easy things for you maybe difficult fo me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900