vb.net中的饼图控件如何使用文本框来配置值 [英] Pie Chart control in vb.net how to assing values using text boxes

查看:212
本文介绍了vb.net中的饼图控件如何使用文本框来配置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!我知道这是我在vb.net中关于图表的第三个问题,这很烦人,但我需要它用于我的应用程序。所以我从工具箱中删除了一个图表组件,我添加了1个系列和8个数据点。我也有8个文本框。我想要做的就是当用户点击按钮时,饼图值将是文本框中的值!请问您能提供一些编码或外部网站来解释吗?我希望你能理解我!谢谢!

解决方案

我把你的问题投了5,因为它让你在标题中输入了错字。 :)你应该编辑你的标题。

实际上,你写的方式有时看起来合适。



这是一个简单的例子我一起向您展示了将TextBox绑定到饼图的一种方法。当我第一次阅读你的问题时,使用按钮的部分没有注册。所以这个例子不使用按钮来更新图表;更新是自动的。



为此,启动一个新项目并向表单添加一个图表控件和两个TextBox控件(可以添加尽可能多的TextBoxes如你所愿,以后)。然后右键单击表单并选择查看代码。用这个替换现有代码并使用它。



作为参考,您可以找到链接 [ ^ ]很有用。



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

Public Class Form1
'使用BindingList来表示列表更改
Private WithEvents Sectors As New ComponentModel.BindingList(Of SectorItem)

Private Sub Form1_Load(ByVal sender As System。 Object,ByVal e As System.EventArgs)处理MyBase.Load
'设置文本仅用于演示目的
TextBox1.Text =10
TextBox2.Text =5

'定义一个饼图系列
Dim PieSeries1 As New Charting.Series(Pie1)
PieSeries1.ChartType = Charting.SeriesChartType.Pie
PieSeries1.CustomProperties =PieLabelStyle = Disabled

Chart1.Series.Clear()'重置图表系列集合
Chart1.Series.Insert(0,PieSeries1)'将PieSeries1添加到Chart1

'添加SectorItems
Sectors.Add(New SectorItem(TextBox1,Sector 1))
Sectors.Add(New SectorItem(TextBox2,Sector 2))
Sectors.ResetItem(0)'强制ItemChanged事件设置Points DataBinding
End Sub

'''< summary>
'''每当任何SectorItem因名称更改或
'''数据绑定更改时更新图表数据TextBox.Text更改
'''< / 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>
'''用于将TextBox值绑定到图表的辅助类。
'''< / summary>
私有类SectorItem
'实现INotifyPropertyChanged以表示对属性值的更改
Implements System.ComponentModel.INotifyPropertyChanged
公共事件PropertyChanged(ByVal sender As Object,ByVal e As System.ComponentModel .PropertyChangedEventArgs)实现System.ComponentModel.INotifyPropertyChanged.PropertyChanged

Public Sub New(ByVal tb As TextBox,ByVal Name As String)
Me._Name = Name
Double.TryParse( tb.Text,_Value)
'bind对TextBox.Text属性的值
'你可能想要使用DataSourceUpdateMode.OnValidation而不是DataSourceUpdateMode.OnPropertyChanged
'OnPropertyChanged在你输入时触发每个更改在TextBox
tb.DataBindings.Add(Text,Me,Value,True,DataSourceUpdateMode.OnPropertyChanged)
End Sub

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

Private _Name As String
Public Property Name()As String
Get
返回_Name
结束获取
设置(ByVal值为字符串)
'如果值实际更改,则仅进行单值更改
Dim notify As Boolean = Not String.Equals(_Name,value)
_Name = value
如果通知然后NotifyPropertyChanged(名称)
结束集
结束物业'名称

私人_Value As Double
公共财产价值()As Double
获取
返回_Value
结束获取
设置(ByVal值为Double)
'如果值实际变化$ b $只有信号变化b Dim notify As Boolean =(_ Value<>价值)
_Value =价值
如果通知则NotifyPropertyChanged(价值)
结束集
结束物业'价值
结束类'SectorItem
结束类


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!

解决方案

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


这篇关于vb.net中的饼图控件如何使用文本框来配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆