PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 24, 2022

[FIXED] how do I bind the repeating codes shared by multiple forms in c#

 August 24, 2022     c#, dll, module, parent-child, repeat     No comments   

Issue

  • I have around 10+ child forms and all the child forms have the following features
  1. Close button: on hover displays "close" text and when mouse clicked, shows a message box to confirm the action
  2. Minimize button: On hover, it displays "minimize" text, when the mouse is clicked, the form gets minimized
  3. Drag anyway inside the parent form
  • I have to avoid using Visual Studio's default style to get a desirable customized form.

-The code below is shared by all the child forms, since the code is the same, is there a way to bind to one place and reference?

code

public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

 //Dtrag the form
        private void DragPanel(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
            }
        }

       #region controlbox
        private void minimizebtn_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }
        private void closebtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to Close the window?", "Close", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Close();
                //Application.Exit();
            }
            
        }
        
        private void closebtn_MouseHover(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(this.closebtn, "Close");
        }

        private void minimizebtn_MouseHover(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
            ToolTip1.SetToolTip(this.minimizebtn, "Minimize");
        }
        #endregion


Solution

You could create a base class which itself derives from Form. Your actual forms should then derive from that base class.



Answered By - Romout
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing