Hello:
I have tried for two days now. I still can not get it to work. I have the following code that I have in every form. I would like to put it in one place and include it in all my forms. Like the C+ with the #include. Can someone give me a sample of how I can do it?
public FormICEPack()
{
InitializeComponent();
Control TheControl = GetNextControl(null, true);
while (TheControl != null)
{
if (TheControl.GetType().Name == "TextBox")
{
TheControl.Enter += new EventHandler(Object_Enter);
TheControl.Leave += new EventHandler(Object_Leave);
//TheControl.Click += new EventHandler(Object_Click);
TheControl.MouseHover += new EventHandler(textBox_MouseHover);
TheControl.MouseLeave += new EventHandler(textBox_MouseLeave);
}
TheControl = GetNextControl(TheControl, true);
}
}
Loading
Jaish MathewsPosted Apr 1, 2010, 3:06 PM
Your 1st aproach is the correct one. So you got an error. But not mentioned what's the error. Any way I copied your code and got error like "GetNextControl()" not there. Thta's fine only. why???
"GetNextControl" is define in side "System.Windiw.Form:. So one your are in side a page, u can directly call it.But in a customized class, u need to pass the form to the class like below.
//I am calling this from a From. So passed "this". Otherwise u need to pass the corresponding form's object
ClassUser cls = new ClassUser(this, "Hey...");
Now u can access GetNextControl(), like below
public ClassUser(Form frm, string DOIT)
{
MessageBox.Show("IN: ClassUser: " + DOIT);
Control TheControl = frm.GetNextControl(null, true);
while (TheControl != null)
{
if (TheControl.GetType().Name == "TextBox")
{
TheControl.Enter += new EventHandler(Object_Enter);
}
TheControl = frm.GetNextControl(TheControl, true);
}
}
GustavoPosted Apr 1, 2010, 8:42 PM
I tried the userControl, but could not get it to work. I am sure it was something that I was missing.
I am going to ge this working this way. Then I will re-try the UserControl again. I finally have moved the code into the class, but now I have 1 bug left...
Sam HobbsPosted Apr 1, 2010, 8:37 PM
Also, you said you were going to use a UserControl. When you do, you will not have this problem. Yes, deriving from a form is a good solution, but I did not say much about that since you sais you are using a UserControl.
Finally, C++ programmers would not use an #include in the manner you show. C programmers would use a macro, which is close to what you are showing. C++ programmers are less likely to use macros, especially the designer of the C++ language. Macros don't even exist in C#; the #define statement is used to define macros and for C# the define statement is extremely limited in it's use in C#. Good C++ prgrammers and all C# programmers would do something different, such as creating a class or deriving from a class.
theLizardPosted Apr 1, 2010, 4:13 PM
public ClassUser(Form frm, string DOIT, string ctrlType)
{
then
if (TheControl.GetType().Name == "TextBox")
if (TheControl.GetType().Name == ctrlType)
then the function i snot restricted to TextBoxes, It should work...
GustavoPosted Apr 1, 2010, 3:28 PM
Interesting... I will read up on it. Sounds a lot cleaner that what I was thinking of doing.
Jaish MathewsPosted Apr 1, 2010, 3:26 PM
Forgot to suggest a better design like below.
//Make custom base class inherited from builtin "Form". All pages can derive from this base class.
//Now you can directly call GetNextControl(), as now you have access to a "Form" class.
public class ClassUser:Form
{
}
//Page inherited from custom base class defined above.
class FormICEPack:ClassUser
{
}
GustavoPosted Apr 1, 2010, 3:21 PM
I did a quick test and I think it will work. Thank you. Now I have something to play with to make it work.
Thanks
GustavoPosted Apr 1, 2010, 2:06 PM
Example: In the main program you add:
#include "MenuExit.txt";
Then the: MenuExit.txt
Will have something like this:
// ShowMessage("Action Exit - (Generic)");
ShowMessage("You really want to close... Data verification...");
Close();
So, whereever I put: #include "MenuExit.txt"; the above code will be included.
GustavoPosted Apr 1, 2010, 2:05 PM
I tried that before and I retied it again. Its not working for me. I get the error on the lines with the: GetNextControl.
This is the code of the class. I took a lot out of it to make it simple and then work from there.
namespace ICEPack
{
#region Notes
//
// To Do:
// 1) Remove static. - DONE
// 2) Make Private.
// 3)
// 4)
// 5)
#endregion
//
public class ClassUser
{
//#region Variables
//
//
public ClassUser(string DOIT)
{
MessageBox.Show("IN: ClassUser: " + DOIT);
Control TheControl = GetNextControl(null, true);
while (TheControl != null)
{
if (TheControl.GetType().Name == "TextBox")
{
TheControl.Enter += new EventHandler(Object_Enter);
}
TheControl = GetNextControl(TheControl, true);
}
ClassUser ClassUser = new ClassUser("Hey..." + DOIT);
}
public void Object_Enter(object sender, EventArgs e)
{
((TextBox)sender).BackColor = Color.Red;
}
}
}
In my main program, this is how I am trying to call it:
public FormICEPack()
{
InitializeComponent();
//Control TheControl = GetNextControl(null, true);
//while (TheControl != null)
//{
// if (TheControl.GetType().Name == "TextBox")
// {
// TheControl.Enter += new EventHandler(Object_Enter);
// //TheControl.Leave += new EventHandler(Object_Leave);
// //TheControl.Click += new EventHandler(Object_Click);
// //TheControl.MouseHover += new EventHandler(textBox_MouseHover);
// //TheControl.MouseLeave += new EventHandler(textBox_MouseLeave);
// }
// TheControl = GetNextControl(TheControl, true);
//}
ClassUser ClassUser = new ClassUser("Hey...");
}
Jaish MathewsPosted Apr 1, 2010, 12:44 PM
I didn't tried with smart device. But in WebForms normally creating a BasePage and will define common things inside it, like below
class WebForm1: BasePage
{
//Page specific things + can call common items inside BasePage
}
class WebForm2 : BasePage
{
//Page specific things + can call common items inside BasePage
}
class BasePage :Page
{
//Common things
}