Click a button from another Form

ELIRDFUL 185 Reputation points
2023-12-17T21:33:52.1266667+00:00

Hello everyone

I have a form called frmSaldoFactura, from the FormClosed event, of the form frmSaldoFactura, I have the following Code.

User's image

What I am trying to do: is that when I close the form frmSaldoFactura, click on the new button of the form FrmInvoice., but unfortunately the PerformClick() is not working nor does it assign focus.

 

I count on everyone's help.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} vote

2 answers

Sort by: Most helpful
  1. Damarcus Jones 20 Reputation points
    2023-12-17T21:40:35.8366667+00:00

    void MonthCal_HitTest(

    hmc,

    pinfousing System;

    using System.Diagnostics;

    using System.Runtime.InteropServices;

    using System.Windows.Forms;

    internal class CurrentMonthCalendar : MonthCalendar

    {

    public CurrentMonthCalendar() {
    
        var today = DateTime.Today;
    
        MinDate = new DateTime(today.Year, today.Month, 1);
    
        MaxDate = new DateTime(today.Year, today.Month,
    
                DateTime.DaysInMonth(today.Year, today.Month));
    
    }
    
    protected override void WndProc(ref Message m) {
    
        switch (m.Msg) {
    
            case WM_LBUTTONDOWN:
    
                if (!IsDisableMouse(ref m)) {
    
                    base.WndProc(ref m);
    
                }
    
                break;
    
            default:
    
                base.WndProc(ref m);
    
                break;
    
        }
    
    }
    
    private bool IsDisableMouse(ref Message m) {
    
        var mci = new MCHITTESTINFO();
    
        mci.pt_x = SignedLOWORD(m.LParam);
    
        mci.pt_y = SignedHIWORD(m.LParam);
    
        var result = (int)SendMessage(Handle, MCM_HITTEST, 0, mci);
    
        switch (result) {
    
            case MCHT_TITLE:
    
                return true;
    
        }
    
        return false;
    
    }
    
    const int WM_LBUTTONDOWN = 0x0201;
    
    const int MCM_FIRST = 0x1000;
    
    const int MCM_HITTEST = MCM_FIRST + 14;
    
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    
    class MCHITTESTINFO
    
    {
    
        public int cbSize = Marshal.SizeOf(typeof(MCHITTESTINFO));
    
        public int pt_x = 0;
    
        public int pt_y = 0;
    
        public int uHit = 0;
    
        public short st_wYear = 0;
    
        public short st_wMonth = 0;
    
        public short st_wDayOfWeek = 0;
    
        public short st_wDay = 0;
    
        public short st_wHour = 0;
    
        public short st_wMinute = 0;
    
        public short st_wSecond = 0;
    
        public short st_wMilliseconds = 0;
    
    }
    
    private const int
    
        MCHT_TITLE = 0x00010000,
    
        MCHT_CALENDAR = 0x00020000,
    
        MCHT_TODAYLINK = 0x00030000,
    
        MCHT_TITLEBK = (0x00010000),
    
        MCHT_TITLEMONTH = (0x00010000 | 0x0001),
    
        MCHT_TITLEYEAR = (0x00010000 | 0x0002),
    
        MCHT_TITLEBTNNEXT = (0x00010000 | 0x01000000 | 0x0003),
    
        MCHT_TITLEBTNPREV = (0x00010000 | 0x02000000 | 0x0003),
    
        MCHT_CALENDARBK = (0x00020000),
    
        MCHT_CALENDARDATE = (0x00020000 | 0x0001),
    
        MCHT_CALENDARDATENEXT = ((0x00020000 | 0x0001) | 0x01000000),
    
        MCHT_CALENDARDATEPREV = ((0x00020000 | 0x0001) | 0x02000000),
    
        MCHT_CALENDARDAY = (0x00020000 | 0x0002),
    
        MCHT_CALENDARWEEKNUM = (0x00020000 | 0x0003);
    
    [DllImport("User32", CharSet = CharSet.Auto)]
    
    private static extern IntPtr SendMessage(
    
           IntPtr hWnd, int msg, int wParam, MCHITTESTINFO lParam);
    
    private static int SignedHIWORD(IntPtr n) {
    
        return SignedHIWORD(unchecked((int)(long)n));
    
    }
    
    private static int SignedLOWORD(IntPtr n) {
    
        return SignedLOWORD(unchecked((int)(long)n));
    
    }
    
    private static int SignedHIWORD(int n) {
    
        int i = (short)((n >> 16) & 0xffff);
    
        return i;
    
    }
    
    private static int SignedLOWORD(int n) {
    
        int i = (short)(n & 0xFFFF);
    
        return i;
    
    }
    

    }

    );


  2. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2023-12-17T23:23:06.23+00:00

    I'm going to give you a conceptual example.

    Form1 creates an instance of Form2 where Form2 has an event which Form1 subscribes too. On FormClosed for Form2 invoke the event which in turn Form1 responds too. In Form1 we want to click Button2 but instead code that you might have in the button click event needs to be in a procedure which when actually clicking the button or invoking from the event both do the same thing.

    Guaranteed to work, all you need is to adapt to your code.

    Form1

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new();
            form2.ClickSomeButton += Form2_ClickSomeButton;
            form2.ShowDialog();
        }
    
        private void Form2_ClickSomeButton()
        {
            DoSomething();
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            DoSomething();
        }
    
        private void DoSomething()
        {
            MessageBox.Show("Dp some work");
        }
    }
    

    Form2

    public partial class Form2 : Form
    {
        public delegate void OnClickSomeButton();
        public event OnClickSomeButton ClickSomeButton;
        public Form2()
        {
            InitializeComponent();
            FormClosed += Form2_FormClosed;
        }
    
        private void Form2_FormClosed(object? sender, FormClosedEventArgs e)
        {
            ClickSomeButton?.Invoke();
        }
    }
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.