HelpNeeder
HelpNeeder

Reputation: 6490

Add entry to textbox in other class?

I am having a form in which I can input data from one window and then press enter button (btnEnter) and click display (btnShowBooks) button and show data in other window. When I press btnEnter with code below and then click btnShowBooks to display input in new window the txtBookList textbox is still empty. Here is a simplified code which doesn't work and hope for some tips.

BookEntry form file

private void btnEnter_Click(object sender, EventArgs e)
{
    BookList bookList = new BookList();
    bookList.txtBookList.Text = "aaa";
}

BookList form file in which I have the layout and close button so there isn't any relevant code. There is only texbox named txtBookList and btnClose button.

In BookList designer file I have made the field public:

public System.Windows.Forms.TextBox txtBookList;

Regards. HelpNeeder.

-- SOLVED --

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;
using System.Collections;

namespace Lab_8
{
    public partial class BookEntry : Form
    {
        BookList bookList = new BookList();

        public BookEntry()
        {
            InitializeComponent();
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            bookList.txtBookList.Text = "aaa";
        }

        private void btnShowBooks_Click(object sender, EventArgs e)
        {
            bookList.ShowDialog();
        }
    }
}

Upvotes: 0

Views: 333

Answers (3)

chemicalNova
chemicalNova

Reputation: 841

I just replicated this and it is working fine (VS2010 .NET4).. are you showing the same instance of the form afterwards? (as below):

BookList bookList = new BookList();
bookList.txtBookList.Text = "aaa";
bookList.Show();

EDIT

Given your response, it seems that you're instantiating separate forms. Here is what you need to do:

First, in your main form, declare this:

private BookList _bookList = new BookList();

Then, wherever you need to reference that form, use _bookList to accomplish that. E.g:

_bookList.txtBookList.Text = "aaa";
_bookList.ShowDialog();

You need to keep 1 reference to the dialog whenever you're performing tasks. If you do this each time you want to operate on that form:

BookList bookList = new BookList();

You are actually creating completely different copies of the form.

Upvotes: 2

Dylan Smith
Dylan Smith

Reputation: 22255

Where are you doing the .Show() on the BookList form. Make sure you not creating 2 separate instances of the form, and setting the textbox on one but showing the other (this is what I suspect).

Upvotes: 1

Law Metzler
Law Metzler

Reputation: 1235

You are creating a new BookList and then losing it as soon as the method returns. Are you trying to access a BookList object inside your BookEntry form? It should look more like:

private void btnEnter_Click(object sender, EventArgs e)
{
    // bookListForm is the ID of the form in the designer 
    bookListForm.txtBookList.Text="aaa";
}

Upvotes: 1

Related Questions