项目总结[3]--浅谈窗体间传值

今天有朋友问起窗体间如何传值,才想起来找到以前写的文章。供大家参考参考,多年前的文章

Public   Class   Form_Load  
          '启动窗体  
          '项目总结[3]--浅谈窗体间传值,并提供实例  
          '项目总结[2]--浅谈如何设置菜单,因为在整理当中  
          '监于有些人提出如何进行窗体间传递变量,和如何查询问题,  
          '特地写了如下例子,   供大家参考  
          Public   Shared   common_string   As   String   =   "   Form1   String"  
          <STAThread()>   Public   Shared   Sub   Main(ByVal   args()   As   String)  
                  Dim   a1   As   Sample1Form1   =   New   Sample1Form1()  
                  Application.Run(a1)  
          End   Sub  
  End   Class  

'2003-11-3  
  '通过公共变量在两个窗体间传递参数  
  '或者通过传递窗体来取的变量  
  'Author:MeetWeb  
  Public   Class   Sample1Form1  
          Inherits   System.Windows.Forms.Form  
   
          Private   Sub   Sample1Form1_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load  
                  Me.Label1.Text   =   Form_Load.common_string  
          End   Sub  
   
          Private   Sub   Button1_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click  
                  '方法一:直接把Form对象传递到Form2  
                  Dim   f2   As   Sample1Form2   =   New   Sample1Form2(Me)  
                  f2.Show()   '显示Form2  
                  Me.Hide()   '隐藏Form1  
          End   Sub  
   
  End   Class  
   
  '2003-11-3  
  '通过公共变量在两个窗体间传递参数  
  '或者通过传递窗体来取的变量  
  'Author:MeetWeb  
  Public   Class   Sample1Form2  
          Inherits   System.Windows.Forms.Form  
    Private   f1   As   Sample1Form1           '定义smapleform1  
          Public   Sub   New(ByVal   f1   As   Sample1Form1)  
                  '把smapleform1当变量传递给smaple1form2  
                  MyBase.New()  
                  '该调用是   Windows   窗体设计器所必需的。  
                  InitializeComponent()  
                  Me.f1   =   f1  
          End   Sub  
          Private   Sub   Sample1Form2_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load  
                  Me.Label1.Text   =   Form_Load.common_string  
          End   Sub  
          Private   Sub   Button1_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click  
                  Me.Close()  
          End   Sub  
          Private   Sub   Button2_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button2.Click  
                  '改变公共变量  
                  Form_Load.common_string   =   "Form2'String"  
                  '改变Form1's   Label显示  
                  f1.Label1.Text   =   Form_Load.common_string  
                  '直接操作Form1的所有控件或者是公共变量  
                  Me.Close()  
                  '重新显示Form1  
                  Me.f1.Show()  
          End   Sub  
  End   Class  

'   2003-7-1  
  '   窗体间相互传递变量应用实例2  
  '通过查询窗体改变主窗体的DataGrid查询  
  '   Author   MeetWeb  
  Public   Class   Sample2Form1  
          Inherits   System.Windows.Forms.Form  
          Public   myFormFather   As   Sample2Form2   '定义主窗体  
          Private   Sub   Sample2Form1_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load  
                  '主窗体实例  
                  myFormFather   =   Me.Owner  
          End   Sub  
   
          Private   Sub   Button1_Click(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click  
                  Dim   findstring   As   String   =   "CustName='"   +   Me.TextBox1.Text   +   "'"  
                  '直接引用主窗体的dataview对象进行数据查询  
                  myFormFather.dataview1.RowFilter   =   findstring  
          End   Sub  
  End   Class  

'   2003-7-1  
  '   窗体间相互传递变量应用实例2[如果需要源码,请留下Email]  
  '   Author   MeetWeb  
  Public   Class   Sample2Form2  
          Inherits   System.Windows.Forms.Form  
   
    Private   WithEvents   myDataSet   As   DataSet  
          Public   dataview1   As   DataView  
          Public   Binding1   As   BindingManagerBase  
          Private   Sub   Sample2Form2_Load(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load  
                  SetUp()  
          End   Sub  
   
  #Region   "Get   DataBase"  
          Private   Sub   SetUp()  
                  MakeDataSet()  
                  '使用dataview绑定数据  
                  dataview1   =   New   DataView(Me.myDataSet.Tables(0))  
                  Binding1   =   Me.BindingContext(dataview1)  
                  '致DataGrid数据源为DataView,目的是为了查询做准备工作  
                  myDataGrid.DataSource   =   Me.dataview1  
   
                  AddCustomDataTableStyle()  
          End   Sub  
   
          Private   Sub   AddCustomDataTableStyle()  
                  '设置DataGrid样式,具体请查看帮助  
                  Dim   ts1   As   DataGridTableStyle  
                  ts1   =   New   DataGridTableStyle()  
                  ts1.MappingName   =   "Customers"  
                  '   Set   other   properties.  
                  ts1.AlternatingBackColor   =   Color.LightGray  
                  Dim   boolCol   As   DataGridColumnStyle  
                  boolCol   =   New   DataGridBoolColumn()  
                  boolCol.MappingName   =   "Current"  
                  boolCol.HeaderText   =   "IsCurrent   Customer"  
                  boolCol.Width   =   150  
                  ts1.GridColumnStyles.Add(boolCol)  
                  Dim   TextCol   As   DataGridColumnStyle  
                  TextCol   =   New   DataGridTextBoxColumn()  
                  TextCol.MappingName   =   "custName"  
                  TextCol.HeaderText   =   "Customer   Name"  
                  TextCol.Width   =   250  
                  ts1.GridColumnStyles.Add(TextCol)  
                  Me.myDataGrid.RowHeaderWidth   =   50   'use   if   no   tablestyle  
                  myDataGrid.TableStyles.Add(ts1)  
   
          End   Sub  
          Private   Sub   MakeDataSet()  
                  '新建立DataSet对象,并把数据录入  
                  '   Create   a   DataSet.  
                  myDataSet   =   New   DataSet("myDataSet")  
                  '   Create   two   DataTables.  
                  Dim   tCust   As   DataTable  
                  tCust   =   New   DataTable("Customers")  
                  '   Create   two   columns,   and   add   them   to   the   first   table.  
                  Dim   cCustID   As   DataColumn  
                  cCustID   =   New   DataColumn("CustID",   GetType(System.Int32))  
                  Dim   cCustName   As   DataColumn  
                  cCustName   =   New   DataColumn("CustName")  
                  Dim   cCurrent   As   DataColumn  
                  cCurrent   =   New   DataColumn("Current",   GetType(System.Boolean))  
                  tCust.Columns.Add(cCustID)  
                  tCust.Columns.Add(cCustName)  
                  tCust.Columns.Add(cCurrent)  
                  '   Add   the   tables   to   the   DataSet.  
                  myDataSet.Tables.Add(tCust)  
                  Dim   newRow1   As   DataRow  
                  Dim   i   As   Integer  
                  i   =   1  
                  Do   While   (i   <   4)  
                          newRow1   =   tCust.NewRow  
                          newRow1("custID")   =   i  
                          '   Add   the   row   to   the   Customers   table.  
                          tCust.Rows.Add(newRow1)  
                          i   =   (i   +   1)  
                  Loop  
                  '   Give   each   customer   a   distinct   name.  
                  tCust.Rows(0)("custName")   =   "Alpha"  
                  tCust.Rows(1)("custName")   =   "Beta"  
                  tCust.Rows(2)("custName")   =   "Omega"  
                  '   Give   the   Current   column   a   value.  
                  tCust.Rows(0)("Current")   =   True  
                  tCust.Rows(1)("Current")   =   True  
                  tCust.Rows(2)("Current")   =   False  
                  'set   topleft   corner   point   so   we   can   locate   the   toprow  
          End   Sub  
   
  #End   Region  
          Private   Sub   Button1_Click_1(ByVal   sender   As   System.Object,   ByVal   e   As   System.EventArgs)   Handles   Button1.Click  
                  Dim   FormChild   As   Sample2Form1   =   New   Sample2Form1()   '定义查询窗口  
                  FormChild.Owner   =   Me       '把主窗体赋予查询窗体的Owner对象  
                  FormChild.Show()               '显示查询窗体  
          End   Sub  
  End   Class  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 不好意思,你的问题似乎不太明确。"c"可能是一个字母,一个单词,或者是其他的某种意思。如果你能够提供更多的背景信息或者明确你想要了解的内容,我将会尽力给你一个更准确的回答。 ### 回答2: 好的,我会用中文来回答您的问题。 C是计算机科学中的一门编程语言,它最初由美国贝尔实验室的丹尼斯·里奇(Dennis Ritchie)在1972年设计和开发。C语言是一种通用的高级编程语言,被广泛用于系统软件、嵌入式系统和应用程序开发等领域。 C语言具有简洁、高效和灵活的特点,它提供了丰富的编程工具和许多底层的操作功能,使得开发者可以更加精确地控制计算机的硬件和软件资源。C语言的语法规则相对简单,易于学习和理解,这也是它成为许多计算机科学教育领域中的第一门编程语言的原因之一。 C语言的应用范围广泛,包括操作系统、编译器、数据库系统、游戏开发和网络应用等。许多著名的软件和系统都是使用C语言开发的,比如UNIX操作系统、Linux操作系统、MySQL数据库等。 另外,C语言还衍生出了许多其他流行的编程语言,比如C++、C#和Objective-C等。这些语言基于C语言,并在其基础上添加了许多新功能和特性,以满足不同领域的需求。 总的来说,C语言在计算机科学中起着重要的作用,它是一门强大而广泛使用的编程语言,为开发者提供了丰富的工具和底层操作能力,使得他们可以更好地控制计算机资源并实现复杂的应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值