1

I'm attempting to block websites in windows form application C# using the WebBrowser Tool for a child browser project. So currently I'm able to block the predefined websites, however, I want to be able to add to the array which isn't possible. So I was wondering if there was another way?

private void webBrowser1_Navigating(object sender,WebBrowserNavigatingEventArgs e)        
{            
    String[] BlockList = new String[5]; //Array list stores the block list

    BlockList[0] = "http://www.google.com";
    BlockList[1] = "http://www.google.co.uk";
    BlockList[2] = "http://www.gmail.com";
    BlockList[3] = "http://www.yahoo.com";
    BlockList[4] = "http://www.bing.com";

    for (int i = 0; i < BlockList.Length; i++)
    {
        if (e.Url.Equals(BlockList[i]))
        {
            e.Cancel = true;
            MessageBox.Show("Booyaa Says No!", "NO NO NO", MessageBoxButtons.OK, MessageBoxIcon.Hand); // Block List Error Message
        }
    }
}
2
  • 2
    use a list instead of an array, you also need some way of loading the list. It needs to be a property of the form class, not a local in the event handler Commented Jan 16, 2017 at 18:56
  • @pm100 could possibly show me an example ? Commented Jan 16, 2017 at 19:07

1 Answer 1

2

here is an example

private List<string> BlockedUrls {get;set;}
private void webBrowser1_Navigating(object sender,WebBrowserNavigatingEventArgs e)        
{            
      if(BlockedUrls.Contains(e.Url.ToString())
      {
            e.Cancel = true;
            MessageBox.Show("Booyaa Says No!", "NO NO NO", MessageBoxButtons.OK, MessageBoxIcon.Hand); // Block List Error Message
        }
    }
}

you can then go

// in constructor of form
       BlockedURls = new List<string>();
        BlockedUrls.Add("www.blocked.com");
Sign up to request clarification or add additional context in comments.

3 Comments

@SharabeelShah I would recommend HashSet<string> instead of List<string> and maybe .Host instead of .ToString()
@pm100 So I've implemented a list which blocks. However, how would I now add to the list via a button in the form application
sorry dude, i cant write the whole thing for you. Basically you need top load that list from somewhere and offer someway of maintinaing it. Probzbly should read it off a file at form load time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.