473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Form Calling Objects in Other Forms

I have a form (formA). This form used to following code on FormA to load a
new form(FormB).

formB f = new formB();
f.ShowDialog();

However: on formB, I am unable to access any object (ListBox) created on
FormA. I have the listbox set to internal, however: I cannot seem to be
able to access the selected value of the listbox. I am using the following
code:

int iVerseID;
VerseGroupUtility.frmMain fMain;
fMain = (frmMain)this.Parent;
iVerseID = (int)fMain.lbGroups.SelectedValue;

I keep getting a System.NullReferenceException error, however: I am 100%
positive that there is a value selected and it has a value.

Would anyone be able to provide some information for what I am missing or
doing wrong?

Thanks
Andy
Nov 16 '05 #1
2 1738
HI andy,

you need to first declare a static variable in the Form1 class like the
following.

public class Form1 : System.Windows.Forms.Form
{

public static Form1 myform1 = null;
//all other codes
............
................

}

write a method to return the listbox value

public string getListBoxValue()
{
return listBox1.Text.ToString();
}
assuming that you are opening form2 in a button click event in Form1, write
the following code in form1
private void btnOpenForm2_Click(object sender, System.EventArgs e)
{
myform1 = this;
Form2 objForm2 = new Form2();
objForm2.Show();
}
now you can access and check the listbox in form1. I have written this in
FormLoad of Form2
private void Form2_Load(object sender, System.EventArgs e)
{
MessageBox.Show(Form1.myform1.getListBoxValue());
}

happy programming!!
pradeep_TP

"Andy" wrote:
I have a form (formA). This form used to following code on FormA to load a
new form(FormB).

formB f = new formB();
f.ShowDialog();

However: on formB, I am unable to access any object (ListBox) created on
FormA. I have the listbox set to internal, however: I cannot seem to be
able to access the selected value of the listbox. I am using the following
code:

int iVerseID;
VerseGroupUtility.frmMain fMain;
fMain = (frmMain)this.Parent;
iVerseID = (int)fMain.lbGroups.SelectedValue;

I keep getting a System.NullReferenceException error, however: I am 100%
positive that there is a value selected and it has a value.

Would anyone be able to provide some information for what I am missing or
doing wrong?

Thanks
Andy

Nov 16 '05 #2
Hi Andy,

The Parent property (Control) is for children in a container control and
does not work for windows forms unless they are MDIChildren.

Overload the constructor in formB to accept a FormA reference and store
this reference for later use.

formB f = new formB(this);

....

private FormA parentReference;

public formB(FormA Aref)
{
this.parentReference = Aref)
}

....

iVerseID = parentReference.lbGroups.SelectedValue;

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dan | last post by:
I'm trying to write a Windows application that obtains information from a web service. I can access the web service successfully, but I can't access the object returned by the web service in...
4
by: Tonya | last post by:
Hi, Does anyone have any example of how i can manage forms in my application?? I want to be able to reference my form instances that are currently open from other forms. why cant i open...
4
by: Dan | last post by:
I'm trying to write a Windows application that obtains information from a web service. I can access the web service successfully, but I can't access the object returned by the web service in...
4
by: Tonya | last post by:
Hi, Does anyone have any example of how i can manage forms in my application?? I want to be able to reference my form instances that are currently open from other forms. why cant i open...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.