简体   繁体   中英

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. 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. I've outlined the problem in pseudo-code below. I appreciate your help!

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_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:

(MainMenu.cs) Instances of Locations and Rooms are created. 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. 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. When the user changes the selection on dgvLocations, the 'dgvLocations_SelectionChanged' event is fired which invokes the Rooms.UpdateRooms method.

(Rooms.cs) The 'UpdateRooms' method displays a new set of rooms based on the passed value of 'locationID'.

This link was helpful. Visual C# - Access instance of object created in one class in another .

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. 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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