Click here to Skip to main content
15,867,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The system will display one image on PictureBox from the Upload folder(contains multiple images).
After the user clicks on the fail button in Form4 then it will show the Form6 to let the user choose a list of defect categories then click on the `BtnAdd_Click()`in Form6.

When the user clicks on `BtnAdd_Click()` in Form6 then the image displays in the PictureBox in Form4 will move to the next image `incrementImage()`.

I want to check whether the `BtnAdd_Click()` was clicked in Form6 and pass to Form4 to perform the `incrementImage()` function. May I know how to do this thanks.

Form4.cs
C#
public void incrementImage()
        {

            nCurrentItem++;
            firstImage++;

            if (nCurrentItem > nTotalNumber)
                nCurrentItem = nTotalNumber;

            else if (nCurrentItem < nTotalNumber)
            {
                Image img;
                using (var bmpTemp = new Bitmap(ImageFilenames[nCurrentItem]))
                {
                    img = new Bitmap(bmpTemp);
                }
                PicBox.Image = img;

            }

            imgCounter.Text = firstImage.ToString() + " / " + nTotalNumber.ToString();
        }


Form6.cs

C#
private void BtnAdd_Click(object sender, EventArgs e)
            {
                if (listBoxFailCategories.SelectedIndex == -1)
                {
                    MessageBox.Show("Please select any defect category");
                }
                else
                {
                    var tempDefectCategory = "";
    
                foreach (string item in listBoxFailCategories.SelectedItems)
                {
                    string defectFolder = Path.Combine(Value, item);
                    File.Copy(Image, Path.Combine(defectFolder, Path.GetFileName(Image)), true);

                    added = true;

                    tempDefectCategory = tempDefectCategory + item.ToString() + ",";

                    FullPathName = Path.Combine(defectFolder, Path.GetFileName(Image));

                    this.Close();
                }
                DefectCategory = tempDefectCategory;
                File.Delete(Image);
            }
        }


What I have tried:

I have tried to pass a string text from the `BtnAdd_Click()` in Form6 to Form4.

When Form4 received a text from Form6 then will perform the `incrementImage()` function but still didn't work.
Posted
Updated 13-Oct-21 2:56am
Comments
PIEBALDconsult 13-Oct-21 11:12am    
No, don't do that. Neither form should know the other exists. Both forms should interact with a separate class which handles the details.

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
Assuming the instance of Form4 has a valid reference to the instance of Form6:
C#
// 1) in Form6 create a public Action method (a delegate):

public Action DefectButtonPressed;

// 1a) in the Form6 BtnAdd_Click():

private void `BtnAdd_Click(object sender, EventArgs e)
{
    if (DefectButtonPressed != null) DefectButtonPressed();
}

// 2) in Form4 subscribe to the delegate (Action)

instanceOfForm6.DefectButtonPressed += OnDefectButtonPressed;

// 2a) handle the event

private void OnDefectButtonPressed()
{
   // your code to handle the event goes here
}
If both Form4 and Form6 are created by some other Form, do the right thing so Form4 has a reference to Form6.

This type of "code injection" keeps the dependency between the two Forms minimal; there will be no error thrown if the Action is not defined when the button on Form6 is clicked.

'Action is a Delegate; it can have multiple subscribers: [^]
 
Share this answer
 
v2
Comments
Vektor 2021 13-Oct-21 9:08am    
Form4 already created by another form so how to get the reference to Form6
BillWoodruff 13-Oct-21 9:33am    
Form4 and Form6 are created by different Forms ? Which/what is the Main Form ?
Vektor 2021 13-Oct-21 10:00am    
I followed yours and added but can't work.


Form1.cs

Form6 fm6; //added new
public Form4()
{
InitializeComponent();

fm6 = new Form6(); //added new
fm6.DefectButtonPressed += OnDefectButtonPressed; //added new

}

private void OnDefectButtonPressed() //added new
{
incrementImage();
}

Form6.cs

public Action DefectButtonPressed; //added new

private void BtnAdd_Click(object sender, EventArgs e)
{
if (listBoxFailCategories.SelectedIndex == -1)
{
MessageBox.Show("Please select any defect category");
}
else
{
var tempDefectCategory = "";

foreach (string item in listBoxFailCategories.SelectedItems)
{
string defectFolder = Path.Combine(Value, item);
File.Copy(Image, Path.Combine(defectFolder, Path.GetFileName(Image)), true);

added = true;

tempDefectCategory = tempDefectCategory + item.ToString() + ",";

FullPathName = Path.Combine(defectFolder, Path.GetFileName(Image));

}
DefectCategory = tempDefectCategory;
File.Delete(Image);
if (DefectButtonPressed != null) DefectButtonPressed(); //added new
this.Close();
}
}
BillWoodruff 13-Oct-21 10:34am    
The code you show should trigger the OnDefectButtonPressed method in Form1 when the Button on Form6 is pressed. Test that by putting breakpoinys in the OnDefectButtonPressed method in Form1, and in the button click event in Form6.

I use this technique all the time.

I note there is no mention of Form4 in this code.
Vektor 2021 13-Oct-21 10:44am    
How should trigger the OnDefectButtonPressed method in Form 4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900