الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

التعامل مع ال controls من خلال string

مغلق
بدأه C#please في 6 فبراير 2006 · 6 رد · 510 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

عندي علي ال form كنترول pictureBox

و string كما في التالي :

string Name="PictureBox1";

اريد أن اتعامل مع ال PictureBox بالكود من خلال ال string

مثلا بدلا من :

PictureBox1.Image = ......;

يكون :

Name.Image=....;

وشكرا ...

والسلام عليكم ورحمة الله وبركاته

#2
 PictureBox box = this.FindControl("PictureBox1")  As PictureBox

Sr. Software Development Engineer
Hulu, LLC
My Blogs

#3

جزاكم الله خيرا

ونفع بنا وبك يا أخي الحبيب

#4

أخي الحبيب :

للأسف لم أجد ال : this.FindControl

#5

احتمال

PictureBox box = Controls.FindControl("PictureBox1")  As PictureBox

لم أجربها لعدم وجود ال VS على جهازي

شكرا أستاذنا البشمهندس

3424.gif
#6

اسف فهذه الخاصية FindControl موجودة فقط في Web Forms و لا أعرف لماذا لا توجد في Windows Forms مع انها خاصية مهمة

يمكنك أن تقوم ببناء هذه الfunction بنفسك هكذا

private Control FindControl(string id)
        {
            foreach (Control ctrl in this.Controls)
                if (ctrl.Name == id)
                    return ctrl;
            return null;
        }

        private Control FindControl(string id, bool caseSensetive)
        {
            if (caseSensetive)
                return FindControl(id);
            string lowerId = id.ToLower();
            foreach (Control ctrl in this.Controls)
                    if (ctrl.Name.ToLower() == lowerId)
                        return ctrl;
            return null;
        }

الfunction الأولى تبحث عن الControl في الform باسمه و لكنها Case Sensetive أي أنها تبحث عن الاسم المُعطى لها تماماً بمعنى أن

TextBox1 تختلف عن textbox1 و لهذا يجب اعطائها اسم الControl صحيحاً

أما الثانية فهي تُعطيك الاختيار اذا كنت تريد البحث Case Sensetive أو لا...

و استخدامها بسيط و هذا مثال

تخيل أن الForm بها TextBox واحد فقط اسمه textBox1 هكذا يكون الكود

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            TextBox box = FindControl("texTBox1", false) as TextBox;
            if(box != null)
                box.Text = "Found";
            
        }

        private Control FindControl(string id)
        {
            foreach (Control ctrl in this.Controls)
                if (ctrl.Name == id)
                    return ctrl;
            return null;
        }

        private Control FindControl(string id, bool caseSensetive)
        {
            if (caseSensetive)
                return FindControl(id);
            string lowerId = id.ToLower();
            foreach (Control ctrl in this.Controls)
                    if (ctrl.Name.ToLower() == lowerId)
                        return ctrl;
            return null;
        }
    }
}

Sr. Software Development Engineer
Hulu, LLC
My Blogs

#7

جزاكم الله خيرا يا أخي الحبيب

هذا الموضوع مغلق.

مواضيع مشابهة