兄弟我正在看比你上面提到版本更新的這本書 Visual Basic 2008 入門經(jīng)典(第5版) 2005(第4版) 2003(第3版)
10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有遼源免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
內(nèi)容基本差不多,就是改頭換面換鈔票,不過建議使用與軟件的版本一至。
我用VB2008 書的版本不宜低于2005
另外:
這書是國(guó)外著名編程出版社出版的圖書(Wrok),唯一遺憾的是代碼里面的注釋沒有翻譯,真是太缺德了(清華出版社 出版)
如果是初學(xué)者建議學(xué)國(guó)人編的書,推薦 《精通Visual Basic.NET中文版》網(wǎng)上到處都是免費(fèi)下載 這本書寫得很詳細(xì)
public?static?String?reverse1(String?str)
{
return?new?StringBuffer(str).reverse().toString();
}
2.最常用的方法:
public?static?String?reverse3(String?s)
{?
char[]?array?=?s.toCharArray();?
String?reverse?=?"";??//注意這是空串,不是null
for?(int?i?=?array.length?-?1;?i?=?0;?i--)?
reverse?+=?array[i];?
return?reverse;?
}?
3.常用方法的變形:
public?static?String?reverse2(String?s)
{?
int?length?=?s.length();?
String?reverse?=?"";??//注意這是空串,不是null
for?(int?i?=?0;?i??length;?i++)?
reverse?=?s.charAt(i)?+?reverse;//在字符串前面連接,??而非常見的后面
return?reverse;?
}?
4.C語(yǔ)言中常用的方法:
public?static?String?reverse5(String?orig)
{?
char[]?s?=?orig.toCharArray();?
int?n?=?s.length?-?1;?
int?halfLength?=?n?/?2;?
for?(int?i?=?0;?i?=?halfLength;?i++)?{?
char?temp?=?s[i];?
s[i]?=?s[n?-?i];?
s[n?-?i]?=?temp;?
}?
return?new?String(s);??//知道??char數(shù)組和String相互轉(zhuǎn)化
}
就可以了,我有一段不太成熟的代碼:
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Public Class Form1
Dim TCPListenerRun, TCPClientRun As Boolean
Dim ControlTCPClient, ServerClient As TcpClient
Dim ControlTCPListener As TcpListener
Dim TCPClientT, TCPListenerT As Thread
Private ControlStream, SControlStream As NetworkStream
Private ControlReadStream, SControlReadStream As StreamReader
Private ControlWriteStream, SControlWriteStream As StreamWriter
'異步委托
Private Delegate Sub InvokeDelegate(ByVal iMessage As String)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If TCPListenerRun Then
TCPListenerT.Abort()
ControlTCPListener.Stop()
TCPListenerRun = False
Button1.Text = "啟動(dòng)服務(wù)端 "
TextBox2.AppendText( "服務(wù)器已停止! " vbCrLf)
Else
ControlTCPListener = New TcpListener(IPAddress.Parse( "127.0.0.1 "), 13000)
ControlTCPListener.Start()
TCPListenerT = New Thread(AddressOf Listen)
TCPListenerT.Start()
TextBox2.AppendText( "等待連接... " vbCrLf)
TextBox2.AppendText( "服務(wù)器已啟動(dòng)! " vbCrLf)
TCPListenerRun = True
Button1.Text = "停止服務(wù)端 "
End If
Catch ex As Exception
TextBox2.AppendText(ex.Message vbCrLf)
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
If TCPClientRun Then
TCPClientT.Abort()
ControlTCPClient.Close()
TCPClientRun = False
Button2.Text = "啟動(dòng)客戶端 "
Else
ControlTCPClient = New TcpClient(New IPEndPoint(IPAddress.Parse( "127.0.0.1 "), 13001))
ControlTCPClient.Connect(New IPEndPoint(IPAddress.Parse( "127.0.0.1 "), 13000))
TCPClientRun = True
Button2.Text = "停止客戶端 "
TCPClientT = New Thread(AddressOf Receive)
TCPClientT.Start()
ControlStream = ControlTCPClient.GetStream()
ControlReadStream = New StreamReader(ControlStream)
ControlWriteStream = New StreamWriter(ControlStream)
End If
Catch ex As SocketException
'If ex.ErrorCode = 10048 Then
ControlTCPClient.Close()
'End If
TCPClientRun = False
Button2.Text = "啟動(dòng)客戶端 "
TextBox2.AppendText(ex.Message vbCrLf)
End Try
End Sub
Private Sub Listen()
ServerClient = ControlTCPListener.AcceptTcpClient
TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), "成功連接... ")
SControlStream = ServerClient.GetStream()
SControlReadStream = New StreamReader(SControlStream)
SControlWriteStream = New StreamWriter(SControlStream)
While True
TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), SControlReadStream.ReadLine())
End While
End Sub
Private Sub Receive()
While True
TextBox2.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod_Receive), ControlReadStream.ReadLine())
'Console.WriteLine(ControlTCPClient.Available)
End While
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TCPListenerRun = False
TCPClientRun = False
End Sub
Private Sub InvokeMethod_Receive(ByVal iMessage As String)
TextBox2.AppendText(iMessage vbCrLf)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ControlWriteStream.WriteLine(TextBox1.Text)
ControlWriteStream.Flush()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
SControlWriteStream.WriteLine(TextBox3.Text)
SControlWriteStream.Flush()
End Sub
End Class
使用FileStream讀寫文件
文件頭:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
讀文件核心代碼:
byte[] byData = new byte[100];
char[] charData = new char[1000];
try
{
FileStream sFile = new FileStream("文件路徑",FileMode.Open);
sFile.Seek(55, SeekOrigin.Begin);
sFile.Read(byData, 0, 100); //第一個(gè)參數(shù)是被傳進(jìn)來(lái)的字節(jié)數(shù)組,用以接受FileStream對(duì)象中的數(shù)據(jù),第2個(gè)參數(shù)是字節(jié)數(shù)組中開始寫入數(shù)據(jù)的位置,它通常是0,表示從數(shù)組的開端文件中向數(shù)組寫數(shù)據(jù),最后一個(gè)參數(shù)規(guī)定從文件讀多少字符.
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadLine();
return;
}
Decoder d = Encoding.UTF8.GetDecoder();
d.GetChars(byData, 0, byData.Length, charData, 0);
Console.WriteLine(charData);
Console.ReadLine();
寫文件核心代碼:
FileStream fs = new FileStream(文件路徑,FileMode.Create);
//獲得字節(jié)數(shù)組
byte [] data =new UTF8Encoding().GetBytes(String);
//開始寫入
fs.Write(data,0,data.Length);
//清空緩沖區(qū)、關(guān)閉流
fs.Flush();
fs.Close();
2、使用StreamReader和StreamWriter
文件頭:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
StreamReader讀取文件:
StreamReader objReader = new StreamReader(文件路徑);
string sLine="";
ArrayList LineList = new ArrayList();
while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null!sLine.Equals(""))
LineList.Add(sLine);
}
objReader.Close();
return LineList;
StreamWriter寫文件:
FileStream fs = new FileStream(文件路徑, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//開始寫入
sw.Write(String);
//清空緩沖區(qū)
sw.Flush();
//關(guān)閉流
sw.Close();
fs.Close();
===================================================================================
方式一:用FileStream
//實(shí)例化一個(gè)保存文件對(duì)話框
SaveFileDialog sf = new SaveFileDialog();
//設(shè)置文件保存類型
sf.Filter = "txt文件|*.txt|所有文件|*.*";
//如果用戶沒有輸入擴(kuò)展名,自動(dòng)追加后綴
sf.AddExtension = true;
//設(shè)置標(biāo)題
sf.Title = "寫文件";
//如果用戶點(diǎn)擊了保存按鈕
if(sf.ShowDialog()==DialogResult.OK)
{
//實(shí)例化一個(gè)文件流---與寫入文件相關(guān)聯(lián)
FileStream fs = new FileStream(sf.FileName,FileMode.Create);
//獲得字節(jié)數(shù)組
byte [] data =new UTF8Encoding().GetBytes(this.textBox1.Text);
//開始寫入
fs.Write(data,0,data.Length);
//清空緩沖區(qū)、關(guān)閉流
fs.Flush();
fs.Close();
}
方式二:用StreamWriter
//實(shí)例化一個(gè)保存文件對(duì)話框
SaveFileDialog sf = new SaveFileDialog();
//設(shè)置文件保存類型
sf.Filter = "txt文件|*.txt|所有文件|*.*";
//如果用戶沒有輸入擴(kuò)展名,自動(dòng)追加后綴
sf.AddExtension = true;
//設(shè)置標(biāo)題
sf.Title = "寫文件";
//如果用戶點(diǎn)擊了保存按鈕
if (sf.ShowDialog() == DialogResult.OK)
{
//實(shí)例化一個(gè)文件流---與寫入文件相關(guān)聯(lián)
FileStream fs = new FileStream(sf.FileName, FileMode.Create);
//實(shí)例化一個(gè)StreamWriter--與fs相關(guān)聯(lián)
StreamWriter sw = new StreamWriter(fs);
//開始寫入
sw.Write(this.textBox1.Text);
//清空緩沖區(qū)
sw.Flush();
//關(guān)閉流
sw.Close();
fs.Close();
}
string FileName = Guid.NewGuid().ToString() + ".txt"; //GUID生成唯一文件名
StringBuilder ckpw = new StringBuilder("\"憑證輸出\", \"V800\", \"001\", \"東風(fēng)隨州專用汽車有限公司\"," + "\"F89自由項(xiàng)16\", \"F90審核日期:\"");
if (!FileIO.IsFolderExists(Server.MapPath("pzsc")))
FileIO.CreaterFolder(Server.MapPath(""), "");
string filePath = Server.MapPath("pzsc") + "\\" + FileName;
System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, false, Encoding.GetEncoding("GB2312"));//創(chuàng)建的時(shí)候需要指定編碼格式,默認(rèn)是UTF-8,中文顯示亂碼
sw.WriteLine(ckpw.ToString());
sw.Close();
方式三:用BinaryWriter
//實(shí)例化一個(gè)保存文件對(duì)話框
SaveFileDialog sf = new SaveFileDialog();
//設(shè)置文件保存類型
sf.Filter = "txt文件|*.txt|所有文件|*.*";
//如果用戶沒有輸入擴(kuò)展名,自動(dòng)追加后綴
sf.AddExtension = true;
//設(shè)置標(biāo)題
sf.Title = "寫文件";
//如果用戶點(diǎn)擊了保存按鈕
if (sf.ShowDialog() == DialogResult.OK)
{
//實(shí)例化一個(gè)文件流---與寫入文件相關(guān)聯(lián)
FileStream fs = new FileStream(sf.FileName, FileMode.Create);
//實(shí)例化BinaryWriter
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(this.textBox1.Text);
//清空緩沖區(qū)
bw.Flush();
//關(guān)閉流
bw.Close();
fs.Close();
}
C#緩存流示例------用緩存流復(fù)制文件
C#文件處理操作必須先導(dǎo)入命名空間:using System.IO;
背景:使用VS2005、一個(gè)按鈕、一個(gè)窗體、C#緩存流、把D:\KuGoo\愛得太多.wma復(fù)制到D:\并更名為love.wma,即:D:\love.wma
在按鈕的Click事件中添加如下代碼:
private void button1_Click(object sender, EventArgs e)
{
//創(chuàng)建兩個(gè)文件流 一個(gè)是源文件相關(guān),另一個(gè)是要寫入的文件
FileStream fs = new FileStream(@"D:\KuGoo\愛得太多.wma",FileMode.Open);
FileStream fs2 = new FileStream(@"D:\love.wma",FileMode.Create);
//創(chuàng)建一個(gè)字節(jié)數(shù)組,作為兩者之間的媒介
//好比兩個(gè)人拿蘋果,這個(gè)字節(jié)數(shù)組就好比一個(gè)籃子,一個(gè)人作死的把蘋果送到籃子里面,
//而我就可以作死得拿蘋果,通過這個(gè)媒介我們互不干擾,
//不需要互相等待【她往籃子里面放了蘋果我才可以去拿】,提高了效率
byte[] data = new byte[1024];
//創(chuàng)建兩個(gè)緩沖流,與兩個(gè)文件流相關(guān)聯(lián)
BufferedStream bs = new BufferedStream(fs);
BufferedStream bs2= new BufferedStream(fs2);
//fs作死的讀,fs2作死的寫,直到fs沒有字節(jié)可讀fs2就不寫了
//好比,一個(gè)人作死的往籃子里面丟蘋果,另一個(gè)人作死得往籃子里面拿蘋果,直到籃子里面沒有蘋果拿了為止
//即--那個(gè)人沒有蘋果往籃子里面放了
while(fs.Read(data,0,data.Length)0)
{
fs2.Write(data,0,data.Length);
fs2.Flush();
}
//關(guān)閉流,好比兩個(gè)人累了,都要休息 呵呵o(∩_∩)o...
fs.Close();
fs2.Close();
}
C#內(nèi)存流示例-----用內(nèi)存流來(lái)讀取圖片
C#文件處理操作必須先導(dǎo)入命名空間:using System.IO;
背景:一個(gè)窗體、一個(gè)pictureBox、一個(gè)lable[沒有選擇圖片,lable的text為"圖片未選擇"],在pictureBox1的Click事件中添加如下代碼:
private void pictureBox1_Click(object sender, EventArgs e)
{
//實(shí)例化一個(gè)打開文件對(duì)話框
OpenFileDialog op = new OpenFileDialog();
//設(shè)置文件的類型
op.Filter = "JPG圖片|*.jpg|GIF圖片|*.gif";
//如果用戶點(diǎn)擊了打開按鈕、選擇了正確的圖片路徑則進(jìn)行如下操作:
if(op.ShowDialog()==DialogResult.OK)
{
//清空文本
this.label1.Text = "";
//實(shí)例化一個(gè)文件流
FileStream fs = new FileStream(op.FileName, FileMode.Open);
//把文件讀取到字節(jié)數(shù)組
byte[] data = new byte[fs.Length];
fs.Read(data, 0, data.Length);
fs.Close();
//實(shí)例化一個(gè)內(nèi)存流---把從文件流中讀取的內(nèi)容[字節(jié)數(shù)組]放到內(nèi)存流中去
MemoryStream ms = new MemoryStream(data);
//設(shè)置圖片框 pictureBox1中的圖片
this.pictureBox1.Image = Image.FromStream(ms);
}
}
因?yàn)閒oreach是通過迭代來(lái)工作的,當(dāng)你刪除了一個(gè)元素后,這個(gè)集合內(nèi)部元素之間的關(guān)系就會(huì)被破壞,所以你得不到正確的結(jié)果
你最好用for循環(huán),或者先將要?jiǎng)h除的控件放到一個(gè)數(shù)組中,foreach結(jié)束之后,統(tǒng)一刪除,
順序不對(duì)吧,先查詢?cè)俅蜷_數(shù)據(jù)庫(kù)?
Dim myconn As New SqlConnection("Initial Catalog=school;Data Source=(Local);" "Integrated Security=SSPI;")
myconn.Open() '先打開數(shù)據(jù)庫(kù)
Dim sql As String = "Select * From T_Student Where BookCode='" TextBox3.Text "'"
Dim mycmd As New SqlCommand(sql, myconn) '再查詢
Dim myread As SqlDataReader