TextBoxWithTextArea.cs 3.3 KB

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