IdDsnPropEdBindingNET.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. unit IdDsnPropEdBindingNET;
  2. interface
  3. uses
  4. System.Drawing, System.Collections, System.ComponentModel,
  5. System.Windows.Forms, System.Data, IdObjs, IdSocketHandle;
  6. type
  7. TIdDsnPropEdBindingNET = class(System.Windows.Forms.Form)
  8. {$REGION 'Designer Managed Code'}
  9. strict private
  10. /// <summary>
  11. /// Required designer variable.
  12. /// </summary>
  13. Components: System.ComponentModel.Container;
  14. btnOk: System.Windows.Forms.Button;
  15. btnCancel: System.Windows.Forms.Button;
  16. lblBindings: System.Windows.Forms.Label;
  17. lbBindings: System.Windows.Forms.ListBox;
  18. btnNew: System.Windows.Forms.Button;
  19. btnDelete: System.Windows.Forms.Button;
  20. lblIPAddress: System.Windows.Forms.Label;
  21. edtIPAddress: System.Windows.Forms.ComboBox;
  22. lblPort: System.Windows.Forms.Label;
  23. edtPort: System.Windows.Forms.NumericUpDown;
  24. cboIPVersion: System.Windows.Forms.ComboBox;
  25. lblIPVersion: System.Windows.Forms.Label;
  26. /// <summary>
  27. /// Required method for Designer support - do not modify
  28. /// the contents of this method with the code editor.
  29. /// </summary>
  30. procedure InitializeComponent;
  31. procedure btnNew_Click(sender: System.Object; e: System.EventArgs);
  32. procedure btnDelete_Click(sender: System.Object; e: System.EventArgs);
  33. procedure edtPort_ValueChanged(sender: System.Object; e: System.EventArgs);
  34. procedure edtIPAddress_SelectedValueChanged(sender: System.Object; e: System.EventArgs);
  35. procedure cboIPVersion_SelectedValueChanged(sender: System.Object; e: System.EventArgs);
  36. procedure lbBindings_SelectedValueChanged(sender: System.Object; e: System.EventArgs);
  37. {$ENDREGION}
  38. strict protected
  39. /// <summary>
  40. /// Clean up any resources being used.
  41. /// </summary>
  42. procedure Dispose(Disposing: Boolean); override;
  43. private
  44. FHandles : TIdSocketHandles;
  45. FDefaultPort : Integer;
  46. FIPv4Addresses : TIdStrings;
  47. FIPv6Addresses : TIdStrings;
  48. FCurrentHandle : TIdSocketHandle;
  49. { Private Declarations }
  50. procedure SetHandles(const Value: TIdSocketHandles);
  51. procedure SetIPv4Addresses(const Value: TIdStrings);
  52. procedure SetIPv6Addresses(const Value: TIdStrings);
  53. procedure UpdateBindingList;
  54. procedure UpdateEditControls;
  55. procedure FillComboBox(ACombo : System.Windows.Forms.ComboBox; AStrings :TIdStrings);
  56. public
  57. constructor Create;
  58. function Execute : Boolean;
  59. function GetList: string;
  60. procedure SetList(const AList: string);
  61. property Handles : TIdSocketHandles read FHandles write SetHandles;
  62. property DefaultPort : Integer read FDefaultPort write FDefaultPort;
  63. property IPv4Addresses : TIdStrings read FIPv4Addresses write SetIPv4Addresses;
  64. property IPv6Addresses : TIdStrings read FIPv6Addresses write SetIPv6Addresses;
  65. end;
  66. [assembly: RuntimeRequiredAttribute(TypeOf(TIdDsnPropEdBindingNET))]
  67. procedure FillHandleList(const AList: string; ADest: TIdSocketHandles);
  68. function GetListValues(const ASocketHandles : TIdSocketHandles) : String;
  69. implementation
  70. uses
  71. IdGlobal,
  72. IdIPAddress,
  73. IdDsnCoreResourceStrings, IdStack, IdSys;
  74. const
  75. IPv6Wildcard1 = '::'; {do not localize}
  76. IPv6Wildcard2 = '0:0:0:0:0:0:0:0'; {do not localize}
  77. IPv6Loopback = '::1'; {do not localize}
  78. IPv4Wildcard = '0.0.0.0'; {do not localize}
  79. IPv4Loopback = '127.0.0.1'; {do not localize}
  80. function IsValidIP(const AAddr : String): Boolean;
  81. var LIP : TIdIPAddress;
  82. begin
  83. LIP := TIdIPAddress.MakeAddressObject(AAddr);
  84. Result := Assigned(LIP);
  85. if Result then
  86. begin
  87. Sys.FreeAndNil(LIP);
  88. end;
  89. end;
  90. function StripAndSymbol(s : String) : String;
  91. begin
  92. Result := '';
  93. repeat
  94. if s='' then
  95. begin
  96. Break;
  97. end;
  98. Result := Result + Fetch(s,'&');
  99. until False;
  100. end;
  101. procedure FillHandleList(const AList: string; ADest: TIdSocketHandles);
  102. var
  103. LItems: TIdStringList;
  104. i: integer;
  105. LIPVersion: TIdIPVersion;
  106. LAddr, LText: string;
  107. LPort: integer;
  108. begin
  109. ADest.Clear;
  110. LItems := TIdStringList.Create;
  111. try
  112. LItems.CommaText := AList;
  113. for i := 0 to LItems.Count-1 do begin
  114. if Length(LItems[i]) > 0 then begin
  115. if TextStartsWith(LItems[i], '[') then begin
  116. // ipv6
  117. LIPVersion := Id_IPv6;
  118. LText := Copy(LItems[i], 2, MaxInt);
  119. LAddr := Fetch(LText, ']:');
  120. LPort := Sys.StrToInt(LText, -1);
  121. end else begin
  122. // ipv4
  123. LIPVersion := Id_IPv4;
  124. LText := LItems[i];
  125. LAddr := Fetch(LText, ':');
  126. LPort := Sys.StrToInt(LText, -1);
  127. //Note that 0 is legal and indicates the server binds to a random port
  128. end;
  129. if IsValidIP(LAddr) and (LPort > -1) and (LPort < 65536) then begin
  130. with ADest.Add do begin
  131. IPVersion := LIPVersion;
  132. IP := LAddr;
  133. Port := LPort;
  134. end;
  135. end;
  136. end;
  137. end;
  138. finally
  139. LItems.Free;
  140. end;
  141. end;
  142. function NumericOnly(const AText : String) : String;
  143. var i : Integer;
  144. begin
  145. Result := '';
  146. for i := 1 to Length(AText) do
  147. begin
  148. if IsNumeric(AText[i]) then
  149. begin
  150. Result := Result + AText[i];
  151. end
  152. else
  153. begin
  154. Break;
  155. end;
  156. end;
  157. if (Length(Result) = 0) then
  158. begin
  159. Result := '0';
  160. end;
  161. end;
  162. function IndexOfNo(const ANo : Integer; AItems : System.Windows.Forms.ComboBox.ObjectCollection) : Integer;
  163. begin
  164. for Result := 0 to AItems.Count -1 do
  165. begin
  166. if ANo = Sys.StrToInt( NumericOnly(AItems[Result].ToString )) then
  167. begin
  168. Exit;
  169. end;
  170. end;
  171. Result := -1;
  172. end;
  173. function GetDisplayString(const AIP : String; const APort : Integer; AIPVer : TIdIPVersion): string;
  174. begin
  175. Result := '';
  176. case AIPVer of
  177. Id_IPv4 : Result := Sys.Format('%s:%d',[AIP,APort]);
  178. Id_IPv6 : Result := Sys.Format('[%s]:%d',[AIP,APort]);
  179. end;
  180. end;
  181. function GetListValues(const ASocketHandles : TIdSocketHandles) : String;
  182. var i : Integer;
  183. begin
  184. Result := '';
  185. for i := 0 to ASocketHandles.Count -1 do begin
  186. Result := Result + ',' + GetDisplayString(ASocketHandles[i].IP,ASocketHandles[i].Port,ASocketHandles[i].IPVersion );
  187. end;
  188. Delete(Result,1,1);
  189. end;
  190. {$AUTOBOX ON}
  191. {$REGION 'Windows Form Designer generated code'}
  192. /// <summary>
  193. /// Required method for Designer support -- do not modify
  194. /// the contents of this method with the code editor.
  195. /// </summary>
  196. procedure TIdDsnPropEdBindingNET.InitializeComponent;
  197. type
  198. TArrayOfInteger = array of Integer;
  199. begin
  200. Self.btnOk := System.Windows.Forms.Button.Create;
  201. Self.btnCancel := System.Windows.Forms.Button.Create;
  202. Self.lblBindings := System.Windows.Forms.Label.Create;
  203. Self.lbBindings := System.Windows.Forms.ListBox.Create;
  204. Self.btnNew := System.Windows.Forms.Button.Create;
  205. Self.btnDelete := System.Windows.Forms.Button.Create;
  206. Self.lblIPAddress := System.Windows.Forms.Label.Create;
  207. Self.edtIPAddress := System.Windows.Forms.ComboBox.Create;
  208. Self.lblPort := System.Windows.Forms.Label.Create;
  209. Self.edtPort := System.Windows.Forms.NumericUpDown.Create;
  210. Self.cboIPVersion := System.Windows.Forms.ComboBox.Create;
  211. Self.lblIPVersion := System.Windows.Forms.Label.Create;
  212. (System.ComponentModel.ISupportInitialize(Self.edtPort)).BeginInit;
  213. Self.SuspendLayout;
  214. //
  215. // btnOk
  216. //
  217. Self.btnOk.Anchor := (System.Windows.Forms.AnchorStyles((System.Windows.Forms.AnchorStyles.Bottom
  218. or System.Windows.Forms.AnchorStyles.Right)));
  219. Self.btnOk.DialogResult := System.Windows.Forms.DialogResult.OK;
  220. Self.btnOk.Location := System.Drawing.Point.Create(312, 160);
  221. Self.btnOk.Name := 'btnOk';
  222. Self.btnOk.TabIndex := 0;
  223. //
  224. // btnCancel
  225. //
  226. Self.btnCancel.Anchor := (System.Windows.Forms.AnchorStyles((System.Windows.Forms.AnchorStyles.Bottom
  227. or System.Windows.Forms.AnchorStyles.Right)));
  228. Self.btnCancel.DialogResult := System.Windows.Forms.DialogResult.Cancel;
  229. Self.btnCancel.Location := System.Drawing.Point.Create(392, 160);
  230. Self.btnCancel.Name := 'btnCancel';
  231. Self.btnCancel.TabIndex := 1;
  232. //
  233. // lblBindings
  234. //
  235. Self.lblBindings.AutoSize := True;
  236. Self.lblBindings.Location := System.Drawing.Point.Create(8, 8);
  237. Self.lblBindings.Name := 'lblBindings';
  238. Self.lblBindings.Size := System.Drawing.Size.Create(42, 16);
  239. Self.lblBindings.TabIndex := 2;
  240. Self.lblBindings.Text := '&Binding';
  241. //
  242. // lbBindings
  243. //
  244. Self.lbBindings.Anchor := (System.Windows.Forms.AnchorStyles(((System.Windows.Forms.AnchorStyles.Top
  245. or System.Windows.Forms.AnchorStyles.Bottom) or System.Windows.Forms.AnchorStyles.Left)));
  246. Self.lbBindings.Location := System.Drawing.Point.Create(8, 24);
  247. Self.lbBindings.Name := 'lbBindings';
  248. Self.lbBindings.Size := System.Drawing.Size.Create(137, 121);
  249. Self.lbBindings.TabIndex := 3;
  250. Include(Self.lbBindings.SelectedValueChanged, Self.lbBindings_SelectedValueChanged);
  251. //
  252. // btnNew
  253. //
  254. Self.btnNew.Location := System.Drawing.Point.Create(152, 56);
  255. Self.btnNew.Name := 'btnNew';
  256. Self.btnNew.TabIndex := 4;
  257. Include(Self.btnNew.Click, Self.btnNew_Click);
  258. //
  259. // btnDelete
  260. //
  261. Self.btnDelete.Location := System.Drawing.Point.Create(152, 88);
  262. Self.btnDelete.Name := 'btnDelete';
  263. Self.btnDelete.TabIndex := 5;
  264. Include(Self.btnDelete.Click, Self.btnDelete_Click);
  265. //
  266. // lblIPAddress
  267. //
  268. Self.lblIPAddress.Location := System.Drawing.Point.Create(240, 8);
  269. Self.lblIPAddress.Name := 'lblIPAddress';
  270. Self.lblIPAddress.Size := System.Drawing.Size.Create(100, 16);
  271. Self.lblIPAddress.TabIndex := 6;
  272. Self.lblIPAddress.Text := 'Label1';
  273. //
  274. // edtIPAddress
  275. //
  276. Self.edtIPAddress.Anchor := (System.Windows.Forms.AnchorStyles(((System.Windows.Forms.AnchorStyles.Top
  277. or System.Windows.Forms.AnchorStyles.Left) or System.Windows.Forms.AnchorStyles.Right)));
  278. Self.edtIPAddress.DropDownStyle := System.Windows.Forms.ComboBoxStyle.DropDownList;
  279. Self.edtIPAddress.Location := System.Drawing.Point.Create(240, 24);
  280. Self.edtIPAddress.Name := 'edtIPAddress';
  281. Self.edtIPAddress.Size := System.Drawing.Size.Create(224, 21);
  282. Self.edtIPAddress.TabIndex := 7;
  283. Include(Self.edtIPAddress.SelectedValueChanged, Self.edtIPAddress_SelectedValueChanged);
  284. //
  285. // lblPort
  286. //
  287. Self.lblPort.Location := System.Drawing.Point.Create(240, 58);
  288. Self.lblPort.Name := 'lblPort';
  289. Self.lblPort.Size := System.Drawing.Size.Create(100, 16);
  290. Self.lblPort.TabIndex := 8;
  291. Self.lblPort.Text := 'Label1';
  292. //
  293. // edtPort
  294. //
  295. Self.edtPort.Anchor := (System.Windows.Forms.AnchorStyles(((System.Windows.Forms.AnchorStyles.Top
  296. or System.Windows.Forms.AnchorStyles.Left) or System.Windows.Forms.AnchorStyles.Right)));
  297. Self.edtPort.Location := System.Drawing.Point.Create(240, 74);
  298. Self.edtPort.Maximum := System.Decimal.Create(TArrayOfInteger.Create(65535,
  299. 0, 0, 0));
  300. Self.edtPort.Name := 'edtPort';
  301. Self.edtPort.Size := System.Drawing.Size.Create(224, 20);
  302. Self.edtPort.TabIndex := 9;
  303. Include(Self.edtPort.ValueChanged, Self.edtPort_ValueChanged);
  304. //
  305. // cboIPVersion
  306. //
  307. Self.cboIPVersion.DropDownStyle := System.Windows.Forms.ComboBoxStyle.DropDownList;
  308. Self.cboIPVersion.Location := System.Drawing.Point.Create(240, 124);
  309. Self.cboIPVersion.Name := 'cboIPVersion';
  310. Self.cboIPVersion.Size := System.Drawing.Size.Create(224, 21);
  311. Self.cboIPVersion.TabIndex := 10;
  312. Include(Self.cboIPVersion.SelectedValueChanged, Self.cboIPVersion_SelectedValueChanged);
  313. //
  314. // lblIPVersion
  315. //
  316. Self.lblIPVersion.Location := System.Drawing.Point.Create(240, 108);
  317. Self.lblIPVersion.Name := 'lblIPVersion';
  318. Self.lblIPVersion.Size := System.Drawing.Size.Create(100, 16);
  319. Self.lblIPVersion.TabIndex := 11;
  320. Self.lblIPVersion.Text := 'Label1';
  321. //
  322. // TIdDsnPropEdBindingNET
  323. //
  324. Self.AcceptButton := Self.btnOk;
  325. Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  326. Self.CancelButton := Self.btnCancel;
  327. Self.ClientSize := System.Drawing.Size.Create(470, 189);
  328. Self.Controls.Add(Self.lblIPVersion);
  329. Self.Controls.Add(Self.cboIPVersion);
  330. Self.Controls.Add(Self.edtPort);
  331. Self.Controls.Add(Self.lblPort);
  332. Self.Controls.Add(Self.edtIPAddress);
  333. Self.Controls.Add(Self.lblIPAddress);
  334. Self.Controls.Add(Self.btnDelete);
  335. Self.Controls.Add(Self.btnNew);
  336. Self.Controls.Add(Self.lbBindings);
  337. Self.Controls.Add(Self.lblBindings);
  338. Self.Controls.Add(Self.btnCancel);
  339. Self.Controls.Add(Self.btnOk);
  340. Self.FormBorderStyle := System.Windows.Forms.FormBorderStyle.Fixed3D;
  341. Self.MaximizeBox := False;
  342. Self.MaximumSize := System.Drawing.Size.Create(480, 225);
  343. Self.MinimizeBox := False;
  344. Self.MinimumSize := System.Drawing.Size.Create(480, 225);
  345. Self.Name := 'TIdDsnPropEdBindingNET';
  346. Self.ShowInTaskbar := False;
  347. Self.StartPosition := System.Windows.Forms.FormStartPosition.CenterScreen;
  348. Self.Text := 'WinForm';
  349. (System.ComponentModel.ISupportInitialize(Self.edtPort)).EndInit;
  350. Self.ResumeLayout(False);
  351. end;
  352. {$ENDREGION}
  353. procedure TIdDsnPropEdBindingNET.Dispose(Disposing: Boolean);
  354. begin
  355. if Disposing then
  356. begin
  357. if Components <> nil then
  358. begin
  359. Components.Dispose();
  360. Sys.FreeAndNil(FHandles);
  361. Sys.FreeAndNil( FIPv4Addresses);
  362. Sys.FreeAndNil( FIPv6Addresses);
  363. //don't free FCurrentHandle; - it's in the handles collection
  364. TIdStack.DecUsage;
  365. end;
  366. end;
  367. inherited Dispose(Disposing);
  368. end;
  369. constructor TIdDsnPropEdBindingNET.Create;
  370. begin
  371. inherited Create;
  372. //
  373. // Required for Windows Form Designer support
  374. //
  375. InitializeComponent;
  376. //
  377. // TODO: Add any constructor code after InitializeComponent call
  378. //
  379. FHandles := TIdSocketHandles.Create(nil);
  380. FIPv4Addresses := TIdStringList.Create;
  381. FIPv6Addresses := TIdStringList.Create;
  382. SetIPv4Addresses(nil);
  383. SetIPv6Addresses(nil);
  384. TIdStack.IncUsage;
  385. IPv4Addresses := GStack.LocalAddresses;
  386. UpdateEditControls;
  387. //captions
  388. btnNew.Text := RSBindingNewCaption;
  389. btnDelete.Text := RSBindingDeleteCaption;
  390. lblIPAddress.Text := RSBindingHostnameLabel;
  391. lblPort.Text := RSBindingPortLabel;
  392. lblIPVersion.Text := RSBindingIPVerLabel;
  393. btnOk.Text := RSOk;
  394. btnCancel.Text := RSCancel;
  395. //IPVersion choices
  396. //we yhave to strip out the & symbol. In Win32, we use this
  397. //in a radio-box so a user could select by pressingg the 4 or 6
  398. //key. For this, we don't have a radio box and I'm too lazy
  399. //to use two Radio Buttons.
  400. cboIPVersion.Items.Add(StripAndSymbol(RSBindingIPV4Item));
  401. cboIPVersion.Items.Add(StripAndSymbol(RSBindingIPV6Item));
  402. end;
  403. procedure TIdDsnPropEdBindingNET.SetHandles(const Value: TIdSocketHandles);
  404. begin
  405. FHandles.Assign(Value);
  406. UpdateBindingList;
  407. end;
  408. function TIdDsnPropEdBindingNET.GetList: string;
  409. begin
  410. Result := GetListValues(Handles);
  411. end;
  412. procedure TIdDsnPropEdBindingNET.SetIPv6Addresses(const Value: TIdStrings);
  413. begin
  414. if Assigned(Value) then
  415. begin
  416. FIPv6Addresses.Assign(Value);
  417. end;
  418. // Ensure that these two are always present
  419. if FIPv6Addresses.IndexOf(IPv6Loopback)=-1 then
  420. begin
  421. FIPv6Addresses.Insert(0,IPv6Loopback);
  422. end;
  423. if FIPv6Addresses.IndexOf(IPv6Wildcard1)=-1 then
  424. begin
  425. FIPv6Addresses.Insert(0,IPv6Wildcard1);
  426. end;
  427. end;
  428. procedure TIdDsnPropEdBindingNET.SetIPv4Addresses(const Value: TIdStrings);
  429. begin
  430. if Assigned(Value) then
  431. begin
  432. FIPv4Addresses.Assign(Value);
  433. end;
  434. // Ensure that these two are always present
  435. if FIPv4Addresses.IndexOf(IPv6Loopback)=-1 then
  436. begin
  437. FIPv4Addresses.Insert(0,IPv4Loopback);
  438. end;
  439. if FIPv4Addresses.IndexOf(IPv4Wildcard)=-1 then
  440. begin
  441. FIPv4Addresses.Insert(0,IPv4Wildcard);
  442. end;
  443. end;
  444. procedure TIdDsnPropEdBindingNET.SetList(const AList: string);
  445. begin
  446. FCurrentHandle := nil;
  447. FillHandleList(AList, Handles);
  448. UpdateBindingList;
  449. UpdateEditControls;
  450. end;
  451. procedure TIdDsnPropEdBindingNET.lbBindings_SelectedValueChanged(sender: System.Object;
  452. e: System.EventArgs);
  453. begin
  454. if lbBindings.SelectedIndex >= 0 then begin
  455. btnDelete.Enabled := True;
  456. FCurrentHandle := FHandles[lbBindings.SelectedIndex];
  457. end else begin
  458. btnDelete.Enabled := False;
  459. FCurrentHandle := nil;
  460. end;
  461. UpdateEditControls;
  462. end;
  463. procedure TIdDsnPropEdBindingNET.cboIPVersion_SelectedValueChanged(sender: System.Object;
  464. e: System.EventArgs);
  465. begin
  466. case cboIPVersion.SelectedIndex of
  467. 0 :
  468. begin
  469. if FCurrentHandle.IPVersion <> Id_IPv4 then
  470. begin
  471. FCurrentHandle.IPVersion := Id_IPv4;
  472. FillComboBox(edtIPAddress,FIPv4Addresses);
  473. FCurrentHandle.IP := IPv4Wildcard;
  474. end;
  475. end;
  476. 1 :
  477. begin
  478. if FCurrentHandle.IPVersion <> Id_IPv6 then
  479. begin
  480. FCurrentHandle.IPVersion := Id_IPv6;
  481. FillComboBox(edtIPAddress,FIPv6Addresses);
  482. FCurrentHandle.IP := IPv6Wildcard1;
  483. end;
  484. end;
  485. end;
  486. UpdateEditControls;
  487. UpdateBindingList;
  488. end;
  489. procedure TIdDsnPropEdBindingNET.edtIPAddress_SelectedValueChanged(sender: System.Object;
  490. e: System.EventArgs);
  491. begin
  492. FCurrentHandle.IP := edtIPAddress.SelectedItem.ToString;
  493. UpdateBindingList;
  494. end;
  495. procedure TIdDsnPropEdBindingNET.edtPort_ValueChanged(sender: System.Object;
  496. e: System.EventArgs);
  497. begin
  498. if Assigned(FCurrentHandle) then
  499. begin
  500. FCurrentHandle.Port := edtPort.Value.ToInt16(edtPort.Value);
  501. end;
  502. UpdateBindingList;
  503. end;
  504. procedure TIdDsnPropEdBindingNET.btnDelete_Click(sender: System.Object; e: System.EventArgs);
  505. var LSH : TIdSocketHandle;
  506. i : Integer;
  507. begin
  508. if lbBindings.SelectedIndex >= 0 then
  509. begin
  510. // Delete is not available in D4's collection classes
  511. // This should work just as well.
  512. i := lbBindings.get_SelectedIndex;
  513. LSH := Handles[i];
  514. Sys.FreeAndNil(LSH);
  515. lbBindings.Items.Remove(i);
  516. FCurrentHandle := nil;
  517. UpdateBindingList;
  518. end;
  519. Self.lbBindings_SelectedValueChanged(nil,nil);
  520. UpdateEditControls;
  521. end;
  522. procedure TIdDsnPropEdBindingNET.btnNew_Click(sender: System.Object; e: System.EventArgs);
  523. begin
  524. FCurrentHandle := FHandles.Add;
  525. FCurrentHandle.IP := IPv4Wildcard;
  526. FCurrentHandle.Port := FDefaultPort;
  527. UpdateBindingList;
  528. FillComboBox(edtIPAddress,FIPv4Addresses);
  529. UpdateEditControls;
  530. end;
  531. procedure TIdDsnPropEdBindingNET.UpdateBindingList;
  532. var
  533. i: integer;
  534. selected: integer;
  535. s: string;
  536. begin
  537. selected := lbBindings.SelectedIndex;
  538. lbBindings.BeginUpdate;
  539. try
  540. if lbBindings.Items.Count = FHandles.Count then begin
  541. for i := 0 to FHandles.Count - 1 do begin
  542. s := GetDisplayString(FHandles[i].IP, FHandles[i].Port, FHandles[i].IPVersion);
  543. if s <> lbBindings.Items[i].ToString then begin
  544. lbBindings.Items[i] := s;
  545. end;
  546. end;
  547. end else begin
  548. lbBindings.Items.Clear;
  549. for i := 0 to FHandles.Count-1 do begin
  550. lbBindings.Items.Add(GetDisplayString(FHandles[i].IP, FHandles[i].Port,FHandles[i].IPVersion));
  551. end;
  552. end;
  553. finally
  554. lbBindings.EndUpdate;
  555. if Assigned(FCurrentHandle) then begin
  556. lbBindings.SelectedIndex := FCurrentHandle.Index;
  557. end else begin
  558. lbBindings.SelectedIndex := Min(selected, lbBindings.Items.Count-1);
  559. end;
  560. end;
  561. { selected := lbBindings.SelectedItem;
  562. lbBindings.Items.BeginUpdate;
  563. try
  564. if lbBindings.Items.Count = FHandles.Count then begin
  565. for i := 0 to FHandles.Count - 1 do begin
  566. s := GetDisplayString(FHandles[i].IP, FHandles[i].Port, FHandles[i].IPVersion);
  567. if s <> lbBindings.Items[i] then begin
  568. lbBindings.Items[i] := s;
  569. end;
  570. end;
  571. end else begin
  572. lbBindings.Items.Clear;
  573. for i := 0 to FHandles.Count-1 do begin
  574. lbBindings.Items.Add(GetDisplayString(FHandles[i].IP, FHandles[i].Port,FHandles[i].IPVersion));
  575. end;
  576. end;
  577. finally
  578. lbBindings.Items.EndUpdate;
  579. if Assigned(FCurrentHandle) then begin
  580. lbBindings.ItemIndex := FCurrentHandle.Index;
  581. end else begin
  582. lbBindings.ItemIndex := Min(selected, lbBindings.Items.Count-1);
  583. end;
  584. end; }
  585. end;
  586. procedure TIdDsnPropEdBindingNET.UpdateEditControls;
  587. begin
  588. if Assigned(FCurrentHandle) then
  589. begin
  590. edtPort.Text := '';
  591. edtPort.Value := FCurrentHandle.Port;
  592. case FCurrentHandle.IPVersion of
  593. Id_IPv4 :
  594. begin
  595. FillComboBox(edtIPAddress,FIPv4Addresses);
  596. edtIPAddress.SelectedItem := edtIPAddress.Items[0];
  597. cboIPVersion.SelectedItem := cboIPVersion.Items[0];
  598. end;
  599. Id_IPv6 :
  600. begin
  601. FillComboBox(edtIPAddress,FIPv6Addresses);
  602. edtIPAddress.SelectedItem := edtIPAddress.Items[0];
  603. cboIPVersion.SelectedItem := cboIPVersion.Items[1];
  604. end;
  605. end;
  606. edtIPAddress.SelectedIndex := edtIPAddress.Items.IndexOf(FCurrentHandle.IP);
  607. end;
  608. lblIPAddress.Enabled := Assigned(FCurrentHandle);
  609. edtIPAddress.Enabled := Assigned(FCurrentHandle);
  610. lblPort.Enabled := Assigned(FCurrentHandle);
  611. edtPort.Enabled := Assigned(FCurrentHandle);
  612. lblIPVersion.Enabled := Assigned(FCurrentHandle);
  613. cboIPVersion.Enabled := Assigned(FCurrentHandle);
  614. end;
  615. procedure TIdDsnPropEdBindingNET.FillComboBox(
  616. ACombo: System.Windows.Forms.ComboBox; AStrings: TIdStrings);
  617. var i : INteger;
  618. begin
  619. ACombo.Items.Clear;
  620. for i := 0 to AStrings.Count -1 do
  621. begin
  622. ACombo.Items.Add(AStrings[i]);
  623. end;
  624. end;
  625. function TIdDsnPropEdBindingNET.Execute: Boolean;
  626. begin
  627. Result := Self.ShowDialog = System.Windows.Forms.DialogResult.OK;
  628. end;
  629. end.