I have a form with a groupbox but is initializing its controls from another class. along with the controls is a button with its event declared in the initializing class. Now I need to access the properties of the buttons that is programmatically initialized. I use these commands:
private void buttonUniAdd_Click(object sender, EventArgs e)
{
myProg.Misc frmMisc = (myProg.Misc)System.Windows.Forms.Application.OpenForms["Misc"];
System.Windows.Forms.Button btnAdd = (System.Windows.Forms.Button)frmMisc.Controls["buttonOAdd"];
btnAdd.Text = "&Cancel"; //<-- exception during runtime
}
unfortunately this causes the exception "Object reference not set to an instance of an object.". These are all inside the initialization class that I made.
Mark GutierrezPosted Oct 1, 2007, 2:22 PM
System.Windows.Forms.
Button btnAdd = (System.Windows.Forms.Button)frmMisc.Controls["groupBox1"].Controls["buttonOAdd"];Now it works. Thanks
AlanPosted Oct 1, 2007, 2:21 PM
Ah, the buttons are actually contained in a groupbox rather than directly on the form so they won't actually appear in the form's controls collection but they will appear in the groupbox's controls collection.
So, you need something like this:
private void buttonUniAdd_Click(object sender, EventArgs e)
{
testProg.Form1 frmTest = (testProg.Form1)System.Windows.Forms.Application.OpenForms["Form1"];
System.Windows.Forms.GroupBox gbx = (System.Windows.Forms.GroupBox)
frmtest.Controls["groupBox1"];
System.Windows.Forms.Button thisBtn = (System.Windows.Forms.Button)sender;
System.Windows.Forms.Button btnDel = (System.Windows.Forms.Button)gbx.Controls["buttonODelete"]; // this seems to be name rather than buttonODel
thisBtn.Text = "&Cancel";
btnDel.Enabled = false;
}
Mark GutierrezPosted Oct 1, 2007, 1:15 PM
here are the full code:
testclass.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace clTestProg
{
public class testClass
{
public void InitControls(System.Windows.Forms.GroupBox gbx)
{
System.Windows.Forms.Button buttonODelete = new System.Windows.Forms.Button();
System.Windows.Forms.Button buttonOUpdate = new System.Windows.Forms.Button();
System.Windows.Forms.Button buttonOAdd = new System.Windows.Forms.Button();
//
// buttonODelete
//
buttonODelete.Location = new System.Drawing.Point(251, 324);
buttonODelete.Name = "buttonODelete";
buttonODelete.Size = new System.Drawing.Size(75, 23);
buttonODelete.TabIndex = 0;
buttonODelete.Text = "&Delete";
buttonODelete.UseVisualStyleBackColor = true;
buttonODelete.Click += new System.EventHandler(buttonUniDel_Click);
//
// buttonOUpdate
//
buttonOUpdate.Location = new System.Drawing.Point(170, 324);
buttonOUpdate.Name = "buttonOUpdate";
buttonOUpdate.Size = new System.Drawing.Size(75, 23);
buttonOUpdate.TabIndex = 0;
buttonOUpdate.Text = "&Update";
buttonOUpdate.UseVisualStyleBackColor = true;
buttonOUpdate.Click += new System.EventHandler(buttonUniEdit_Click);
//
// buttonOAdd
//
buttonOAdd.Location = new System.Drawing.Point(89, 324);
buttonOAdd.Name = "buttonOAdd";
buttonOAdd.Size = new System.Drawing.Size(75, 23);
buttonOAdd.TabIndex = 0;
buttonOAdd.Text = "&Add";
buttonOAdd.UseVisualStyleBackColor = true;
buttonOAdd.Click += new System.EventHandler(buttonUniAdd_Click);
gbx.Controls.Add(buttonODelete);
gbx.Controls.Add(buttonOUpdate);
gbx.Controls.Add(buttonOAdd);
}
private void buttonUniAdd_Click(object sender, EventArgs e)
{
testProg.Form1 frmTest = (testProg.Form1)System.Windows.Forms.Application.OpenForms["Form1"];
System.Windows.Forms.Button thisBtn = (System.Windows.Forms.Button)sender;
System.Windows.Forms.Button btnDel = (System.Windows.Forms.Button)frmTest.Controls["buttonODel"];
thisBtn.Text = "&Cancel";
btnDel.Enabled = false;
}
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace testProg
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonTest_Click(object sender, EventArgs e)
{
clTestProg.testClass myLib = new clTestProg.testClass();
myLib.InitControls(groupBox1);
}
}
}
Note: the form only has a 2 controls a groupbox with the name groupBox1 and a button with the name buttonTest. The form name is Form1.
AlanPosted Oct 1, 2007, 12:38 PM
Well, for some reason, btnAdd is null so the code's not finding it within the form's Controls collection.
You could try cycling through the collection and printing out the names of the controls to a MessageBox, just to make sure what you've got in there.
Mark GutierrezPosted Oct 1, 2007, 12:03 PM
AlanPosted Oct 1, 2007, 11:54 AM
The code looks OK to me.
Are you sure that you set the Name property for the Button you're trying to access?