Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:19:11+00:00 2026-05-27T22:19:11+00:00

private void txtOctet1_TextChanged(object sender, EventArgs e) { double numCheck1; if (txtOctet1.Text == ) {

  • 0
private void txtOctet1_TextChanged(object sender, EventArgs e)
        {
            double numCheck1;
            if (txtOctet1.Text == "")
            {
            }
            else
            {
                numCheck1 = Convert.ToDouble(txtOctet1.Text);
                if (numCheck1 < 0 | numCheck1 > 255)
                {
                    btnSubnetting.Enabled = false;
                    lblOctet1Error.Text = "Error";
                    lblOctet1Error.BackColor = Color.Red;
                    lblOctet1Error.ForeColor = Color.White;
                }
                else
                {
                    btnSubnetting.Enabled = true;
                    lblOctet1Error.Text = "No Error";
                    lblOctet1Error.BackColor = Color.White;
                    lblOctet1Error.ForeColor = Color.Black;
                }
            }
        }

I have made a decimal to binary converter in C#. This users a class made by me. The user enters their “IP address” in four textboxes (one for each Octet). The above code does work but I don’t want to have to repeat the above code for the other third Octet input textboxes. How would I manage this (if it is possible)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:19:11+00:00Added an answer on May 27, 2026 at 10:19 pm

    Instead of showing you the solution, let’s play with VS a bit :

    1. Introduce two variables that hold the textbox and label references and replace all use below :

    private void txtOctet1_TextChanged(object sender, EventArgs e)
    {
        double numCheck1;
        TextBox txtToValidate = txtOctet1; // Variable 1
        Label lblError = lblOctet1Error; // Variable 2
    
    /* Select from here in the next step */ 
        if (txtToValidate.Text == "") // Here, txtOctet1 replaced
        {
        }
        else
        {
            numCheck1 = Convert.ToDouble(txtToValidate.Text); // Here, txtOctet1 replaced
            if (numCheck1 < 0 | numCheck1 > 255)
            {
                btnSubnetting.Enabled = false;
                lblError.Text = "Error"; // Here, lblOctet1Error replaced
                lblError.BackColor = Color.Red; // Here, lblOctet1Error replaced
                lblError.ForeColor = Color.White; // Here, lblOctet1Error replaced
            }
            else
            {
                btnSubnetting.Enabled = true;
                lblError.Text = "No Error"; // Here, lblOctet1Error replaced
                lblError.BackColor = Color.White; // Here, lblOctet1Error replaced
                lblError.ForeColor = Color.Black; // Here, lblOctet1Error replaced
            }
        }
    /* Select to here in the next step */ 
    }
    

    No progress for the moment

    2. Select the code you want to reuse

    with the mouse or the keyboard, select all code between the two comments place holder I’ve put. Basically, you should have wrapped up the outermost if/else statement.

    3.a Extract to a method

    Right click the select code, choose Refactor,Extract to a method.

    Choose a name for your method, ValidateOctet for example.

    Validate. You should have extracted the logic in a custom method :

    private void txtOctet1_TextChanged(object sender, EventArgs e)
    {
        double numCheck1;
        TextBox txtToValidate = txtOctet1; // Variable 1
        Label lblError = lblOctet1Error; // Variable 2
        ValidateOctet(txtToValidate, lblError);
        }
    
        private void ValidateOctet(TextBox txtToValidate, Label lblError)
        {
        if (txtToValidate.Text == "") // Here, txtOctet1 replaced
        {
        }
        else
        {
            numCheck1 = Convert.ToDouble(txtToValidate.Text); // Here, txtOctet1 replaced
            if (numCheck1 < 0 | numCheck1 > 255)
            {
                btnSubnetting.Enabled = false;
                lblError.Text = "Error"; // Here, lblOctet1Error replaced
                lblError.BackColor = Color.Red; // Here, lblOctet1Error replaced
                lblError.ForeColor = Color.White; // Here, lblOctet1Error replaced
            }
            else
            {
                btnSubnetting.Enabled = true;
                lblError.Text = "No Error"; // Here, lblOctet1Error replaced
                lblError.BackColor = Color.White; // Here, lblOctet1Error replaced
                lblError.ForeColor = Color.Black; // Here, lblOctet1Error replaced
            }
        }    
    }
    

    Still no visible progress

    3.b (Optionnal) remove useless variable

    I choose to simplify the code by removing the variable we created before. I can call the method directly with the textbox and label reference. Choosing to keep the variable or not is a matter of code styling.

    private void txtOctet1_TextChanged(object sender, EventArgs e)
    {
        double numCheck1;
        ValidateOctet(txtOctet1, lblOctet1Error);
        }
    

    4. Reuse the method for all textboxes

    Simply call ValidateOctet for all couple of Textbox/Label :

    private void txtOctet1_TextChanged(object sender, EventArgs e)
    {
        double numCheck1;
        ValidateOctet(txtOctet1, lblOctet1Error);
        ValidateOctet(txtOctet2, lblOctet2Error);
        ValidateOctet(txtOctet3, lblOctet3Error);
        ValidateOctet(txtOctet4, lblOctet4Error);
        }
    

    Progress: you have now the logic defined in one place

    5. Possible optimization

    1. Take a look at other’s answer. Some will probably help you.
    2. There is a System.Net.IPAdress class. That provides a set of methods to play with IP Addresses (notably the TryParse method).
    3. Don’t know you business requirement, but your code won’t support IP V6. We are at the door of (at least) deployment of IP V6. Maybe you should use only one textbox + the IPAddress class to avoid future limitations.
    4. If you intend to have several IP Address fields, you should think about wrapping all the UI and the logic in a reusable UserControl.

    Disclaimer

    Please not that my answer was not targeted to find the better solution, but help you with using Visual Studio and it’s refactoring features. It’s quite common to start with simple things, to prototype, or playing a bit before the actual implementation. Refactoring tools allows to simply redesign some parts of your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code example: private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { if(some condition) { comboBox.Text =
private void button5_Click(object sender, EventArgs e) { if (domainUpDown2.Text == Battlefield: Bad Company 2)
private void button1_Click(object sender, EventArgs e) { string name; name = textBox5.Text; SqlConnection con10
private void btnMiles_Click(object sender, EventArgs e) { try { int milesless200 = int.Parse(txtMiles.Text); int
private void btnBrowserGo_Click(object sender, EventArgs e) { browser.Navigate(txtBrowserURL.Text); } The code above directs the
private void btnGo_Click(object sender, EventArgs e) { Array IDlist = txtUserID.Text.Split(new char[] { });
private void delete_Click(object sender, EventArgs e) { convertedText.Text = ; } private void copy_Click(object
. private void Form1_Load(object sender, EventArgs e) { List<CaclulatedData> tests = new List<CaclulatedData> {
private void displayResultsButton_Click(object sender, EventArgs e) { gameResultsListView.Items.Clear(); //foreach (Game game in footballLeagueDatabase.games) //{
private void launchbutton_Click(object sender, EventArgs e) { launchbutton.Enabled = false; Process proc = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.