Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / January 2006

Tip: Looking for answers? Try searching our database.

modify property of a control of formA from formB

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rain County - 26 Jan 2006 01:59 GMT
My main form, frmA, contains a dataGridView, dgvItems.  When you click the
"Configure" menu item on frmA, a configuration form, frmB, is created and
shown.  I wish to use frmB as the place where a user can customize the
dgvItems dataGridView (hide column, change column headings, etc.).

Problems (probably two systems of the same problem):
1) When coding in frmB, intellisense doesn't "know" dgvItems.
2) Then I tried this code istead: DataGridView dgv =
(DataGridView)this.Parent.Controls["dgvItems"];
    When I try to execute this, I get a runtime error: "Object reference
not set to an instance of an object".

   (I changed the declaration of dgvItems to public, and this did not
change the results)

Isn't it possible to access the controls of one form from another?

TIA
Phil
Rain County - 26 Jan 2006 05:51 GMT
I just found *a* solution to my own posted question.  It was so simple, I
thought I'd share it in case anyone else would like to know.

I added a constructor to my frmB which expects a reference to the
dataGridView.  When frmA creates and shows frmB, it passes the reference in
the constructor. The reference is then assigned to a privately declared
dataGridView IN frmB.  Here's code snippets:

frmA -----------------------------------------
       private void configureMenuItem_Click(object sender, EventArgs e)
       {
           frmB frm = new frmB(dgvItems);
           frm.ShowDialog();
       }

frmB ------------------------------------------
   public partial class frmB : Form
   {
       DataGridView dgvItems;

       //Default Constructor -- no longer neeeded; I will delete it
       public frmB()
       {
           InitializeComponent();
       }

       //Custom Constructor
       public frmB(DataGridView dgvItems)
       {
           this.dgvItems = dgvItems;
           InitializeComponent();
       }

Phil

> My main form, frmA, contains a dataGridView, dgvItems.  When you click the
> "Configure" menu item on frmA, a configuration form, frmB, is created and
[quoted text clipped - 15 lines]
> TIA
> Phil

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.