[. Net custom control] ComboBox control rewriting-comboboxex

Source: Internet
Author: User

ComboBox extension, added the read-only value attribute, and selectedbyvalue and selectedbytext two methods

Method or attribute Introduction
Text Original control Property
Value It is equivalent to selectedvalue in the ComboBox binding status or text in the non-binding status.
Selectedbyvalue (Object OBJ) The value is selected based on valuemember when binding status, and the value is selected based on selecteditem when not binding status. Exact search
Selectedbytext (Object OBJ) The options are displaymember-based and selecteditem-based. Exact search

 

Example:

Comboboxex1.text
Comboboxex1.value

Comboboxex1.selectedbyvalue ("2 ");
Comboboxex1.selectedbytext ("name3 ");

ControlsCode:

View plaincopy to clipboardprint?
  1. Using system;
  2. Using system. Windows. forms;
  3. // Web: http://www.yongfa365.com/
  4. Namespace yongfa365.controls. comboboxex
  5. {
  6. Public partial class comboboxex: ComboBox
  7. {
  8. Public comboboxex ()
  9. {
  10. This. dropdownstyle = comboboxstyle. dropdownlist;
  11. }
  12. Public void selectedbyvalue (Object OBJ)
  13. {
  14. If (this. datasource = NULL)
  15. {
  16. // When not bound
  17. This. selecteditem = OBJ;
  18. }
  19. Else
  20. {
  21. Object prevalue = This. selectedvalue;
  22. // Search directly when binding
  23. This. selectedvalue = OBJ;
  24. If (this. selectedvalue = NULL)
  25. {
  26. // Cannot be found to keep the original control value unchanged
  27. This. selectedvalue = prevalue;
  28. }
  29. }
  30. }
  31. Public void selectedbytext (Object OBJ)
  32. {
  33. If (this. datasource = NULL)
  34. {
  35. // When not bound
  36. This. selecteditem = OBJ;
  37. }
  38. Else
  39. {
  40. // When binding
  41. This. Text = obj. tostring ();
  42. }
  43. }
  44. Public String Value
  45. {
  46. Get
  47. {
  48. If (this. datasource = NULL)
  49. {
  50. // If it is not bound, text is returned.
  51. Return this. text;
  52. }
  53. Else
  54. {
  55. // Return selectedvalue when binding
  56. Return this. selectedvalue. tostring ();
  57. }
  58. }
  59. }
  60. }
  61. }
Using system; using system. windows. forms; // web: http://www.yongfa365.com/namespace yongfa365.controls. comboboxex {public partial class comboboxex: ComboBox {public comboboxex () {This. dropdownstyle = comboboxstyle. dropdownlist;} public void selectedbyvalue (Object OBJ) {If (this. datasource = NULL) {// This. selecteditem = OBJ;} else {object prevalue = This. selectedvalue; // you can directly find this when binding. selectedvalue = OBJ; If (this. selectedvalue = NULL) {// The original control value remains unchanged. selectedvalue = prevalue; }}} public void selectedbytext (Object OBJ) {If (this. datasource = NULL) {// This. selecteditem = OBJ;} else {// This. TEXT = obj. tostring () ;}} public string value {get {If (this. datasource = NULL) {// return text return this if it is not bound. text;} else {// return selectedvalue return this when binding. selectedvalue. tostring ();}}}}}

: Comboboxex.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.