Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
AnswerRe: Chat Building - transfering data between server and client Pin
Reelix6-May-08 21:10
Reelix6-May-08 21:10 
QuestionGetting an Angle for Rotations Pin
RedHotFunk6-May-08 14:28
RedHotFunk6-May-08 14:28 
AnswerRe: Getting an Angle for Rotations Pin
Christian Graus6-May-08 15:42
protectorChristian Graus6-May-08 15:42 
GeneralRe: Getting an Angle for Rotations Pin
RedHotFunk7-May-08 16:00
RedHotFunk7-May-08 16:00 
AnswerRe: Getting an Angle for Rotations Pin
Reelix6-May-08 21:26
Reelix6-May-08 21:26 
QuestionMdi Child Pin
hadad6-May-08 12:53
hadad6-May-08 12:53 
AnswerRe: Mdi Child Pin
Anindya Chatterjee6-May-08 18:52
Anindya Chatterjee6-May-08 18:52 
QuestionC# background Worker and Progress Bar help Pin
Relentless6-May-08 11:35
Relentless6-May-08 11:35 
Hi All,

Ive been doing alot of ready about backgroud workers and would like to learn how they work and get one into my small simple application i am building. The problem is that i am finding it hard to understand how to get it into my applciation and how they really work. I was wondering if someone could point me in the direction or even give me an example of it working in my code!

bear in mined that i am a beginner to programming, not just C# so any help would be very much appreciated!

My Port Scanner Application:

[Form1.cs]

using System;<br />
using System.Collections.Generic;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Net.Sockets;<br />
using System.IO;<br />
<br />
namespace GUI_Port_Scanner<br />
{<br />
    public partial class Form1 : Form<br />
    {<br />
        string _ip;<br />
        int _startPort;<br />
        int _endPort;<br />
        string fileName = "scan_results.txt";<br />
        TcpClient TcpScan = new TcpClient();<br />
<br />
        public Form1()<br />
        {<br />
            InitializeComponent();<br />
        }<br />
<br />
        private void Form1_Load(object sender, EventArgs e)<br />
        {<br />
            _ip = "";<br />
            _startPort = 0;<br />
            _endPort = 0;<br />
        }<br />
<br />
        private void startScanBtt_Click(object sender, EventArgs e)<br />
        {<br />
            <br />
            if (prevItems.Checked == true)<br />
            {<br />
                listBoxClosed.Items.Clear();<br />
                listBoxOpen.Items.Clear();<br />
            }<br />
<br />
            _ip = hostTxt.Text;<br />
            _startPort = Int32.Parse(startPortTxt.Text);<br />
            _endPort = Int32.Parse(endPortTxt.Text);<br />
<br />
            for (int _currentPort = _startPort; _currentPort <= _endPort; _currentPort++)<br />
            {<br />
                try<br />
                {<br />
                    TcpScan.Connect(_ip, _currentPort);<br />
                    listBoxOpen.Items.Add(" => Port " + _currentPort + " is open.");<br />
                }<br />
                catch<br />
                {<br />
                    listBoxClosed.Items.Add(" => Port " + _currentPort + " is closed.");<br />
                }<br />
            }<br />
<br />
            toFileBtt.Enabled = true;<br />
        }<br />
<br />
        private void toFileBtt_Click(object sender, EventArgs e)<br />
        {<br />
            StreamWriter writeTo = new StreamWriter(fileName, true);<br />
            if (listBoxOpen.Items.Count != 0)<br />
            {<br />
                writeTo.WriteLine();<br />
                writeTo.WriteLine("   Open Ports:");<br />
                writeTo.WriteLine("*-----------------------------*");<br />
            }<br />
            foreach (object item in listBoxOpen.Items)<br />
            {<br />
                writeTo.WriteLine(item.ToString());<br />
            }<br />
            if (listBoxClosed.Items.Count != 0)<br />
            {<br />
                writeTo.WriteLine();<br />
                writeTo.WriteLine("   Closed Ports:");<br />
                writeTo.WriteLine("*-----------------------------*");<br />
            }<br />
            foreach (object item in listBoxClosed.Items)<br />
            {<br />
                writeTo.WriteLine(item.ToString());<br />
            }<br />
            writeTo.Close();<br />
        }<br />
    }<br />
}


[Form1.Designer.cs]

namespace GUI_Port_Scanner<br />
{<br />
    partial class Form1<br />
    {<br />
        /// <summary><br />
        /// Required designer variable.<br />
        /// </summary><br />
        private System.ComponentModel.IContainer components = null;<br />
<br />
        /// <summary><br />
        /// Clean up any resources being used.<br />
        /// </summary><br />
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param><br />
        protected override void Dispose(bool disposing)<br />
        {<br />
            if (disposing && (components != null))<br />
            {<br />
                components.Dispose();<br />
            }<br />
            base.Dispose(disposing);<br />
        }<br />
<br />
        #region Windows Form Designer generated code<br />
<br />
        /// <summary><br />
        /// Required method for Designer support - do not modify<br />
        /// the contents of this method with the code editor.<br />
        /// </summary><br />
        private void InitializeComponent()<br />
        {<br />
            this.hostTxt = new System.Windows.Forms.TextBox();<br />
            this.startPortTxt = new System.Windows.Forms.TextBox();<br />
            this.label1 = new System.Windows.Forms.Label();<br />
            this.label2 = new System.Windows.Forms.Label();<br />
            this.endPortTxt = new System.Windows.Forms.TextBox();<br />
            this.label3 = new System.Windows.Forms.Label();<br />
            this.startScanBtt = new System.Windows.Forms.Button();<br />
            this.listBoxOpen = new System.Windows.Forms.ListBox();<br />
            this.listBoxClosed = new System.Windows.Forms.ListBox();<br />
            this.label4 = new System.Windows.Forms.Label();<br />
            this.label5 = new System.Windows.Forms.Label();<br />
            this.prevItems = new System.Windows.Forms.CheckBox();<br />
            this.toFileBtt = new System.Windows.Forms.Button();<br />
            this.SuspendLayout();<br />
            // <br />
            // hostTxt<br />
            // <br />
            this.hostTxt.Location = new System.Drawing.Point(72, 12);<br />
            this.hostTxt.Name = "hostTxt";<br />
            this.hostTxt.Size = new System.Drawing.Size(196, 20);<br />
            this.hostTxt.TabIndex = 1;<br />
            // <br />
            // startPortTxt<br />
            // <br />
            this.startPortTxt.Location = new System.Drawing.Point(72, 38);<br />
            this.startPortTxt.Name = "startPortTxt";<br />
            this.startPortTxt.Size = new System.Drawing.Size(65, 20);<br />
            this.startPortTxt.TabIndex = 2;<br />
            // <br />
            // label1<br />
            // <br />
            this.label1.AutoSize = true;<br />
            this.label1.Location = new System.Drawing.Point(12, 15);<br />
            this.label1.Name = "label1";<br />
            this.label1.Size = new System.Drawing.Size(32, 13);<br />
            this.label1.TabIndex = 3;<br />
            this.label1.Text = "Host:";<br />
            // <br />
            // label2<br />
            // <br />
            this.label2.AutoSize = true;<br />
            this.label2.Location = new System.Drawing.Point(11, 41);<br />
            this.label2.Name = "label2";<br />
            this.label2.Size = new System.Drawing.Size(54, 13);<br />
            this.label2.TabIndex = 4;<br />
            this.label2.Text = "Start Port:";<br />
            // <br />
            // endPortTxt<br />
            // <br />
            this.endPortTxt.Location = new System.Drawing.Point(203, 38);<br />
            this.endPortTxt.Name = "endPortTxt";<br />
            this.endPortTxt.Size = new System.Drawing.Size(65, 20);<br />
            this.endPortTxt.TabIndex = 5;<br />
            // <br />
            // label3<br />
            // <br />
            this.label3.AutoSize = true;<br />
            this.label3.Location = new System.Drawing.Point(144, 42);<br />
            this.label3.Name = "label3";<br />
            this.label3.Size = new System.Drawing.Size(51, 13);<br />
            this.label3.TabIndex = 6;<br />
            this.label3.Text = "End Port:";<br />
            // <br />
            // startScanBtt<br />
            // <br />
            this.startScanBtt.Location = new System.Drawing.Point(274, 12);<br />
            this.startScanBtt.Name = "startScanBtt";<br />
            this.startScanBtt.Size = new System.Drawing.Size(79, 46);<br />
            this.startScanBtt.TabIndex = 7;<br />
            this.startScanBtt.Text = "Scan";<br />
            this.startScanBtt.UseVisualStyleBackColor = true;<br />
            this.startScanBtt.Click += new System.EventHandler(this.startScanBtt_Click);<br />
            // <br />
            // listBoxOpen<br />
            // <br />
            this.listBoxOpen.BackColor = System.Drawing.SystemColors.MenuText;<br />
            this.listBoxOpen.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));<br />
            this.listBoxOpen.ForeColor = System.Drawing.Color.LimeGreen;<br />
            this.listBoxOpen.FormattingEnabled = true;<br />
            this.listBoxOpen.ItemHeight = 16;<br />
            this.listBoxOpen.Location = new System.Drawing.Point(12, 104);<br />
            this.listBoxOpen.Name = "listBoxOpen";<br />
            this.listBoxOpen.Size = new System.Drawing.Size(168, 164);<br />
            this.listBoxOpen.TabIndex = 8;<br />
            // <br />
            // listBoxClosed<br />
            // <br />
            this.listBoxClosed.BackColor = System.Drawing.SystemColors.MenuText;<br />
            this.listBoxClosed.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));<br />
            this.listBoxClosed.ForeColor = System.Drawing.Color.DarkRed;<br />
            this.listBoxClosed.FormattingEnabled = true;<br />
            this.listBoxClosed.ItemHeight = 16;<br />
            this.listBoxClosed.Location = new System.Drawing.Point(186, 104);<br />
            this.listBoxClosed.Name = "listBoxClosed";<br />
            this.listBoxClosed.Size = new System.Drawing.Size(167, 164);<br />
            this.listBoxClosed.TabIndex = 9;<br />
            // <br />
            // label4<br />
            // <br />
            this.label4.AutoSize = true;<br />
            this.label4.Location = new System.Drawing.Point(14, 88);<br />
            this.label4.Name = "label4";<br />
            this.label4.Size = new System.Drawing.Size(63, 13);<br />
            this.label4.TabIndex = 10;<br />
            this.label4.Text = "Open Ports:";<br />
            // <br />
            // label5<br />
            // <br />
            this.label5.AutoSize = true;<br />
            this.label5.Location = new System.Drawing.Point(188, 88);<br />
            this.label5.Name = "label5";<br />
            this.label5.Size = new System.Drawing.Size(69, 13);<br />
            this.label5.TabIndex = 11;<br />
            this.label5.Text = "Closed Ports:";<br />
            // <br />
            // prevItems<br />
            // <br />
            this.prevItems.AutoSize = true;<br />
            this.prevItems.Location = new System.Drawing.Point(73, 65);<br />
            this.prevItems.Name = "prevItems";<br />
            this.prevItems.Size = new System.Drawing.Size(122, 17);<br />
            this.prevItems.TabIndex = 13;<br />
            this.prevItems.Text = "Clear Previous Items";<br />
            this.prevItems.UseVisualStyleBackColor = true;<br />
            // <br />
            // toFileBtt<br />
            // <br />
            this.toFileBtt.Enabled = false;<br />
            this.toFileBtt.Location = new System.Drawing.Point(274, 61);<br />
            this.toFileBtt.Name = "toFileBtt";<br />
            this.toFileBtt.Size = new System.Drawing.Size(79, 23);<br />
            this.toFileBtt.TabIndex = 14;<br />
            this.toFileBtt.Text = "Copy to File";<br />
            this.toFileBtt.UseVisualStyleBackColor = true;<br />
            this.toFileBtt.Click += new System.EventHandler(this.toFileBtt_Click);<br />
            // <br />
            // Form1<br />
            // <br />
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);<br />
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br />
            this.ClientSize = new System.Drawing.Size(365, 347);<br />
            this.Controls.Add(this.toFileBtt);<br />
            this.Controls.Add(this.prevItems);<br />
            this.Controls.Add(this.label5);<br />
            this.Controls.Add(this.label4);<br />
            this.Controls.Add(this.listBoxClosed);<br />
            this.Controls.Add(this.listBoxOpen);<br />
            this.Controls.Add(this.startScanBtt);<br />
            this.Controls.Add(this.label3);<br />
            this.Controls.Add(this.endPortTxt);<br />
            this.Controls.Add(this.label2);<br />
            this.Controls.Add(this.label1);<br />
            this.Controls.Add(this.startPortTxt);<br />
            this.Controls.Add(this.hostTxt);<br />
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;<br />
            this.Name = "Form1";<br />
            this.Text = "Input | Output - Port Scanner";<br />
            this.Load += new System.EventHandler(this.Form1_Load);<br />
            this.ResumeLayout(false);<br />
            this.PerformLayout();<br />
<br />
        }<br />
<br />
        #endregion<br />
<br />
        private System.Windows.Forms.TextBox hostTxt;<br />
        private System.Windows.Forms.TextBox startPortTxt;<br />
        private System.Windows.Forms.Label label1;<br />
        private System.Windows.Forms.Label label2;<br />
        private System.Windows.Forms.TextBox endPortTxt;<br />
        private System.Windows.Forms.Label label3;<br />
        private System.Windows.Forms.Button startScanBtt;<br />
        private System.Windows.Forms.ListBox listBoxOpen;<br />
        private System.Windows.Forms.ListBox listBoxClosed;<br />
        private System.Windows.Forms.Label label4;<br />
        private System.Windows.Forms.Label label5;<br />
        private System.Windows.Forms.CheckBox prevItems;<br />
        private System.Windows.Forms.Button toFileBtt;<br />
    }<br />
}<br />



Also any critisism on any other aspect of the program would be great, along with tips and tricks Big Grin | :-D

Thank you very much!

Relentless. Smile | :)
AnswerRe: C# background Worker and Progress Bar help Pin
Christian Graus6-May-08 11:38
protectorChristian Graus6-May-08 11:38 
GeneralRe: C# background Worker and Progress Bar help Pin
Relentless6-May-08 11:42
Relentless6-May-08 11:42 
AnswerRe: C# background Worker and Progress Bar help Pin
Gareth H6-May-08 11:59
Gareth H6-May-08 11:59 
AnswerRe: C# background Worker and Progress Bar help Pin
GuyThiebaut6-May-08 12:24
professionalGuyThiebaut6-May-08 12:24 
GeneralRe: C# background Worker and Progress Bar help Pin
Relentless6-May-08 13:08
Relentless6-May-08 13:08 
GeneralRe: C# background Worker and Progress Bar help Pin
Christian Graus6-May-08 13:11
protectorChristian Graus6-May-08 13:11 
GeneralRe: C# background Worker and Progress Bar help Pin
Relentless6-May-08 13:13
Relentless6-May-08 13:13 
GeneralRe: C# background Worker and Progress Bar help Pin
Christian Graus6-May-08 13:24
protectorChristian Graus6-May-08 13:24 
QuestionWriting output in a windows application Pin
Jerry Graham6-May-08 11:21
Jerry Graham6-May-08 11:21 
AnswerRe: Writing output in a windows application Pin
Pete O'Hanlon6-May-08 11:28
mvePete O'Hanlon6-May-08 11:28 
GeneralRe: Writing output in a windows application Pin
Jerry Graham6-May-08 11:30
Jerry Graham6-May-08 11:30 
AnswerRe: Writing output in a windows application Pin
Gareth H6-May-08 11:30
Gareth H6-May-08 11:30 
AnswerRe: Writing output in a windows application Pin
PIEBALDconsult6-May-08 13:46
mvePIEBALDconsult6-May-08 13:46 
QuestionGet mouse coordinates relative to control in separate application... Pin
Edmundisme6-May-08 10:40
Edmundisme6-May-08 10:40 
AnswerRe: Get mouse coordinates relative to control in separate application... Pin
Christian Graus6-May-08 11:37
protectorChristian Graus6-May-08 11:37 
GeneralRe: Get mouse coordinates relative to control in separate application... Pin
Edmundisme7-May-08 8:21
Edmundisme7-May-08 8:21 
QuestionWindows Service - Service Thread Not Starting Pin
ooten146-May-08 10:03
ooten146-May-08 10:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.