Hello:
I am trying to setup a string in a Class to set the value of a string in the calling Form.
Below is the code that I have but I get an error on the underlined. Is my syntax wrong or do I have to use a special 'using'?
Form FromForm = (Form)TheControlEvent.FindForm();
String FromString = (String)FromForm.Controls.Find("RunAs", true);
I will also need to do this for a DataTable:
DataTable FromDataTableRecord = (DataTable)FromForm.Controls.Find("DataTableRecord", true);
So: I am trying to get the value of a string called: "RunAs" from a form that I only have the name of as: Form FromForm = (Form)TheControlEvent.FindForm();
Loading
PJ MartinsPosted Jun 6, 2010, 12:40 PM
GustavoPosted Jun 7, 2010, 8:16 PM
I finally found the answer. You had me use a GetProperty but it shlould of been GetField.
RunAs = (String)ActiveForm.ActiveMdiChild.GetType().GetField("RunAs").GetValue(ActiveForm.ActiveMdiChild);
The credit goes to 'Sam' but need to close this issue.
GustavoPosted Jun 6, 2010, 12:47 PM
The "FormICEPack" is the MDIParent.
"FormGeneral" and "FormProduct" are the MDIChild
"ClassControl" is the Class where I have the Controls.
In "FormGeneral" the code:
public void FormGeneral_Enter(object sender, EventArgs e)
{
//FormICEPack.RunAs = RunAs;
//FormICEPack.dataGridViewRecord.DataSource = DataTableRecord;
//FormICEPack.dataGridViewRecord.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
Is what I am trying to move to the "ClassControl"
GustavoPosted Jun 6, 2010, 12:36 PM
Thanks for helping...
I will zip up the source and senbd it to you. Will send the MDIParent, the two MDIChild and the ClassControl.
Unless you just want me to send you the whole project... I can do that. But it will not run without the database.
PJ MartinsPosted Jun 6, 2010, 12:32 PM
GustavoPosted Jun 6, 2010, 11:49 AM
Here is the error in the enclosed document.
PJ MartinsPosted Jun 6, 2010, 11:39 AM
GustavoPosted Jun 5, 2010, 8:45 PM
Rats... did not work. It went to the 'Catch'.
PJ MartinsPosted Jun 5, 2010, 8:41 PM
RunAs = (String)TheParentForm.ActiveMdiChild.GetType().GetProperty("RunAs").GetValue(TheParentForm.ActiveMdiChild, null);
GustavoPosted Jun 5, 2010, 8:04 PM
The line:
RunAs = (String)oAssembly.GetType("ICEPack." + TheChildForm + "()").GetProperty("RunAs").GetValue(oForm, null);
Looked good when I first looked at it...But, it did not work.
I put it in a try/catch and at compile time it does not show any errors, but at run time there is.
Do you have any other suggestions?
I tried to break it down and show me what the values are. With: RunAs = (String)oAssembly.ToString(); It does give me something and does not blow up.
But with: RunAs = (String)oAssembly.GetType("ICEPack." + TheChildForm + "()").ToString(); It does blow up. It is what I am passing with the GetType(...)?
PJ MartinsPosted Jun 5, 2010, 7:22 PM
RunAs = (String)oAssembly.GetType("ICEPack." + TheChildForm + "()").GetProperty("RunAs").GetValue(oForm, null);
GustavoPosted Jun 4, 2010, 11:39 PM
Forgot to mention... I am a newbie. I have learned a lot in the past few months, but still don't know everything. After I have a working sample, I then get to know it well.
I have a MDIParent Form called ICEPack and many MDIChild Forms. For now I am working on the Forms called FormGeneral and FormProduct. When I get these working, then all the rest will be simple. I have a Class called ClassControl where I put all my Controls.
For example... The TextBox events: In ClassControl. This way all my Forms use the same code.
while (TheControlEvent != null)
{
TheEvent = TheControlEvent.GetType().Name;
switch (TheEvent)
{
#region TextBox
case "TextBox":
TheControlEvent.Enter += new EventHandler(TextBoxEnter);
TheControlEvent.Leave += new EventHandler(TextBoxLeave);
TheControlEvent.MouseHover += new EventHandler(TextBoxMouseHover);
TheControlEvent.MouseLeave += new EventHandler(TextBoxMouseLeave);
break;
#endregion
... other code
Before the 'while' I add the following:
TheForm.Enter += new EventHandler(FormEnter);
So there will be only one routine for all Form Enter.
Currently in the MDIChild I have:
public void FormGeneral_Enter(object sender, EventArgs e)
{
FormICEPack.RunAs = RunAs;
FormICEPack.dataGridViewRecord.DataSource = DataTableRecord;
FormICEPack.dataGridViewRecord.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}
And would like to put this in the ClassControl:
private void FormEnter(object sender, EventArgs e)
{
// Original code from FormGeneral & FormProduct
//FormICEPack.RunAs = RunAs;
//FormICEPack.dataGridViewRecord.DataSource = DataTableRecord;
//FormICEPack.dataGridViewRecord.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
// New code???
//...
Form TheParentForm = Form.ActiveForm; // Get The Parent Form
TheChildForm = TheParentForm.ActiveMdiChild.Name.ToString(); // Get The Child Form
Assembly oAssembly = Assembly.GetEntryAssembly();
Form oForm = (Form)oAssembly.CreateInstance("ICEPack." + TheChildForm + "()");
RunAs = (String)oForm.Controls.Find("RunAs", true).ToString(); // ERROR Does not give value
}
PJ MartinsPosted Jun 4, 2010, 11:06 PM
public class TheParentForm
{
private string RunAs;
public string GetRunAs()
{
return RunAs;
}
}
Now you can do: string FindString = FindForm.GetRunAs();
GustavoPosted Jun 4, 2010, 9:32 PM
This is the code I have now. I still get the error on the line underlined.
Form TheParentForm = Form.ActiveForm; // Get The Parent Form
string TheChildForm = TheParentForm.ActiveMdiChild.Name.ToString(); // Get The Child Form
Assembly oAssembly = Assembly.GetEntryAssembly();
Form oTheForm = (Form)oAssembly.CreateInstance("ICEPack." + TheChildForm); // Assembly for the form to get stuff
string TheRunAs = oTheForm.Controls.Find("RunAs", true); // Get the string variable RunAs value
Can you tell me what I have wrong?
PJ MartinsPosted Jun 4, 2010, 6:47 PM