Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of methods?

Status
Not open for further replies.

myuserid7

MIS
Mar 2, 2005
111
0
0
ES
I have a program which creates an array of Picture Boxes. I need to assign each PictureBox it's own DoubleClick method but I'm not sure how to do it without writing the same method endless times. Is there a way to replace "pictureBox1_DoubleClick" with a new method?


pBXR.DoubleClick += new System.EventHandler(pictureBox1_DoubleClick);
 
Loop throught the collection.
Use the foreach {}.
 
Perhaps I should explain it better. I am trying to make a program which loads a configuration file and creates an array of pictureBoxes. Each Individual pictureBox is suppose to have it's own Icon and have it's own unique action.

How would I go about giving each PictureBox it's own draw and click method? Right now, I have an array of 10 but they all have the same draw method and action.

private void pictureBX_DoubleClick(object sender, EventArgs e)
{
//Actions go here

}

All the picture boxes have that method. Is there a way to tell which box was clicked?
 
The sender argument will tell you which specific picturebox was clicked.


Hope this helps.

[vampire][bat]
 
Do control arrays exist in C#? Well, in vb6 they were, but do not exist in VB.NET. I dont know.

As Rick said, the sender object (casted to a picturebox) will tell you who was the caller of the event. To do so, you need to add the same event handler name to all pictureboxes.
So you will either write i-times "pBXR.DoubleClick += new System.EventHandler(pictureBox1_DoubleClick);" where i=0, 1, 2 ..etc , or have a loop for it.
 
Just to make something clear, because it looks like wrong. The sender should be casted to a picturebox if you want to access its properties, e.g change the image.
 
pictureBX_DoubleClick(object sender, EventArgs e)

There is no reason for them to have separate event handlers if the code that gets executed for each is identical. You can point them all to the same method.

Code:
pBXR[i].DoubleClick += new System.EventHandler(myPicBoxDoubleClickHandler);

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Chip is right - you are either drawing or clicking on AN image of some sort. What that image is should be up to the picturebox.

 
Hello all.
Let me explain my solution:
I have similar problem: I want to validate each text box by their own function. So I use inheritance and encapsulation:
First, I create a class which inherits System.Windows.Forms.TextBox. Into this class I override (in my case) "OnValidating" event. Also, this class has one private member which is a generalized type of class.
So, beside this newTextBox class I created already mentioned generalized class which has an abstract method Check. From this class I created so many classes as I need to fulfill all different ways of TextBox checking.
Back to newTextBox class: overridable OnValidating event always call generalized base class method check which return true or false and therefore my validation permit to leave the text box or not.

To implement these classes into my code I do not need additional coding: the registering events and handling events remain the same. I just need to change original windows control with my - new control type and set proper validating object in Form_Laod method.

My example of code:
// Improved control:
class TxtCtrl : System.Windows.Forms.TextBox {
private ValidatingData m_Vdata; // base class of Validation object

public ValidatingData ValidateDataObj { // to set proper validation object
set {
m_Vdata = value;
}
}
protected override void OnValidating (System.ComponentModel.CancelEventArgs e) {
e.Cancel = m_Vdata.Check(this.Text); //managing Validating event
}
}

// base class:
abstract class ValidatingData {
public abstract bool Check(string a_value);
}

// Validating text box if .text is empty
class ValidNotNull : ValidatingData {
public override bool Check(string a_value) {
if (a_value == "") {
return true;
}
else {
return false;
}
}
}

// MainForm.cs
private void MainForm_Load(object sender, EventArgs e) {
...
txt_NewTypeControl.ValidateDataObj = new ValidNotNull();
...
}
// MainForm.Designer.cs changing type of control
//this.txt_NewTypeControl= new System.Windows.Forms.TextBox();
this.txt_NewTypeControl= new TxtCtrl();


Best regards
Andreo
 
Wait for it....

Why is this so complicated? You're only checking a string right? Sounds like a great time for Regular Expressions. You can associate a regular expression with a particular textbox if you really want via a hashtable.

Use your Check() method with the appropriate regex.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top