Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. In C# Express, what's best way to get IP Address from user?

In C# Express, what's best way to get IP Address from user?

Scheduled Pinned Locked Moved C#
csharpjavatutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Naruki 0
    wrote on last edited by
    #1

    C# newb here. I've gone down the rabbit hole trying to find the answer myself, so I am asking here. In my windows form, I want to prompt the user to enter an IP address. I made a popup dialog with a MaskedTextBox (mask was 990.990.990.990). This worked, but it required the user to fill in every character (even if he just had to press the arrow key or spacebar, that is kind of annoying). I tried other masks (like 099.099.099.099), but nothing makes that annoyance go away. So I decided to make 4 separate text boxes. This adds the ability to easily tab to the next octet, but it takes away the nice Copy/Paste ability to dump in an IP from the clipboard. So I started looking for some kind of OnPaste event that I could handle and have it disburse pasted addresses automagically. There doesn't appear to be such an event. I found several explanations of how to intercept Ctrl-V or Shift-Ins key combinations, but that is an incomplete solution; I want to catch all possible paste methods. I was thinking about overriding the IsInputKey method somehow, but I don't know how to do that without creating a custom control (which just seems like massive overkill to me). In Java, I could override the method at instantiation like so:

    this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox() {
    protected override bool IsInputKey(Keys AKeyData) {...}
    };

    But that doesn't seem to work in C#. So what's the best way to accomplish what I want to do?

    My other signature is a Porche.

    B 1 Reply Last reply
    0
    • N Naruki 0

      C# newb here. I've gone down the rabbit hole trying to find the answer myself, so I am asking here. In my windows form, I want to prompt the user to enter an IP address. I made a popup dialog with a MaskedTextBox (mask was 990.990.990.990). This worked, but it required the user to fill in every character (even if he just had to press the arrow key or spacebar, that is kind of annoying). I tried other masks (like 099.099.099.099), but nothing makes that annoyance go away. So I decided to make 4 separate text boxes. This adds the ability to easily tab to the next octet, but it takes away the nice Copy/Paste ability to dump in an IP from the clipboard. So I started looking for some kind of OnPaste event that I could handle and have it disburse pasted addresses automagically. There doesn't appear to be such an event. I found several explanations of how to intercept Ctrl-V or Shift-Ins key combinations, but that is an incomplete solution; I want to catch all possible paste methods. I was thinking about overriding the IsInputKey method somehow, but I don't know how to do that without creating a custom control (which just seems like massive overkill to me). In Java, I could override the method at instantiation like so:

      this.maskedTextBox1 = new System.Windows.Forms.MaskedTextBox() {
      protected override bool IsInputKey(Keys AKeyData) {...}
      };

      But that doesn't seem to work in C#. So what's the best way to accomplish what I want to do?

      My other signature is a Porche.

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #2

      You can still override functionality, you just need to do it as its own inherited class, rather than inline at instantiation.

      Help me! I'm turning into a grapefruit! Buzzwords!

      L N 2 Replies Last reply
      0
      • B benjymous

        You can still override functionality, you just need to do it as its own inherited class, rather than inline at instantiation.

        Help me! I'm turning into a grapefruit! Buzzwords!

        L Offline
        L Offline
        Luc 648011
        wrote on last edited by
        #3

        Some of these[^] may interest you. :)

        N 1 Reply Last reply
        0
        • B benjymous

          You can still override functionality, you just need to do it as its own inherited class, rather than inline at instantiation.

          Help me! I'm turning into a grapefruit! Buzzwords!

          N Offline
          N Offline
          Naruki 0
          wrote on last edited by
          #4

          As I said, I don't know how to do that without creating a custom control (i.e., inheriting the class). Doing that seems like over-working what should be a simple event handler. As a one-off, that may be a necessary compromise. But it seems a bit like hard coding the text value for a button. Do it enough, and you lock yourself down. If I do go the custom control route, should I override Paste functionality for four small textboxes, or should I override masking functionality for one big textbox? Either way I really don't know the best functionality to override, so I'll have to pull my hair out all over again on that. All I want is a super slick-input UI for IP addresses. Is that so much to ask for, Microsoft? ;-)

          My other signature is a Porche.

          1 Reply Last reply
          0
          • L Luc 648011

            Some of these[^] may interest you. :)

            N Offline
            N Offline
            Naruki 0
            wrote on last edited by
            #5

            I appreciate the thought. I already saw a couple of those, but I was hoping to avoid adding custom controls into my code. Especially since it should be such a small change, dangit! Why does MS torment me so?

            My other signature is a Porche.

            S 1 Reply Last reply
            0
            • N Naruki 0

              I appreciate the thought. I already saw a couple of those, but I was hoping to avoid adding custom controls into my code. Especially since it should be such a small change, dangit! Why does MS torment me so?

              My other signature is a Porche.

              S Offline
              S Offline
              Skymir
              wrote on last edited by
              #6

              Use keydown etc. events to filter out any bad characters, then in lost focus use IpAddress.TryParse(string). If that works IpAddress.Parse(string). I don't know about your app, but you might also want to handle host names in the same box, which makes things a bit more complicated as well.

              The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups