Jan 3, 2014

Промена на боја на MDI Parrent form C# - во една линија код



Свите што програмиаа во C#  користеле MDI форме горе доле ни се појавила потреба да се смени боја да не биде таја под дефаулт, а што е најглупо у пропертис на формата нема и не работа, можеш да сменис само слика...

е сеа workaround на ова да се додаде код у Load  евентот на формата што ќе го најде MDI Clientot и ќе му ја смени бојата.

На официјалната страна од мајкрософт е објавен следниов код , кој може да биде 3x по оптимален односно да се напише со само една линија..

не дека ќе добиете нешто на брзина, или квалитет едноставно кодот ќе биде полесен за читање а истово може да се примени и на други работи , наместо да вртите циклуси да си искористите Linq



Ова е кодот од мајкрософт
How to change the background color for an MDI parent form in Visual C#

Create a Sample Windows Application by Using Visual C# .NET

  1. Create a new Visual C# Windows application. Form1 is created by default.
  2. Click the form, and then, on the View menu, select Properties Window to view the properties for the form.
  3. Set the BackColor property to the color that you want (such as LightBlue).
  4. Set the IsMDIContainer property to True. Note that the background color of the form changes to the color that theApplication Background color is set to in Control Panel.
  5. Set the WindowState property to Maximized.
  6. Double-click the form to view its code window.
  7. Paste the following code into the form's Load event handler:
    MdiClient ctlMDI;
    
    // Loop through all of the form's controls looking
    // for the control of type MdiClient.
    foreach (Control ctl in this.Controls)
    {
       try
       {
          // Attempt to cast the control to type MdiClient.
          ctlMDI = (MdiClient) ctl;
    
          // Set the BackColor of the MdiClient control.
          ctlMDI.BackColor = this.BackColor;
       }
       catch (InvalidCastException exc)
       {
          // Catch and ignore the error if casting failed.
       }           
    }
      
    // Display a child form to show this is still an MDI application.
    Form2 frm = new Form2();
    frm.MdiParent = this;
    frm.Show();
         
  8. On the Project menu, click Add Windows Form.
  9. Accept the default name Form2.cs, and then click Open.
  10. Press F5 to run the application.
Note that the MDI parent form loads and has a light blue background.

Цел овој  foreach циклус може да се замени со една линија, зошто секако нема да имате повеќе од еден mdiClient дури и да имате може да искористите at() наместо  first()


using System.Linq;
private void frmSettings_Load( object sender, EventArgs e ) {
     this.Controls.OfType<MdiClient>().First().BackColor = System.Drawing.Color.Beige;
}


Незнам колку  ќе ви користи постов али дефинитивно е добро да се има на блог да не се бара секогаш.. 


No comments :

Post a Comment