4

I am trying to be able to paste (mouse or keyboard shortcut) a 12 digit number (IP address, but no periods between) into 4 fields. Each has maximum length of 3.

I am trying to do this by using TextChange, text box, property. I was trying to use Substring but it doesn't each octet work.

    public PingIPRange()
    {
        InitializeComponent();

        txtF1.TextChanged += new EventHandler(NextField);
        txtF2.TextChanged += new EventHandler(NextField);
        txtF3.TextChanged += new EventHandler(NextField);
    }

    private void NextField(object sender, EventArgs e)
    {
        if (txtF1.TextLength == 3)
        {
            txtF2.Focus();
            txtF1.Text = txtF1.Text.Substring(0, 3);
            txtF2.Text = txtF1.Text.Substring(3, 30);
        }
        if (txtF2.TextLength == 3)
        {
            txtF3.Text = txtF2.Text.Substring(3, 27);
            txtF3.Focus(); 
        }
        if (txtF3.TextLength == 3)
        {
            txtF4.Focus();
        }
    }
NewHelpNeeder
  • 693
  • 1
  • 10
  • 19

4 Answers4

4

Try putting this code in NextField method. And hookup only to txtF1 textbox textchange event.

TextBox txt = (TextBox) sender; 
var s1 = txt.Text.Split('.');

if(s1.Length==4)
{
    txtF1.Text = s1[0];
    txtF2.Text = s1[1];
    txtF3.Text = s1[2];
    txtF4.Text = s1[3];
} 

UPDATE: As you have updated the question that there will not be any dot symbol you can split string like this

var s1=Enumrable
    .Range(0,4)
    .Select(i => txt.Text.Substring(i * 3, 3))
    .ToArray();
Community
  • 1
  • 1
Maheep
  • 5,539
  • 3
  • 28
  • 47
  • This is great answer, but I need something with out signs like just digits. Do you know a way so I can do similar but picking only every 3 digits? – NewHelpNeeder Dec 14 '11 at 10:10
  • 1
    I would add `TextBox txt = (TextBox) sender; var s1 = txt.Text.Split('.');` to be able to paste in all textboxes. – Renatas M. Dec 14 '11 at 10:10
  • @NewHelpNeeder This will not give any sign as this will treat things as string not numbers. – Maheep Dec 14 '11 at 10:15
2

This wont work so well as you're trying to change text within the TextChanged handler - so it will fire itself again. Why not just have the event handler change focus to the next box when the length is 3, that way you avoid a cyclic loop.

Paul
  • 1,483
  • 14
  • 32
1

Works only if it's pasted in TextF1.

public PingIPRange()
{
    InitializeComponent();

    txtF1.TextChanged += new EventHandler(PasteNumbers);
}

private void PasteNumbers(object sender, EventArgs e)
{
    if (txtF1.TextLength > 9) { txtF4.Text = txtF1.Text.Substring(9, 3); }
    if (txtF1.TextLength > 6) { txtF3.Text = txtF1.Text.Substring(6, 3); }
    if (txtF1.TextLength > 3)
    {
       txtF2.Text = txtF1.Text.Substring(3, 3);
       txtF1.Text = txtF1.Text.Substring(0, 3);
    }
}

Edit, to be totally correct, you need to do a min between (txtF1.TextLength - 9) and the 3 in the Substring not to have OutOfBound problems for txtF4, and the same for txtF3 and txtF2.

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
1

You could you the KeyUp and MouseUp events on txtF1 and use the following code:

    private void txtF1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Control && e.KeyCode == Keys.V)
        {
            PasteText();
        }
    }

    private void txtF1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right &&
            ((TextBox)sender).Modified)
        {
            PasteText();
        }
    }

    private void PasteText()
    {
        string[] val = txtF1.Text.Split('.');

        txtF1.Text = val[0].ToString();
        txtF2.Text = val[1].ToString();
        txtF3.Text = val[2].ToString();
        txtF4.Text = val[3].ToString();
    }

NOTE: I haven't included any error handling.

Kev Ritchie
  • 1,599
  • 10
  • 16