I am still rather newb with c# and I am just going through the painful task of translating my apps from vb.net to c#.net so I can integrate them all in to one easy to use app.
The problem I have never had before but seem to be having is accessing form controls from my classes.
I try to use a structure where by all database calls are in one class, all validation and calculations are in another class and then only the form controls are on the form code.
So an example of an issue I am having is that I read textbox values from the form and also do things like update the status strip progress bar etc.
I have even tried both with and without delegates and I simply can't manage it.
Usually if I want to access a form control I would reference a new form instance:
Form1 frm = new Form1();
and use it like this:
frm1.ToolStripProgressBar1.Value = 100;
If I do this on the form it works fine:
this.ToolStripProgressBar1.Value = 100;
But from another class just doesn't seem to work.
My guess is that I reference "= new Form1();"
Does this refer to a new instance of the form I am trying to access controls on and if so how do I access the current instance of a form?
I had been reading a little about IEnumerable
Thanks in advance for any help!
Andy
VulpesPosted Apr 19, 2012, 11:02 AM
andy mcinnesPosted Apr 19, 2012, 11:14 AM
andy mcinnesPosted Apr 19, 2012, 10:40 AM
Thanks for the quick answer!
I am struggling a little in applying it.
So I will give a little more specfic detail...
So I have a class called FormFuncs, this deals with any requests and processes for the form.
Currently it is just a class so:
Class FormFuncs
I have a load of public variables defined within this class and many are called by other functions within the class and various form controls.
Currently the objects within the FormFuncs class are called and refernced as follows
private void btDelPd_Click(object sender, EventArgs e)
{
try
{
if (stConst.Text != "Connected")
{
MessageBox.Show("There is no connection");
return;
}
if (MessageBox.Show("This will permanently delete the selected item from the Table, do you want to continue?", "Delete Processed Items", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
FormFuncs f = new FormFuncs();
f.LoadGrid("del", DateTime.Today);
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
}
catch(Exception Err)
{
MessageBox.Show(Err.Data + "\r\n" + Err.Message);
}
}
The function this particular form control calls, uses a public variable called "loc2", however when I currently run it "loc2" is null as I start a new instance.
How would I go about implementing your static class suggestion?
Also so you have a full picture of what I am trying to do, yes there are some functions that don't need any saved variables and for these the new instance is fine, but some like the one described above, rely on the values of the public variables that are set by various forms and functions.
Thanks again for all your help,
Andy
VulpesPosted Apr 19, 2012, 10:16 AM
If all the forms and classes in your project may need a reference to the same object, then you could expose it via by a global property. For example:
static class Global
{
internal static Class1 C1 {get; set;}
}
You can then set the global object using code like this:
Global.C1 = new Class1();
// set any properties of the object
and get it using this line:
Class1 c1 = Global.C1;
Because Global is a static class you can't (and don't want to) create an instance of it.
andy mcinnesPosted Apr 19, 2012, 9:53 AM
Some variables I make public in a class and values are stored in these variable and may be refered to from other forms or the same form for other processes.
So when I use:
Class1 c = new Class1(); I guess this means that all variables in the class will be blank as this is a new instance?
So how do I access a current instance of a class?
I tried the above format of Class1 c = (Class1) but then got stuck with what to do next as its not a form?
Any ideas,
Andy
andy mcinnesPosted Apr 18, 2012, 11:48 AM
I thought it was something stright forward!
Many thanks this worked a treat.
Andy
VulpesPosted Apr 18, 2012, 9:06 AM
One way is this:
// make sure you have this using statement near the top of the class file
using System.Windows.Forms;
You can then do:
Form1 frm1 = (Form1)Application.OpenForms["Form1"];
If ToolStripProgressBar1 is then public or internal rather than private (as controlled by the Modifiers property), you can then do:
frm1.ToolStripProgressBar1.Value = 100;
However, if you have a lot of controls to access, then you may benefit from this article which the theLizard wrote recently which shows how to do this in a generic way:
http://www.c-sharpcorner.com/UploadFile/theLizard/how-to-access-a-control-in-another-form/
andy mcinnesPosted Apr 18, 2012, 8:32 AM
Thanks for the reply I have tried the suggest without avail, perhaps its the way I am trying to reference it.
I am trying to access form controls from my other queries, as opposed to another form, and so the various functions reference the form controls values when and if they need to.
Here is an example of what I tried in response to your suggestions...
(In the form called acForm the control is called stConst which is the toolstriplabel.)
public static ToolStripStatusLabel vstConst;
public acForm()
{
InitializeComponent();
vstConst = stConst;
}
(In the class I query the label value (this void is called programitcally where needed from with the class library) This is a snippet the rest of the void is unrelated. )
public void acGet(bool gType, bool enq, bool bulk, bool mkEmail, bool mkNet)
{
acForm a = new acForm();
Password p = new Password();
susForm s = new susForm();
bool oem = false;
if (p.Visible)
{
p.BringToFront();
}
else
{
//if (a.textGet("daEid") != "Connected")
if (acForm.vstConst.Text != "Connected")
{
System.Windows.Forms.MessageBox.Show("There is no connection, therefore accounts cannot be queried or generated");
return;
}
}
}
I know there are lots of other ways to what I am doing here but this is just a theoretical example.
So, in this test what should be returned is "Connected" however it is bring the standard text that a new instance of the label would have, which is "Connecting...".
I guess its becuase I try to reference the control during the form initialisation, however there are a lot of controls on the form and I really what a way of accessing them that doesn't require me to reference them as part of any procedure.
In vb this was simple just: acForm.stConst.Text from anywhere but this is causing me some confusion in c#.
Typically I don't try to access a current instance of a form so have surficed with the = new Form() but in this case I need to keep accessing form controls.
Is there anyway that all the form controls can be made available for access from other forms/classes and rather than each control manually requiring a reference, to do it programitcally?
Thanks again for the help,
Andy
Gaurav GuptaPosted Apr 18, 2012, 7:58 AM
u are right ..
= new Form1();" ,,.. this will create a new instance of class
i think i can help u..
if i m not wrong u want to access the ToolStripProgressBar1 of one form into the other form..
i take a example of button.. u can use it in the ToolStripProgressBar1
suppose i have button in the first form and want to access it into other form
write this code:: into the first window form....
1.. make a reference variable of button class, mark it as public and static.
public static Button b;
2... then give the reference of button1 to the variable b...
and show the other form..
private void button1_Click(object sender, EventArgs e)
{
b = button1;
Form2 k = new Form2();
k.Show();
}
In the form2 u access the variable using class object name..
like this.. u are able to get the button of form1 into the form2
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show( Form1.b.Text);
}
thanks..
hope u will get ur answer....