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
        }
    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115

1 Answers1

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");
pm100
  • 48,078
  • 23
  • 82
  • 145
  • @SharabeelShah I would recommend `HashSet` instead of `List` and maybe `.Host` instead of `.ToString()` – Slai Jan 16 '17 at 19:56
  • @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 – Sharabeel Shah Jan 16 '17 at 22:08
  • 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 – pm100 Jan 16 '17 at 23:45