当前位置:编程学习 > C#/ASP.NET >>

再问:两个窗体之间如何传递数据?

A窗体中有一个过程,需要获得B窗体中ComboBox的数据,请问该如何实现!
请告诉个思路,有代码说明也可以!
--------------------编程问答-------------------- 通过窗体变量来实现.
dim a as form1
dim b as form2

B窗体中ComboBox的数据
b.combobox.item(n) 来获取
--------------------编程问答-------------------- public --------------------编程问答-------------------- 这要看A和B的关系,如果B是通过A的某个方法实例化的,可以直接在A中访问B的Combox,如果A和B由其它窗体显示出来,则B需要将Combobox申明为public Shared --------------------编程问答-------------------- 我这样做:
在窗体A中
一个Button1_Click事件
dim frm as B=new B
textbox1.text=frm.ComboBox1.Item(ComboBox1.SelectIndex)
textbox1在窗体A中

结果没有反应~~~~~~~~~~
请指示 --------------------编程问答-------------------- 你按照 Bote_China 说的设置了吗? --------------------编程问答-------------------- 写个属性也可以实现 --------------------编程问答-------------------- 我通常是用一个“中间人”,这个人就是一个放在一个单独模块类里的的一个Public变量!
--------------------编程问答-------------------- 用

 a=FormB.comboBox.text

就可以把这个变量传递出去了吧, --------------------编程问答-------------------- Text=((TextBox)Page.FindControl("TextBox1")).Text; --------------------编程问答-------------------- 定义一个全局变量如何? --------------------编程问答-------------------- Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim frm As Form2 = New Form2
#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(37, 66)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(207, 21)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(59, 141)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(162, 37)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(57, 198)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(164, 25)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Button2"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With frm.ComboBox1
            .Items.Add("12")
            .Items.Add("132")
            .Items.Add("142")
        End With
        frm.Visible = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If frm.ComboBox1.SelectedIndex > 0 Then
            TextBox1.Text = frm.ComboBox1.Items(frm.ComboBox1.SelectedIndex)
        End If
    End Sub
End Class --------------------编程问答-------------------- http://community.csdn.net/Expert/TopicView3.asp?id=5620399
这儿我写了一个,跟你的需求差不多吧。可以借鉴之
呵呵 --------------------编程问答-------------------- 这个代码你看看,用的是委托。
Public Class Form1
    Private counterform As New Form2
    Public Delegate Sub showcounter(ByVal value As Integer)
    Private counter As showcounter
    Private Sub init()
        Me.counter = AddressOf counterform.setcountervalue
        counterform.Show()
    End Sub
 
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Static count As Integer = 0
        count += 1
        If Not counter Is Nothing Then
            counter(count)

        End If
        init()
    End Sub
End Class

Public Class Form2
    Public Sub setcountervalue(ByVal value As Integer)
        Me.Label2.Text = CStr(value)
    End Sub
End Class
点击主窗体的按钮,就像从窗体传递一次值,通过从窗体显示该值。 --------------------编程问答-------------------- 感谢各位高手,问题已经解决了!
跟大家学了很多东西..........
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,