首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么从主窗体传值到子窗体

2014-01-09 
如何从主窗体传值到子窗体 IPEndPoint readerip new IPEndPoint(IPAddress.Parse(currentDevice.DeviceI

如何从主窗体传值到子窗体
 IPEndPoint readerip = new IPEndPoint(IPAddress.Parse(currentDevice.DeviceIP),currentDevice.PortNo);
我如何将主窗体的currentDevice.DeviceIP和currentDevice.PortNo传值给子窗体?
           public string  IP()
          {
              string ip;
              ip = currentDevice.DeviceIP;
              return ip;        
          }
          public int PORT()
          {
              int port;
              port = currentDevice.PortNo;
              return port;
          }
想试着用上面的函数传值,可是报错:未将对象引用设置到对象的实例。
我应该怎么修改呢?或者是谁有好的方法呢?
[解决办法]
报错,应该是你的currentDevice为null,你需要先赋值,至于子窗体传值,你需要说清楚子窗体与主窗体的逻辑关系以及调用关系,确切的说是应用场景。传值方式还是有很多的
[解决办法]
窗体间传值的几种方法,搬运过来的~
第一种方法:
创建一个类,里面声明用于存储接收的字段。传的时候存储于字段中,要用的时候,直接类名.字段名 进行调用。(这种方法传递是双向的)
 
第二种方法:
1.在Form1里定义

public string Name = "*****"

 
2. 在Form2里创建Form1对象,

Form1 f = new Form1();

 
然后就可以通过f.Name取值了
 
第三种方法:用构造函数
在窗体Form2中


int value1; 
string value2; 

public Form2 ( int value1 , string value2 ) 

     InitializeComponent ( ); 

     this.value1 = value1; 
     this.value2 = value2; 
}


在窗体Form1中这样调用

new Form2 ( 111 , "222" ).Show ( ); 

这样就把111,"222",这2个值传送给了Form2  d
但是这样的传值是单向的

第四种方法:通过窗体的公有属性值(特点:实现简单)
举例“在窗体Form2中定义一个公有属性Form2Value,获取和设置textBox1的文本值



public string Form2Value 

     get 
     { 
          return this.textBox1.Text; 
     } 
     set 
     { 
         this.textBox1.Text = value; 
     } 



在窗体Form1中这样调用

Form2 f2 = new Form2 ( ); 
f2.Form2Value = "Ok"; //给Form2的textBox1赋值Ok 
f2.ShowDialog ( ); 

 
第五种方法:通过窗体的公有属性值和Owner属性(特点:实现简单,灵活)

在窗体Form1中

public int Form1Value = 1; 

Form2 f2 = new Form2 ( ); 
f2.ShowDialog ( this ); //把Form1作为Form2的所有者传递给Form2 

在窗体Form2中


//Form2的所有者是Form1 
Form1 f1 = ( Form1 ) this.Owner; 
//取到Form1的值是1 
MessageBox.Show ( f1.Form1Value .ToString ( ) ); 
//给Form1的Form1Value赋值222 
f1.Form1Value = 222; 


 
第六种方法:通过窗体的公有属性值和Application.OpenForms属性(感觉用的比较少)
说明:Application.OpenForms属性:获取属于应用程序的打开窗体的集合。(此属性在 .NET Framework2.0版中)
实现代码如下: 
在窗体Form1中

public int Form1Value = 1; 

Form2 f2 = new Form2 ( ); 
f2.Show ( ); 

在窗体Form2中


string formName = "Form1"; 
Form fr = Application.OpenForms [ formName ]; 

if ( fr != null ) 

     Form1 f1 = ( Form1 ) fr; 
     //取到Form1的值是1 
     MessageBox.Show ( f1.Form1Value.ToString ( ) ); 
     //给Form1的Form1Value赋值222 


     f1.Form1Value = 222; 



 
第七种方法:通过事件
在窗体Form2中定义公有属性Form2Value,获取和设置textBox1的文本值 
并且还定义一个accept事件


public string Form2Value 

     get 
     { 
          return this.textBox1.Text; 
     } 
     set 
    { 
         this.textBox1.Text = value; 
    } 


public event EventHandler accept; 

private void button1_Click ( object sender , EventArgs e ) 

     if ( accept != null ) 
     { 
          accept ( this , EventArgs.Empty ); //当窗体触发事件,传递自身引用 
     } 



在窗体Form1中


Form2 f2 = new Form2 ( ); 
f2.accept += new EventHandler ( f2_accept ); 
f2.Show ( ); 

void f2_accept ( object sender , EventArgs e )

     //事件的接收者通过一个简单的类型转换得到Form2的引用 
     Form2 f2 = (Form2) sender; 
     //接收到Form2的textBox1.Text 
     this.textBox1.Text = f2.Form2Value; 
}
[解决办法]
怎么从主窗体传值到子窗体
怎么从主窗体传值到子窗体

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace df
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Mainwindows());
           
        }
        public  class DemoEdit:TextBox
        {
            protected override void DefWndProc(ref Message m)
            {
                if (m.Msg == 0x400)
                {
                    var ptrToStringAuto = Marshal.PtrToStringAnsi(m.LParam, (int) m.WParam);
                    Text += ptrToStringAuto;
                    Text += "\n";

                    return;
                }
                base.DefWndProc(ref m);
            }

            public DemoEdit()
            {
                Multiline = true;
                Dock = DockStyle.Fill;

            }

            public override sealed bool Multiline
            {
                get { return base.Multiline; }
                set { base.Multiline = value; }
            }


        }

        public class Mainwindows:Form
        {
            [DllImport("user32.dll")]
            public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);

            private readonly DemoEdit _edit = new DemoEdit();
            readonly Timer _evTimer = new Timer();
            public Mainwindows()
            {
                Controls.Add(_edit);
                _evTimer.Interval = 1000;
                _evTimer.Enabled = true;
                _evTimer.Tick += _evTimer_Tick;
                _evTimer.Start();
            }

            void _evTimer_Tick(object sender, EventArgs e)
            {
                var sb = new StringBuilder("Hello xx oo are y ready ?");
                SendMessage(_edit.Handle, 0x400, (IntPtr)sb.Length, sb.ToString());
            }
        }
    }
}

[解决办法]
事件和实例化属性或字段的赋值应用比较广泛,静态变量传值也是可以的。
[解决办法]

  在Form1中     
      public static Form1 f=null;
      public Form1()
        {
            InitializeComponent();
            f = this;
       }

          public string  IP()
          {
              string ip;
              ip = currentDevice.DeviceIP;
              return ip;        
          }
          public int PORT()
          {
              int port;
              port = currentDevice.PortNo;
              return port;
          }

      private void button1_Click(object sender, EventArgs e)
      {
            Form2 f2 = new Form2();
            f2.Show();      }  

  Form2:

       private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form1.f.IP();
            textBox2.Text = Form1.f.PORT().ToString(); 
        }
         
    

热点排行