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