4 Replies - 2230 Views - Last Post: 16 August 2012 - 08:16 AM Rate Topic: -----

#1 Jakeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 12

C# form app IP program problem

Posted 16 August 2012 - 07:04 AM

Hi ive been tryin to fix my code so that it gives me all of the local IP address but my code keeps skipping to the last IP and leaving the first 3 out of the Textbox.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
              
            String strHostName = Dns.GetHostName();
            textBox2.Text=("Host Name: " + strHostName);
            IPHostEntry iphostentry = Dns.GetHostEntry(strHostName);
            double nIP = 1.0;
            foreach(IPAddress ipaddress in iphostentry.AddressList)
                 {    
                    textBox1.Text=("IP #" + nIP + ": " +                      
                    ipaddress.ToString());
                    nIP++;
                 }   
        }
        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: C# form app IP program problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5417
  • View blog
  • Posts: 11,608
  • Joined: 02-June 10

Re: C# form app IP program problem

Posted 16 August 2012 - 07:25 AM

Line 29: You're setting the text every time, not ADDING to the existing text. Basic breakpoint debugging should tell you what's happening.

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.



Jakeb: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler



This post has been edited by tlhIn`toq: 16 August 2012 - 07:26 AM

Was This Post Helpful? 1
  • +
  • -

#3 Jakeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 12

Re: C# form app IP program problem

Posted 16 August 2012 - 08:05 AM

Ok , had a go at using breakpoints and i completely see whats happening now so i figured out that i would need to use AppendText onto my textBox sothat it adds to the text rather than rewriting it although im now getting the error that "Cannot assign to 'AppendText' Because it is a 'Method group'.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5417
  • View blog
  • Posts: 11,608
  • Joined: 02-June 10

Re: C# form app IP program problem

Posted 16 August 2012 - 08:09 AM

You're complicating it Just add your new text to the old text, plus a newline.

29 textBox1.Text += ("IP #" + nIP + ": " + ipaddress.ToString()) + Environment.NewLine;
Was This Post Helpful? 1
  • +
  • -

#5 Jakeb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-August 12

Re: C# form app IP program problem

Posted 16 August 2012 - 08:16 AM

ah awesome works like a charm now , thanks alot got for the solution and the tips
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1