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

[ تمت الإجابة ]مالمشكلة في هذا الكود ؟؟؟

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

بســم الله الـرحمــن الرحيــم

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

تحية طيبة للجميع

:happy:

المهم انظروا إلى هذا الكود

B)

///////////////this button delete qoustions/////////////////

fileName = ComboBox1.SelectedItem //name of fole

Dim num, index, n2, n3 As Integer

Dim s As String

num = CheckedListBox1.SelectedIndex// checkedlistBox contain 3 qoustions

TextBox2.Text &= num

CheckedListBox1.Items.Remove(CheckedListBox1.SelectedItem)

Dim fileCop As New FileStream(fileName & ".txt", FileMode.Open, FileAccess.Read)

fle = New System.IO.StreamReader(fileCop)

str = File.ReadAllLines(fileName & ".txt")

While fle.Peek <> -1

str(index) = fle.ReadLine() // store in array to do delete

index = index + 1

End While

fle.Close()

Dim fileCop1 As New FileStream(fileName & ".txt", FileMode.Open, FileAccess.Write)

fleW = New System.IO.StreamWriter(fileCop1)

s = "" // temp string

If count = 0 Then // if user press the button delete

While n2 < 3 // to swap because I remove Gap mean :: shift

If n2 = num + 2 Then

str(n2) = ""

str(n2) = str(n2 + 1)

End If

'shift /// complete shift

s = str(n2 + 1)

str(n2 + 1) = str(n2)

str(n2) = s

n2 = n2 + 1

End While

While n3 < 8 // to write in file

fleW.WriteLine(str(n3))

n3 = n3 + 1

End While

'*************************************

وكذه لكل الأسئلة

وأما كوينت حاطته في الفورم عشان كل ما ظغط البوتتون ينمسح كويسشن واحد فقط

وأما النم حاطتها عشان تشوف الإنديكس من التشيكد ليست

يعني إذا اختار السؤال الأول ترجع نم صفر ......... وهكذا

المشكله ؟؟ إنه في الفايل ماينمسح شي بس يصير سطر فاضي مدري ليش ؟؟؟وأححيانا يمسح العناصر الأولى في الفايل؟؟؟

أتمنى توضيح السبب

وألــــــــــــــــف شكر للجميع :wub: :wub: <<<أدري طولت الموضوع

#2

عذراً الـ Code غير واضح :( .

إذا اردت التعامل مع الملفات بشكل مباشر عليك عمل نسخة احتياطية في مكان ما من الـ Hard-Disk أوفي ذاكرة الحاسوب ( أي تصريح متغير من نوع " مصفوفة نصية " ), بعد التعديل اي على سبيل المثال أنك تريدين مسح سطر معين, طبعاً بأي Stream ليس هنالك مايدعو بالعملية ( Remove Or Delete ) لذلك عليك إعادة كتابة الملف مع تجاهل السطور المراد حذفها .

مثال ( مكان من الـ Hard-Disk ) :

 

Dim FileName [color= #b1b100;]As String [color= #339933;]= [color= #0000ff;]"C:TextFile.txt"

Dim TempFileName [color= #b1b100;]As String [color= #339933;]= Replace[color= #009900;](Filename[color= #339933;], [color= #0000ff;]".txt"[color= #339933;], [color= #0000ff;]".temp"[color= #009900;])

 

[color= #000000; font-weight: bold;]Private Sub RemoveLine[color= #009900;]( Byval LineIndex [color= #b1b100;]As Integer [color= #009900;])

My[color= #339933;].Computer[color= #339933;].FileSystem[color= #339933;].CopyFile[color= #009900;](FileName[color= #339933;], TempFileName[color= #009900;])

Dim SWriter [color= #b1b100;]As [color= #000000; font-weight: bold;]New StreamWriter[color= #009900;]( FileName[color= #339933;], [color= #000000; font-weight: bold;]False [color= #009900;])

Dim SReader [color= #b1b100;]As [color= #000000; font-weight: bold;]New StreamReader[color= #009900;]( TempFileName [color= #009900;]) 

Dim CurrentIndex [color= #b1b100;]As Integer [color= #339933;]= [color= #cc66cc;]0

[color= #b1b100;]Do [color= #b1b100;]While SReader[color= #339933;].Peek [color= #339933;]<> [color= #cc66cc;]-1

[color= #b1b100;]If Not CurrentIndex [color= #339933;]= LineIndex Then

SWriter[color= #339933;].WrileLine[color= #009900;]( SReader[color= #339933;].ReadLine[color= #009900;]([color= #009900;]) [color= #009900;])

[color= #b1b100;]Else

SReader[color= #339933;].ReadLine[color= #009900;]([color= #009900;])

[color= #990000;]End [color= #b1b100;]If

CurrentIndex [color= #339933;]+= [color= #cc66cc;]1

Loop

SWriter[color= #339933;].Close[color= #009900;]([color= #009900;]) [color= #339933;]: SReader[color= #339933;].Close [color= #009900;]([color= #009900;])

My[color= #339933;].Computer[color= #339933;].FileSystem[color= #339933;].DeleteFile[color= #009900;](TempFileName[color= #009900;])

Msgbox[color= #009900;]( [color= #0000ff;]"Done !"[color= #009900;])

Process[color= #339933;].Start[color= #009900;]( FileName [color= #009900;]) [color= #0000ff;]' لفتح الملف 
1575;لمعدل 

[color= #0000ff;]End Sub

[color= #0000ff;] 

[color= #0000ff;] 

[color= #0000ff;] 

[color= #0000ff;] 

 

[color= #0000ff;]

مثال ( مكان من ذاكرة الحاسوب ) :

 

Dim FileName As String [color= #339933;]= [color= #0000ff;]"C:TextFile.txt"

 

[color= #000000; font-weight: bold;]Private Sub DeleteLine[color= #009900;]( Byval LineIndex [color= #b1b100;]As Integer [color= #009900;])

Dim Lines[color= #009900;]([color= #009900;]) [color= #b1b100;]As String [color= #339933;]= [color= #990000;]File[color= #339933;].ReadAllLines[color= #009900;]( FileName [color= #009900;])

Dim SWriter [color= #b1b100;]As [color= #000000; font-weight: bold;]New StreamWriter[color= #009900;]( FileName[color= #339933;], [color= #000000; font-weight: bold;]False [color= #009900;])

[color= #b1b100;]For I [color= #b1b100;]As Integer [color= #339933;]= [color= #cc66cc;]0 To UBound[color= #009900;]( Lines [color= #009900;])

[color= #b1b100;]If Not I [color= #339933;]= LineIndex Then SWriter[color= #339933;].WriteLine[color= #009900;]( Lines[color= #009900;]( I [color= #009900;]) [color= #009900;])

[color= #990000;]Next I

SWriter[color= #339933;].Close

Process[color= #339933;].Start[color= #009900;]( FileName [color= #009900;])[color= #0000ff;]' لفتح الملف 
575;لمعدل 

[color= #0000ff;]End Sub

[color= #0000ff;] 

[color= #0000ff;] 

[color= #0000ff;] 

[color= #0000ff;] 

 

[color= #0000ff;]

وإذا أردت حذف مجموعة من سطور, يمكنك استخدام مصفوفة عدد صحيح ( Integer أو Int32 ), والإعتماد على العملية ذات المرجع المنطقي ( Contains ) اي تحوي .

مثال :

 

Dim Indices As [color= #000000; font-weight: bold;]New [color= #990000;]List[color= #009900;](Of Integer[color= #009900;])

Dim FileName [color= #b1b100;]As String [color= #339933;]= [color= #0000ff;]"C:TextFile.txt"

 

[color= #000000; font-weight: bold;]Private Sub DeleteLine[color= #009900;]( Byval LineIndex [color= #b1b100;]As Integer [color= #009900;])

Indices[color= #339933;].AddRange[color= #009900;]([color= #000000; font-weight: bold;]New Int16[color= #009900;]([color= #009900;]) [color= #009900;]{[color= #cc66cc;]0[color= #339933;], [color= #cc66cc;]2[color= #339933;], [color= #cc66cc;]6[color= #009900;]}[color= #009900;])

Dim Lines[color= #009900;]([color= #009900;]) [color= #b1b100;]As String [color= #339933;]= [color= #990000;]File[color= #339933;].ReadAllLines[color= #009900;]( FileName [color= #009900;])

Dim SWriter [color= #b1b100;]As [color= #000000; font-weight: bold;]New StreamWriter[color= #009900;]( FileName[color= #339933;], [color= #000000; font-weight: bold;]False [color= #009900;])

Dim CurrentIndex [color= #b1b100;]As Integer [color= #339933;]= [color= #cc66cc;]0

[color= #b1b100;]Do [color= #b1b100;]While CurrentIndex [color= #339933;]<= UBound[color= #009900;]( Lines [color= #009900;])

[color= #b1b100;]If Not Indices[color= #339933;].Contains[color= #009900;]( CurrentIndex [color= #009900;]) Then SWriter[color= #339933;].WriteLine[color= #009900;]( Lines[color= #009900;]( CurrentIndex  [color= #009900;]) [color= #009900;])

CurrentIndex [color= #339933;]+= [color= #cc66cc;]1

Loop

SWriter[color= #339933;].Close

Process[color= #339933;].Start[color= #009900;]( FileName [color= #009900;])[color= #0000ff;]' لفتح الملف 
575;لمعدل 

[color= #0000ff;]End Sub

[color= #0000ff;] 

[color= #0000ff;] 

[color= #0000ff;] 

[color= #0000ff;]

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

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

الكود مش واضح <<<<< :blush: حسيت كذه

حتا أنا من الذكاء حاطه comment زي طريقة الجافا بدل ماأحط كذه ' '

بــــــــس باين عليك فهمت مرحلتي المبتدئة

وأنا كان سؤالي إني سويت زي ماقلت بس ماظبط

راح أحاول إن شاء الله

طيب ألف شكر وتعبناك معانا

وجمعـــــــــــــه مباركه للجميع

تم تعديل هذه المشاركة بواسطة shada2009 في 2 يناير 2009 في 14:07

#4

اي Code مش واضح ؟

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

عذراً, بالنسبة لـ ( حذف مجموعة من سطور ) علينا استخدام ( ListOf( Integer) ), سأعدل الـ Code .

The important thing is not so much

that every child should be taught,

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

#6

خلاص شـــــــــــكرا

عرفت خطأي وعدلته

خطأي كان إني ماحذفت الفايل وبعدين أنشأته وعبيته من الأرري عشان أتجاهل اللاين المحذوف

إن شاء الله كان خطأي واضح مو زي الكود

:happy:

شكرا مررررررررررررررة بشــــــــــار

والله يزيدك من فضله >>> آمين في عصر الجمعة

#7
shada2009 كتب:
' 1st way ( prefer this ) :

str = File.ReadAllLines(fileName & ".txt")

' 2nd way :

While fle.Peek <> -1

str(index) = fle.ReadLine() // store in array to do delete

index = index + 1

End While

fle.Close()

كنصيحة اخيرة, عليك استخدام طريقة من الطرق المذكورة .

The important thing is not so much

that every child should be taught,

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

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…