 |
|
ทำให้ array loop check data ใน text file 1000 รอบ ครับ หน้าแสดงผมคือ Form_Load |
|
 |
|
|
 |
 |
|
Code (C#)
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace CheckStock1
{
public partial class SR : Form
{
public string A { get; set; }
public string B { get; set; }
public SR(string data,string data2)
{
InitializeComponent();
A = data;
B = data2;
}
private void SR_Load(object sender, EventArgs e)
{
textBox1.Focus();
textBox3.Text = "0";
}
private FileInfo f;
private void button1_Click_1(object sender, EventArgs e)
{
Menu m1 = new Menu();
m1.Show();
this.Hide();
}
private List<string> array = new List<string>();
string C = DateTime.Now.ToString("dd/MM/yyyy");
private void textBox1_KeyDown_1(object sender,KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Text.Length >= 15 || textBox1.Text.Length < 5)
{
MessageBox.Show("Number Invalid");
textBox1.Text = "";
return;
}
string str = ((TextBox) sender).Text;
if (!array.Contains(str))
{
array.Add(str);
int Number = Convert.ToInt32(textBox3.Text.ToString());
string nb = (++Number).ToString();
textBox3.Text = nb;
textBox2.Text = textBox1.Text.Substring(0, 4);
label5.Text = textBox1.Text;
string log = "CheckStock.txt";
this.textBox1.Multiline = true;
if (File.Exists(log) == true)
{
f = new FileInfo(log);
StreamWriter sw = f.AppendText();
sw.WriteLine(C + "\t" + A + "\t" + B + "\t" + textBox1.Text + "\t" + textBox2.Text + "\t" + "1");
textBox1.Text = "";
sw.Close();
}
else if (File.Exists(log) == false)
{
f = new FileInfo(log);
StreamWriter sw = f.CreateText();
sw.WriteLine(C + "\t" + A + "\t" + B + "\t" + textBox1.Text + "\t" + textBox2.Text + "\t" + "1");
textBox1.Text = "";
sw.Close();
}
}
else
{
MessageBox.Show("Scan Again");
textBox1.Text = "";
return;
}
}
}
}
}
อันนี้โค้ดของผมครับ ไม่รุจะใส่array ยังไง
|
 |
 |
 |
 |
Date :
2016-06-28 11:19:17 |
By :
bankzbox1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
for (i=0 ; i<=1000 ; i++)
{
//นำเงื่อนไขมาใส่ครับ
}
|
 |
 |
 |
 |
Date :
2016-06-28 11:27:42 |
By :
bigsuntat |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
File.OpenText(filetxt).ReadToEnd()
แนะนำนิดครับ
ไม่ควรเก็บใน array แต่ให้ foreach เพื่อเช็คดีกว่า
ประมาณว่า
Code (C#)
private bool Check(string input)
{
bool b = false;
foreach (string s in System.IO.File.ReadAllText(@"TextFile1.txt").Trim())
{
if (s == input)
{
b = true;
return b;
}
}
return b;
}
Code (C#)
string str = ((TextBox) sender).Text;
if(!Check(str))
{
using (StreamWriter sw =!System.IO.File.Exists(@"TextFile1.txt")? System.IO.File.CreateText(@"TextFile1.txt"):new StreamWriter(filelst)) { sw.WriteLine("5555"); }
}
โค้ดบ้านๆแค่นี้ก็ครบแล้วละครับ
ทั้งเช็ค ทั้งเพิ่ม
|
 |
 |
 |
 |
Date :
2016-06-28 15:27:56 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error
บันทัดไหนรึ
|
 |
 |
 |
 |
Date :
2016-06-29 10:33:51 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แปลกนะครับ
.ReadToEnd() ต้องออกมาเป็น string ใน txt ก็เป็น string แต่กลับ
error ขึ้นมา cannot convert type char to string
รึว่าใน txt จัดรูปแบบไฟล์ที่ไม่ใช่ string รึครับ
แต่ยังไงก็ตามลองดึงมาจากเจ้า StreamReader ซึ่งเป็นแม่แบบของ ReadAllText ดูครับ
https://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx
Code (C#)
using (StreamReader sr = new StreamReader(@"CheckStock.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if(line ==input)
{
b = true;
return b;
}
}
}
ถ้ายัง error อยู่ น่าจะเป็นที่รูปแบบไฟล์ ครับ
|
 |
 |
 |
 |
Date :
2016-06-29 11:01:44 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรง .ReadToEnd()
error ขึ้นมา cannot convert type char to string
รู้แล้วครับ
ผมผิดเอง ตอนผมลองผมใช้ richTextBox ซึ่งมัน สามารถทำเป็น richTextBox1.Lines ได้
งั้นผมขอลองแก้โค้ดแรกดูนะครับ
Code (C#)
private bool Check(string input)
{
bool b = false;
foreach (string s in System.IO.File.ReadAllText(@"TextFile1.txt").Trim().ToList<string>)
{
if (s == input)
{
b = true;
return b;
}
}
return b;
}
ประมาณว่าเอา string มาทำเป็น array เลย แทนการใช้
private List<string> array = new List<string>();
|
 |
 |
 |
 |
Date :
2016-06-29 11:24:34 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตามมุมมองไม่จำเป็นต้องใส่ใน SR_Load ครับ
เพราะเราคงไม่เช็คตั้งแต่เปิดมาอยู่แล้ว
ถ้าหากใช้
Code (C#)
sw.WriteLine(C + "\t" + A + "\t" + B + "\t" + textBox1.Text + "\t" + textBox2.Text + "\t" + "1");
แสดงว่า txt ไฟล์ ก็จะมีข้อมูลเปลี่ยนไปเรื่อยๆอยู่แล้วจึงไม่มีเหตุผลที่จะเก็บค่าใน SR_Load ให้หนักแรม
*** นอกจากเหตุผลอื่นที่สมเหตุสมผล มากกว่าการเช็คค่าต่างๆดังที่เห็นในโค้ด ครับ
ส่วนถ้าอยากเอาไปใส่จริงๆ ก็แค่
Code (C#)
string s = System.IO.File.OpenText(@"CheckStock.txt").ReadToEnd();
สำหรับ string
หรือ
Code (C#)
private List<string> s = System.IO.File.OpenText(@"CheckStock.txt").ReadToEnd().ToList<string>;
สำหรับ array
|
 |
 |
 |
 |
Date :
2016-06-29 14:35:48 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
รายละเอียดการทำงานของหน้านี้คือ
เข้ามาหน้านี้ จะทำการเช็คข้อมูลที่อยู่ใน text file "CheckStock.txt" จะเป็นการเช็คข้อมูลทีละบรรทัดจนครบทุกบรรทัดครับ
แล้วก็เริ่มสแกนตามปกติครับ ภายในช่องสแกน จะไม่สามารถสแกนซ้ำข้อมูลกับที่เคยสแกนไปก่อนแล้วได้ ข้อมูลที่จะเช็คว่าซ้ำไหม ไม่ได้เช็คที่text file ครับ เช็คที่ สแกนลงไปใน textbox ถ้าซ้ำกับที่สแกนไปจะเด้ง error ครับ ถ้าสแกนสำเร็จก็จะบันทึกข้อมูลลง text ไฟล์เลยครับ
|
 |
 |
 |
 |
Date :
2016-06-29 14:37:11 |
By :
bankzbox1 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เข้ามาหน้านี้ จะทำการเช็คข้อมูลที่อยู่ใน text file "CheckStock.txt" จะเป็นการเช็คข้อมูลทีละบรรทัดจนครบทุกบรรทัดครับ
หากขั้นตอนเหมือน ใน
Code (C#)
private void textBox1_KeyDown_1(object sender,KeyEventArgs e)
หรือแค่เป็นการเก็บค่าเข้ามาเฉยๆ
ก้ไม่จำเป็นต้องเช็คก็ได้ครับ(แต่หากไม่เหมือน ค่อยว่ากันอีกทีครับ)
เพราะยังไงทุกครั้งที่ใช้
Code (C#)
private bool Check(string input)
ทั้งนี้ผมก็ไม่แน่ใจในวัตถุประสงค์ของ
เข้ามาหน้านี้ จะทำการเช็คข้อมูลที่อยู่ใน text file "CheckStock.txt" จะเป็นการเช็คข้อมูลทีละบรรทัดจนครบทุกบรรทัดครับ
เช่นกัน ว่าเก็บไว้ทำไม ครับ ในเมื่อเราต้องนำเข้าข้อมูลจาก txt ใหม่ตลอดอยู่แล้วทุกครั้งที่เช็คครับ
|
 |
 |
 |
 |
Date :
2016-06-29 14:45:32 |
By :
lamaka.tor |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|