TextBoxWithTextArea.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 TextAreaTextBox.Text;
  18. }
  19. set
  20. {
  21. TextAreaTextBox.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. {
  54. if (c.Name != FormName)
  55. {
  56. ControlsName[j] = c.Name;
  57. }
  58. c = c.Parent;
  59. }
  60. taf.ControlName = ControlsName;
  61. taf.X = screenPoint.X / 2;
  62. taf.Y = screenPoint.Y / 2;
  63. taf.ShowDialog();
  64. }
  65. private void textBox1_Leave(object sender, EventArgs e)
  66. {
  67. TextAreaTextBox.BackColor = Color.White;
  68. }
  69. private void textBox1_Enter(object sender, EventArgs e)
  70. {
  71. TextAreaTextBox.BackColor = Color.GreenYellow;
  72. }
  73. private void TextBoxWithTextArea_SizeChanged(object sender, EventArgs e)
  74. {
  75. TextAreaTextBox.Width = this.Width - TextAreaIcon.Width - 3;
  76. }
  77. }
  78. }