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

Datagridview أنا والعذاب من Vb6 الي Vb.net

بدأه programer_vbnet في 26 مارس 2009 · 6 رد · 1,881 مشاهدة · في Microsoft Visual Basic.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم اخواني ... تحية طيبة وبعد

مشكلتي بختصار تخص Cell

عايز اعرف اتعامل مع البيانات الي بداخلها عن طريق Event زي Chang - KeyPress >> الـ .. Event ( الي بتعتمد علي الحروف )

أنا مستغرب جداً أن في حدث أسمة CellBeginEdit و CellEndEdit ومفيش Event بتحس بال Cell طول منا بكتب جواها .... ؟؟!!

فين الـ Event ..... الي بتحس بالكي بورد طول منا جوه الـ Cell زي KeyDown- KeyUp- KeyPress ?

ياريت المشكال دي يكون ليها حل عندكم ...... وشكراً

#2

مرحباً أخي الكريم,

على حسب علمي أنه ليس هنالك من حدث يتابع تغيّرات الخلية في وقت التعديل, ولذلك أعتقد أنه عليك بناء صف جديد يرث التحكم ( DataGridView ) ومن ثم تصريح ( Timer ) واستخدام الحدثين ( ( CellBeginEdit ) و ( CellEndEdit ) ), ليس عليك فهم الفكرة إن لم تكن تهمك, لكن إذا كنت تريد تطوير إبداعك فحاول فهم التالي :

الصف التالي والمدعو ( MyDataGridView ) يرث التحكم ( DataGridView ) كما ذكرنا, وأيضاً يحوي Timer لتفجير الحدث المصرح CellValueChanging وبذلك نكون قد وصلنا إلى مانريد عمله

 

[color= #000000; font-weight: bold;]Public [color= #000000; font-weight: bold;]Class MyDataGridView

    Inherits DataGridView

    [color= #000000; font-weight: bold;]Public Delegate Sub _CellValueChanging[color= #009900;](ByVal Sender [color= #b1b100;]As Object[color= #339933;], ByVal E [color= #b1b100;]As MyDataGridViewCel
lEventArgs[color= #009900;])

    [color= #000000; font-weight: bold;]Public Event CellValueChanging [color= #b1b100;]As _CellValueChanging

    [color= #000000; font-weight: bold;]Private WithEvents MyTimer [color= #b1b100;]As [color= #000000; font-weight: bold;]New Timer

    [color= #000000; font-weight: bold;]Public Sub [color= #000000; font-weight: bold;]New[color= #009900;]([color= #009900;])

        MyBase[color= #339933;].[color= #000000; font-weight: bold;]New[color= #009900;]([color= #009900;])

        MyTimer[color= #339933;].Interval [color= #339933;]= [color= #cc66cc;]100

    [color= #990000;]End Sub

    [color= #000000; font-weight: bold;]Public Structure MyDataGridViewCellEventArgs

        [color= #000000; font-weight: bold;]Public RowIndex [color= #b1b100;]As Integer

        [color= #000000; font-weight: bold;]Public ColumnIndex [color= #b1b100;]As Integer

        [color= #000000; font-weight: bold;]Public Value [color= #b1b100;]As Object

        [color= #000000; font-weight: bold;]Public Sub [color= #000000; font-weight: bold;]New[color= #009900;](ByVal _RowIndex[color= #339933;], ByVal _ColumnIndex[color= #339933;], ByVal _Value[color= #009900;])

            RowIndex [color= #339933;]= _RowIndex [color= #339933;]: ColumnIndex [color= #339933;]= _ColumnIndex

            Value [color= #339933;]= _Value

        [color= #990000;]End Sub

    [color= #990000;]End Structure

    Protected Overrides Sub OnCellBeginEdit[color= #009900;](ByVal E [color= #b1b100;]As [color= #990000;]System[color= #339933;].Windows[color= #339933;].Forms[color= #339933;].DataGridViewCellCan
celEventArgs[color= #009900;])

        MyBase[color= #339933;].OnCellBeginEdit[color= #009900;](E[color= #009900;])

        MyTimer[color= #339933;].Enabled [color= #339933;]= [color= #000000; font-weight: bold;]True

    [color= #990000;]End Sub

    Protected Overrides Sub OnCellEndEdit[color= #009900;](ByVal E [color= #b1b100;]As [color= #990000;]System[color= #339933;].Windows[color= #339933;].Forms[color= #339933;].DataGridViewCellEvent
Args[color= #009900;])

        MyBase[color= #339933;].OnCellEndEdit[color= #009900;](E[color= #009900;])

        MyTimer[color= #339933;].Enabled [color= #339933;]= [color= #000000; font-weight: bold;]False

    [color= #990000;]End Sub

 

    [color= #000000; font-weight: bold;]Private Sub MyTimer_Tick[color= #009900;](ByVal Sender [color= #b1b100;]As Object[color= #339933;], [color= #990000;]_

    ByVal E [color= #b1b100;]As [color= #990000;]System[color= #339933;].EventArgs[color= #009900;]) Handles MyTimer[color= #339933;].Tick

        RaiseEvent CellValueChanging[color= #009900;](Me[color= #339933;], [color= #000000; font-weight: bold;]New MyDataGridViewCellEventArgs[color= #009900;](Me[color= #339933;].CurrentCell[color= #339933;].RowInde
x[color= #339933;], Me[color= #339933;].CurrentCell[color= #339933;].ColumnIndex[color= #339933;], Me[color= #339933;].CurrentCell[color= #339933;].EditedFormattedValue[color= #009900;])[color= #009900;])

    [color= #990000;]End Sub

[color= #990000;]End [color= #000000; font-weight: bold;]Class

والإستخدام :

 

    [color= #000000; font-weight: bold;]Private Sub MyDataGridView1_CellValueChanging[color= #009900;](ByVal Sender [color= #b1b100;]As Object[color= #339933;], ByVal E [color= #b1b100;]As MyDataGrid
View[color= #339933;].MyDataGridViewCellEventArgs[color= #009900;]) Handles MyDataGridView1[color= #339933;].CellValueChanging

        Text [color= #339933;]= E[color= #339933;].Value

    [color= #990000;]End Sub

في المرفقات أيضاً المشروع المسبوق ذكره .

بالتوفيق .

CellValueChanging.rar

تم تعديل هذه المشاركة بواسطة Bashar Rabat في 26 مارس 2009 في 15:42

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#3

السلام عليكم اخي الكريم Bashar Rabat .. المثال ممتاز جداً ......بس هناك مشكلة في ديزن الفورم حولت كثير ابحث عن المشكلة مش لاقي حل لعرض ديزين الفورم

ولكن قمت بتجربتة في الرن تايم .. اشتغل كويس جداا وأد المطلب كما يجب أن يكون .... (بصراحة مكنتش مصدق)

لكن المثال انا مش فاهمة اوي .. ازي اربط ال Event الجديدة بالجريد فيوه ...

اتمني من حضرتك حاجتين ..

أولاً ترفق المثال مرة اخري بس يكون الديزين شغال أكون شاكر ليك جداً ...

الحاجة التانية ....أتمني توضيح أكتر طريقة ربط ال Event بالداتا جريد فيوه

وشكراً جزيلاً ...

تم تعديل هذه المشاركة بواسطة programer_vbnet في 26 مارس 2009 في 17:31

#4

مرحباً أخي الكريم,

لنبدأ بشرح فكرة الصف التالي :

في بادىء الأمر قمت بإنشاء صف جيد, ذلك بالنقر على Project من القائمة الرئيسية ثم النقر على ( Class ) ليتم عرض مربع حوار إضفة ( صف, نموذج, تحكم, مربع حوار, الخ... ) وسميت ذلك الصف الجديد بإسم ( MyDataGridView ), يعدها نقرنا على ( Add ).

الآن شرح مافي الداخل صفنا الحالي,

قمنا بجعل الصف MyDataGridView يرث التحكم ( DataGridView ) وذلك عير ( Inherits DataGridView ) في سطر الثالث من الصف صرحنا ( بيان عام أي Delegate Sub ) مافائدة هذا البيان ؟

إن البيان في الـVB قادر على أن يكون شبه نوع مع الأحداث, وقابل لأن يكون معامل لمهام ما, اي يمكن استخدام هذا البيان كمعامل لـ Theard ما, وفي حالتنا هذه استخدمناه على شكل بيان لحدث مسمى CellValueChanging وجعلناه يحوي معاملين ( الأول وهو المرسل على هيئة Object, والثاني هو ( خصائص أو قيم ) الحدث CellValueChanging على هيئة MyDataGridViewCellEventArgs ) كما ترى أن الـ( MyDataGridViewCellEventArgs ) في عبارة عن ( Structure ) أو ( معلومات قيم ) أي لاتحتاج للتعريف على شكل جديد, ولكي لا تستغرب من شكل الاجراء New في تلك الـ Structure فمبتغانا منها هو فقط لإختصار تصريحها وإعداد خصائصها أو متغيّيراتها .

الآن في النهاية تعريف حدث عام ( CellValueChanging الذي هو على هئية الـبيان ( _CellValueChanging ), لكن أو أن أخبرك أنه يمكننا أيضاً كتابته على الشكل التالي والإستغناء عن البيان ( _CellValueChanging ) :

 

[color= #000000; font-weight: bold;]Public Event CellValueChanging[color= #009900;](Byval Sender [color= #b1b100;]As Object[color= #339933;], Byval E [color= #b1b100;]As MyDataGridViewCellEventArgs[color= #009900;])

ويمكنك أيضاً جعل هذا الحدث على شكل إفتراضي أي عند النقر على تحكمنا الحالي من المصمم سيتم إضافة الحدث llValueChanging

بشكل تلقائي ويتم ذلك عبر اختيار الحدث الافتراضي للصف الحالي من مستعرض الخصائص :

إسم الخاصية : Default Event

وماإلى ذلك تصريح Timer ليكون العامل الأساسي في تفجير هذا الحدث, لكن في نهاية الشرح ستجد صف أخر شبيه بالتالي, لكنه سيحوي متغير من نوع Object سنستخدمه ليتم مقارنة القيمة الحالية بآخر قيمة تم تعديلها كي لا يتم تفجير الحدث إلا بعد تغيّر القيمة الحالية في إحدى الخلايا .

يتم تشغيل ذلك الـ( Timer ) عند بدء المستخدم تعديل إحدى الخلايا ويتم إيقافه عند الإنتهاء من التحرير أو التعديل .

الخاصية العاملة في هذا الأمر برمته هي الخاصية ( EditedFormattedValue ) التي هي القيمة التي يقوم المستخدم بكتابتها في ةقت التعديل الفعلي,

هذا كل شيء أتمنى أن أكون قد شرحت بشكل سلس لؤصل لك فكرة الوراثة وتشكيل أحداث ؟

 
Public Class MyDataGridView
	Inherits DataGridView
	Public Delegate Sub _CellValueChanging(ByVal Sender As Object, ByVal E As MyDataGridViewCellEventArgs)
	Public Event CellValueChanging As _CellValueChanging
	Private WithEvents MyTimer As New Timer
	Public Sub New()
		MyBase.New()
		MyTimer.Interval = 100
	End Sub
	Public Structure MyDataGridViewCellEventArgs
		Public RowIndex As Integer
		Public ColumnIndex As Integer
		Public Value As Object
		Public Sub New(ByVal _RowIndex, ByVal _ColumnIndex, ByVal _Value)
			RowIndex = _RowIndex : ColumnIndex = _ColumnIndex
			Value = _Value
		End Sub
	End Structure
	Protected Overrides Sub OnCellBeginEdit(ByVal E As System.Windows.Forms.DataGridViewCellCancelEventArgs)
		MyBase.OnCellBeginEdit(E)
		MyTimer.Enabled = True
	End Sub
	Protected Overrides Sub OnCellEndEdit(ByVal E As System.Windows.Forms.DataGridViewCellEventArgs)
		MyBase.OnCellEndEdit(E)
		MyTimer.Enabled = False
	End Sub
 
	Private Sub MyTimer_Tick(ByVal Sender As Object, _
	ByVal E As System.EventArgs) Handles MyTimer.Tick
		RaiseEvent CellValueChanging(Me, New MyDataGridViewCellEventArgs(Me.CurrentCell.RowIndex, Me.CurrentCell.ColumnIndex, Me.CurrentCell.EditedFormattedValue))
	End Sub
End Class

 

هذا الصف بعد التعديل :

<System.ComponentModel.DefaultEvent("CellValueChanging")> Public Class MyDataGridView
	Inherits DataGridView
	Public Delegate Sub _CellValueChanging(ByVal Sender As Object, ByVal E As MyDataGridViewCellEventArgs)
	Public Event CellValueChanging As _CellValueChanging
	Private WithEvents MyTimer As New Timer
	Private CurrentValue As Object = Nothing
 
	Public Structure MyDataGridViewCellEventArgs
		Public RowIndex As Integer
		Public ColumnIndex As Integer
		Public Value As Object
 
		Public Sub New(ByVal _RowIndex, ByVal _ColumnIndex, ByVal _Value)
			RowIndex = _RowIndex : ColumnIndex = _ColumnIndex
			Value = _Value
		End Sub
 
	End Structure
 
	Public Sub New()
		MyBase.New()
		MyTimer.Interval = 100
	End Sub
 
	Protected Overrides Sub OnCellBeginEdit(ByVal E As System.Windows.Forms.DataGridViewCellCancelEventArgs)
		MyBase.OnCellBeginEdit(E)
		MyTimer.Enabled = True
	End Sub
 
	Protected Overrides Sub OnCellEndEdit(ByVal E As System.Windows.Forms.DataGridViewCellEventArgs)
		MyBase.OnCellEndEdit(E)
		MyTimer.Enabled = False
	End Sub
 
	Private Sub MyTimer_Tick(ByVal Sender As Object, _
	ByVal E As System.EventArgs) Handles MyTimer.Tick
		If (CurrentValue Is Nothing) Then CurrentValue = Me.CurrentCell.EditedFormattedValue
		If Not (CurrentValue = Me.CurrentCell.EditedFormattedValue) Then
			RaiseEvent CellValueChanging(Me, New MyDataGridViewCellEventArgs(Me.CurrentCell.RowIndex, Me.CurrentCell.ColumnIndex, Me.CurrentCell.EditedFormattedValue))
			CurrentValue = CurrentCell.EditedFormattedValue
		End If
	End Sub
 
End Class

بالتوفيق

تم تعديل هذه المشاركة بواسطة Bashar Rabat في 27 مارس 2009 في 14:19

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

#5

السلام عليكم اخي الكريم Bashar Rabat شكراً جداا علي اهتمامك المثال أكثر من رائع

انا كنت وجد طريقة اخري .... لكن لم اجدها فعالة مثل المثال الذي قمت بأرفاقة ( لم استطيع أن أحصل علي طول النص داخل ال Cell لان في وقت تفجير الحدث بيكونcell بـ Null

مما لا ياعطي فائدة للحدث من الأساس

وهذا هو الكود

	Private Sub DataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView.EditingControlShowing
		Dim TextEdit As TextBox = e.Control

		RemoveHandler TextEdit.TextChanged, AddressOf TextChang
		AddHandler TextEdit.TextChanged, AddressOf TextChang

	End Sub


	Public Sub TextChang(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

		MsgBox("Hi")

	End Sub

لكن المثال بتاعك كان ممتاز .. وكونت أود أن أثقل عليك وبصراحة النية كانت ممبايته من قبل .. لاني حولت وبحثت كثير علي النت ولم اجد حل غير في هذه المشاركة .. فجزاك الله كل خير

هي نفس المشكلة لم اجد حدث KeyPress للـ Cell .... أنا نفذت نفس الكود السابق علي الحدث KeyPress وشتغل تمام لكن الجريد فيو لااااازم تعذبني ...... زوار ال Enter وحتي ال Tab لا يستجيب للحدث .... ايضاً مما لا يعطي فايدة كاملة للحدث

هذا هو الكود

	Private Sub DataGridView_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView.EditingControlShowing
		Dim TextEdit As TextBox = e.Control

		RemoveHandler TextEdit.KeyPress, AddressOf Text_KeyPress
		AddHandler TextEdit.KeyPress, AddressOf Text_KeyPress

	End Sub

	Public Sub Text_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)

		If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Space) Then
			MsgBox("Hi")
		End If

	End Sub

ملحوظة : المثال السابق نفذتة علي زورار ال Space حتي يتم تفعيل الحدث .. جرب أنا تغيرة للزورار الـ Return أو الـ Tab لن يحس بالحدث

أتمني أن اجد لديك حل في ال KeyPress >> وأنا أسف جداً لاني أثقل عليك ...... وجزك الله كل خير

تم تعديل هذه المشاركة بواسطة programer_vbnet في 29 مارس 2009 في 21:18

#6

أخي العزيز Bashar Rabat أنا قمت بتجربت المثال الذي ارفقة لكن يحدث شيء غريب مش عارف احدد المشكلة أية .... عندما جربت المثال علي فورم تانية رفض تشغيل البرنامج رغم أني قمت بالتعديل في ملف الديزين الخاص بالفورمة الجديدة مثل الفورمة التي تعمل .. ولكن رفض والديزين فورم تم تعطيلة

ويرجع السبب الي التغير الذي حدث في الديزين فورم علي ما أعتقد

لقد قمت بتدعيل الاتي

Friend WithEvents MyDataGridView1 As WindowsApplication1.MyDataGridView

بدل من

Friend WithEvents MyDataGridView1 As System.Windows.Forms.DataGridView

أنا عارف أثقل عليك لكن المثال بتاعك هو ما أبحث عنة ..فأرجو أن تكمل جميلك لاني الـ Event دي متوقف عليها اشياء كثيرة في شغلي

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

#7

مرحباً أخي الكريم,

رجاءً قم بإدراج الخطأ الذي يظهر لك, شكراً,

أو جرب إغلاق واجهة التصميم للـ( Form ) ومن ثم قم بإعادة فتحه,

بالتوفيق

The important thing is not so much

that every child should be taught,

as that every child should be given the wish to learn.

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