从另一个表单访问表单数据 [英] Accessing Forms data from another form

查看:107
本文介绍了从另一个表单访问表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出下面的代码,我将如何访问Form2中的listBox1?我确定我缺少愚蠢!在此先感谢."

"Give the code below, how would I access the listBox1 in Form2? I'm sure I'm missing stupid! Thanks in advance."

由于无法访问错误1'WindowsFormsApplication1.Form2.listBox1' 对其保护 C级:\ Users \ dugaj0 \ Desktop \ Developing \ GlobalUser \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 24 19 WindowsFormsApplication1

Error 1 'WindowsFormsApplication1.Form2.listBox1' is inaccessible due to its protection level C:\Users\dugaj0\Desktop\Developing\GlobalUser\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 24 19 WindowsFormsApplication1

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String value1 = File.ReadAllText(textBox1.Text);
            foreach (string line in value1.Split('\n'));
            Form2.listBox1.Items.Add();
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

namespace WindowsFormsApplication1
{
    partial class Form2
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // listBox1
            // 
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(13, 13);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(259, 212);
            this.listBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(105, 231);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Exit";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form2
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.listBox1);
            this.Name = "Form2";
            this.Text = "Form2";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Button button1;
    }
}

推荐答案

首先,您必须创建Form2的实例.

First of all you have to create an instance of Form2.

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    private Form2 form2;

    public Form1()
    {
        InitializeComponent();
        form2 = new Form2();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        String value1 = File.ReadAllText(textBox1.Text);
        foreach (string line in value1.Split('\n'))
        {
            form2.listBox1.Items.Add(line);
        }
    }
}

}

您的特定错误是由于listBox1是私有的.将其更改为公开,您可以访问它.

Your specific error is because of listBox1 is being private. Change it to public and you can access it.

public System.Windows.Forms.ListBox listBox1;

还因为您已经拥有:

 using System.Windows.Forms;

你可以写

public ListBox listBox1;

这篇关于从另一个表单访问表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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