View Single Post
Old June 20th, 2009   #21 (permalink)
@ruantec
Emu author
 
@ruantec's Avatar
 
Join Date: Nov 2002
Location: Austria (originally from Dominican Republic)
Posts: 2,379
mmm i donīt really understand what exactly you mean/want but one thing i donīt get..... why are you using VS2002??? anyways controls in VS2002(which i hated in the past and drived me to hate C# aswell) should have the .Background property aswell as Foreground etc.. anyways iīve noticed youīre writing lots of code in order to change sinple colors... you believe it or not C# offers lots of great stuff to do your task very easy and in a very simple way. each form contains lots of features that arenīt visible in the editor or in the properties window but they are accessable via code and by moving the mouse over them you can find and see thatīs behind it.

one of those properties is ".Controls" which is a collection of contros on the current window. that property not only holds the elements but it allows you to add new ones or do whatever you want to it and at the same time it give you access to every control so you can easily interact on each of them via code.. so now what do i want to show you with this??? the main idea is to show you how to change several properties on every control in a form without the need to write there IDīs each time and property and in that way waste time and code lol..



here is an example:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testingControls
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_change_Click(object sender, EventArgs e)
        {
            foreach (Control control in this.Controls)
            {
                if (control is Button)
                    control.BackColor = Color.Red;
                else if (control is PictureBox)
                    control.BackColor = Color.Blue;
            }
        }
    }
}
as you can see am changing the background color of 2 different types of controls on a form and adding a different value depending on the type.. so how do i know about it??? well very simple... i told you once to use a break point and navigate your code as VS and C# other than many coding tools allows you to deeply navigate all your controls,classes and objects and so see whatīs really behind it and avoid possible errors you could do.

how do i do that? check out the following pic



thatīs why i think C# is a beauty and also the reason why i love it now.

Regards
@ruantec
Attached Files
File Type: zip testingControls.zip (39.3 KB, 1 views)
__________________

Current development tools:

Visual C++.net, Visual C#.net
Visual VB.net, Visual Webdeveloper.net
Bloodshed Dev C++, Borland C++
Visual Basic 6

Last edited by @ruantec; June 20th, 2009 at 20:34..
@ruantec is offline   Reply With Quote

Advertisement [Remove Advertisement]