TextBoxWithTextArea.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace UAS_MES.CustomControl.TextBoxWithIcon
  5. {
  6. public partial class TextBoxWithTextArea : UserControl
  7. {
  8. public TextBoxWithTextArea()
  9. {
  10. InitializeComponent();
  11. }
  12. //重写Text方法,用于接收或者传递值
  13. public override string Text
  14. {
  15. get
  16. {
  17. return textBox1.Text;
  18. }
  19. set
  20. {
  21. textBox1.Text = value;
  22. }
  23. }
  24. private void TextAreaIcon_Click(object sender, EventArgs e)
  25. {
  26. //获取相对屏幕左上角的位置的坐标,传给TextAreaForm作为初始化的坐标
  27. var screenPoint = PointToScreen(TextAreaIcon.Location);
  28. bool GetParent = true;
  29. TextAreaForm taf = new TextAreaForm();
  30. //需要传递当前的Form名称,控件名称,和相对屏幕的位置
  31. string FormName = this.FindForm().Name;
  32. int i = 0;
  33. //当前的this是控件的Icon
  34. Control c=this;
  35. //一直通过父级往上找,直到父级的名称和Form的名称相等,记录经过了多少级
  36. while (GetParent)
  37. {
  38. c= c.Parent;
  39. if (c.Name == FormName)
  40. {
  41. GetParent = false;
  42. }
  43. i++;
  44. }
  45. //指定一个数据用来存放查找过的Control的Name
  46. string[] ControlsName=new string[i];
  47. //指定Form的名称
  48. taf.FormName = FormName;
  49. //重置Icon
  50. c = this;
  51. //将控件的名称添加到字符串数组中
  52. for (int j = 0; j < i; j++) {
  53. if (c.Name != FormName)
  54. {
  55. ControlsName[j] = c.Name;
  56. }
  57. c = c.Parent;
  58. }
  59. taf.ControlName = ControlsName;
  60. taf.X = screenPoint.X-this.Width;
  61. taf.Y= screenPoint.Y-this.Height*5;
  62. taf.ShowDialog();
  63. }
  64. private void textBox1_Leave(object sender, EventArgs e)
  65. {
  66. textBox1.BackColor = Color.White;
  67. }
  68. private void textBox1_Enter(object sender, EventArgs e)
  69. {
  70. textBox1.BackColor = Color.GreenYellow;
  71. }
  72. }
  73. }