简体   繁体   English

为什么Windows Form TextBox不会从外部类更新?

[英]Why Windows Form TextBox won't update from outside class?

Newbie here. 新手在这里。 I'm running Visual Studio C# Express 2008. I have two Windows Forms, each with a TextBox. 我正在运行Visual Studio C#Express2008。我有两个Windows窗体,每个窗体都有一个文本框。 The textboxes update within the same class but not as the result of a invoked method from outside the class. 文本框在同一类中更新,但不是从类外部调用方法的结果。 I need to be able to update tbRooms.Text when the UpdateListOfRooms() method is invoked. 调用UpdateListOfRooms()方法时,我需要能够更新tbRooms.Text。 I've outlined the problem in pseudo-code below. 我在下面的伪代码中概述了该问题。 I appreciate your help! 我感谢您的帮助!

fLocations.cs fLocations.cs

fLocations_Load()
{
    this.tbLocations.Text = Properties.Settings.Default.LocationID + " locationsLoad";  --updates
}

dgvLocations_SelectionChanged()
{
    var rooms = new fRooms();
    rooms.tbRooms.Text = Properties.Settings.Default.LocationID + " locationssSelectionChanged";  --updates
    rooms.UpdateListOfRooms(); 

}

fRooms.cs fRooms.cs

fRooms_Load()
{
    this.tbRooms.Text = Properties.Settings.Default.LocationID + " roomsLoad"; --updates

}

UpdateListOfRooms()
{
    this.tbRooms.Text = Properties.Settings.Default.LocationID + " roomsUpdateListOfRooms";  --does NOT update; still says "roomsLoad"
}

Updated 8/20/14: I've been a busy bee :) I read all the parts of the tutorial by @jmcilhinney and decided to approach this by including references to the two forms, Locations and Rooms, in the MainMenu class that launches them: 2014年8月20日更新:我一直很忙:)我阅读了@jmcilhinney的教程的所有部分,并决定通过在启动的MainMenu类中包括对位置和房间这两种形式的引用来实现此目的。他们:

(MainMenu.cs) Instances of Locations and Rooms are created. (MainMenu.cs)创建位置和房间的实例。 In the constructor, 'rooms' is passed to the 'locations' instance and both forms are shown. 在构造函数中,将“房间”传递给“位置”实例,并显示两种形式。

(Locations.cs) Another Rooms instance is created at class scope so it can be seen by all methods of the class. (Locations.cs)在类范围内创建了另一个Rooms实例,因此该类的所有方法都可以看到它。 In the constructor, this instance is set to the one being passed by MainMenu which means that this class is working with the same instance created in MainMenu. 在构造函数中,此实例设置为MainMenu传递的实例,这意味着此类正在使用MainMenu中创建的相同实例。 When the user changes the selection on dgvLocations, the 'dgvLocations_SelectionChanged' event is fired which invokes the Rooms.UpdateRooms method. 当用户更改dgvLocations上的选择时,将触发'dgvLocations_SelectionChanged'事件,该事件将调用Rooms.UpdateRooms方法。

(Rooms.cs) The 'UpdateRooms' method displays a new set of rooms based on the passed value of 'locationID'. (Rooms.cs)“ UpdateRooms”方法基于传递的“ locationID”值显示一组新的房间。

This link was helpful. 该链接很有帮助。 Visual C# - Access instance of object created in one class in another . Visual C#-在另一个类的一个类中创建的对象的访问实例

public partial class MainMenu : Form
{   
    Locations locations;
    Rooms rooms;    

    public MainMenu()
    {       
        rooms = new Rooms();        
        locations = new Locations(rooms);       
        locations.Show();
        rooms.Show();
        InitializeComponent();
    }
}

public partial class Locations : Form
{   
    Rooms rooms;

    public Locations(Rooms r)
    {
        rooms = r;
        InitializeComponent();
    }

    private void Locations_Load(object sender, EventArgs e)
    {
        // Populate this.dgvLocations using SQL query.
    }

    private void dgvLocations_SelectionChanged(object sender, EventArgs e)
    {

        // Update the rooms instance with current locationID.
        rooms.UpdateRooms(dgvLocations.CurrentCell.Value.ToString());
    }

}

public partial class Rooms : Form
{
        public Rooms()
        {   
            InitializeComponent();
        }

        private void Rooms_Load(object sender, EventArgs e)
        {   
            // Populate this.dgvRooms using SQL query.
        }

        public void UpdateRooms(string locationID)
        {
            // Update dgvRooms based on user changing the locationID in dgvLocations
        }
}

In the first code snippet, you create a new fRooms object but you never call its Show or ShowDialog method, which means that you never display it to the user. 在第一个代码段中,您创建了一个新的fRooms对象,但是您从未调用它的ShowShowDialog方法,这意味着您永远不会将其显示给用户。 That means that any changes you make to that form will not be seen. 这意味着将不会看到您对该表单所做的任何更改。 Presumably the user can see an fRooms object though, but you are not making any changes to that one. 大概用户可以看到一个fRooms对象,但是您没有fRooms对象进行任何更改。

Consider this. 考虑一下。 Let's say that I give you a note pad and you open it and look at the first page. 假设我给您一个记事本,您将其打开并查看第一页。 Let's say that I now buy a new note pad and write on the first page of it. 假设我现在购买了一个新的记事本,并写在它的第一页上。 Would you expect to see the words I wrote magically appear on the page in front of you? 您是否希望看到我神奇地写的字样出现在您面前的页面上? Of course not. 当然不是。 We both are holding a note pad but they are two different note pads. 我们俩都拿着一个记事本,但是它们是两个不同的记事本。 You're looking at one and I'm writing on the other, so you won;t see what I write. 您在看一个而我在写另一个,所以您不会看我在写什么。

The same goes for your forms. 您的表格也是如此。 They are both fRooms objects but they are two different fRooms objects. 它们都是fRooms对象,但是它们是两个不同的fRooms对象。 Changes you make to one will not affect the other. 您对一个所做的更改不会影响另一个。 If you want the user to see the changes you make then you must make those changes to the fRooms object that the user is looking at. 如果希望用户看到所做的更改,则必须对用户正在查看的fRooms对象进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM