TextBoxWithTextArea.cs 3.4 KB

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