2013-07-25 21 views
-4

Iam new in programming.In Windows窗体应用程序中,我希望用户可以在文本框中编写URL并在列表框中添加(使用按钮)作为收藏夹列表,然后用户可以单击在列表框中,然后去浏览器,finaly可以保存并打开列表?缺少Microsoft Sql数据库。我需要一个源代码。将文本框中的URL添加到Windows窗体应用程序中的列表框中#

文本框(输入您的网站):www.google.com

按钮:添加到列表框中

列表框:WWW.Google.com

按钮:保存

按钮:打开

回答

1

如果不想使用数据库,则必须将ListBox.Items保存为纯文本文件。没有约束力,如果你开始深入研究,这个简单的问题会变得有点讨厌。这里是你可能需要的解决方案之一:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    int lastIndex = -1; 
    bool suppressSelectedIndexChanged; 
    //Click event handler for buttonAdd 
    private void buttonAdd_Click(object sender, EventArgs e) 
    { 
     listBox1.Items.Add(textBox1.Text); 
     suppressSelectedIndexChanged = true; 
     listBox1.SelectedIndex = listBox1.Items.Count - 1; 
     suppressSelectedIndexChanged = false; 
    } 
    //Click event handler for buttonRemove 
    private void buttonRemove_Click(object sender, EventArgs e) 
    { 
     if (listBox1.SelectedIndices.Count == 0) return; 
     int k = listBox1.SelectedIndices[0]; 
     suppressSelectedIndexChanged = true; 
     for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--) 
      listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]); 
     suppressSelectedIndexChanged = false; 
     lastIndex = -1; 
     listBox1.SelectedIndex = k < listBox1.Items.Count ? k : listBox1.Items.Count - 1; 
     if (listBox1.Items.Count == 0) textBox1.Clear(); 
    } 
    //Click event handler for buttonSave 
    private void buttonSave_Click(object sender, EventArgs e) 
    {    
     SaveFileDialog save = new SaveFileDialog(); 
     save.Filter = "URLs file|*.urls"; 
     save.FileOk += (s, ev) => 
     { 
      using (StreamWriter writer = File.CreateText(save.FileName)) 
      { 
       foreach (object item in listBox1.Items) 
       { 
        writer.WriteLine(item.ToString()); 
       } 
      } 
     }; 
     save.ShowDialog(); 
    } 
    //Click event handler for buttonOpen 
    private void buttonOpen_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog open = new OpenFileDialog(); 
     open.Filter = "URLs file|*.urls"; 
     open.FileOk += (s, ev) => 
     { 
      listBox1.Items.Clear(); 
      using (StreamReader reader = File.OpenText(open.FileName)) 
      { 
       string line = ""; 
       while ((line = reader.ReadLine()) != null) 
       { 
        listBox1.Items.Add(line); 
       } 
      } 
     }; 
     open.ShowDialog(); 
    } 
    //SelectedIndexChanged event handler for listBox1 
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (suppressSelectedIndexChanged) return; 
     if (lastIndex > -1) 
     { 
      listBox1.Items[lastIndex] = textBox1.Text;     
     }    
     lastIndex = listBox1.SelectedIndex; 
     if (listBox1.SelectedIndex > -1) 
      textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString(); 
    } 
    //Click event handler for buttonVisit 
    private void buttonVisit_Click(object sender, EventArgs e) 
    { 
     if (listBox1.SelectedItem == null) return; 
     System.Diagnostics.Process.Start(listBox1.SelectedItem.ToString()); 
    } 
} 

这里是GUI截屏让你知道需要哪些控件: enter image description here

+0

非常感谢你很多,我真的很感激你。请你在WPF中给我这个源代码。 –

+0

@BehdadBitimax我还在学习WPF,但是这个应用程序非常简单,所以我可以在这里做一个简单的演示http://www.mediafire.com/?xizbeb4kjzl8mk9因为我还在学习WPF,所以有一些'winforms '在我的代码风格。您需要'VS 2010'或更高版本来运行演示。 –

+0

我下载了它,太棒了,希望以后能帮到你。 –

0
 private void btnAdd_Click(object sender, EventArgs e) 
    { 
     string _webstring = @"http://"; 
     string _website = _webstring + textBox1.Text; 
     listBox1.Items.Add(_website); 

     using (StreamWriter w = File.AppendText("websites.txt")) 
     { 
       WriteLog(_website, w); 

     } 
     using (StreamReader r = File.OpenText("websites.txt")) 
     { 
      DisposeLog(r); 
     } 
    } 

    private void btnLaunch_Click(object sender, EventArgs e) 
    { 
     System.Diagnostics.Process.Start("iexplore.exe", listBox1.SelectedItem.ToString()); 
    } 

    private void btnSave_Click(object sender, EventArgs e) 
    { 
     using (StreamWriter w = File.AppendText("websites.txt")) 
     { 
      foreach (string items in listBox1.Items) 
      { 
       WriteLog(items, w); 
      } 
     } 
     using (StreamReader r = File.OpenText("websites.txt")) 
     { 
      DisposeLog(r); 
     } 
    } 


    public static void WriteLog(string logMessage, TextWriter w) 
    { 
     w.WriteLine(logMessage, logMessage); 
    } 

    public static void DisposeLog(StreamReader r) 
    { 
     string line; 
     while ((line = r.ReadLine()) != null) 
     { 
      Console.WriteLine(line); 
     } 
    } 

    private void btnRetrieve_Click(object sender, EventArgs e) 
    { 
     using (TextReader txtRead = File.OpenText("Websites.txt")) 
     { 
      string _text = ""; 
      string[] _textArray = null; 
      while ((_text = txtRead.ReadLine()) != null) 
      { 
       _textArray = _text.Split('\t'); 
       listBox1.Items.Add(txtRead.ReadLine()); 
      } 
      txtRead.Close(); 
     } 
    } 

希望这可以帮助..谢谢

+0

非常感谢你,我真的很感谢你 –

相关问题