How to browse the internet on C# windows forms panel? - c#

I'm creating simple browser just navigate the "google".
How can I do it in my point?
Viewer = new System.Windows.Forms.Panel();
private void Browser_Click(object sender, EventArgs e)
{
WebBrowser web1 = new WebBrowser();
web1.Navigate("http://www.google.com/");
Viewer = web1 // It gets error
}

Just drag out the WebBrowser control like you would with any other control..
Then access it by its name (default would be WebBrowser1 i think so modify your code to be WebBrowser1.Navigate("http://www.google.com/");.)

Reason for error is you are trying convert "Web Browser" control to "Panel" control type.
Change :
Viewer = web1;
To:
Viewer.Controls.Add(web1); // add as child control
web1.Dock = DockStyle.Fill; // fill style in viewer
Make sure your viewer is docked in Parent control as needed.
Hope it helps..!!

try
web1.Url = new Uri("http://www.google.com");

Related

Inline Hyperlink navigate to Page

I'm constructing text, and some pieces of the text should contain a hyperlink. However, these hyperlinks do not redirect to a webpage but should open a page in the UWP app (the currently running UWP app, not a new instance of it or a different app).
A HyperlinkButton can only open URL's that lead to an external browser, it can't open a page inside the app.
Using an InlineUIContainer doesn't work, I get
Exception thrown: 'System.ArgumentException' in mscorlib.ni.dll
Additional information: Value does not fall within the expected range.
With this code
List<Inline> ic = new List<Inline>();
InlineUIContainer container = new InlineUIContainer();
TextBlock tbClickable = new TextBlock();
tbClickable.Foreground = new SolidColorBrush(Colors.Red);
tbClickable.Text = label?.name;
tbClickable.Tag = label?.id;
tbClickable.Tapped += TbArtist_Tapped;
container.Child = tbClickable;
ic.Add(container);
When I use
foreach (var item in ic)
{
dTb.Inlines.Add(item);
}
Where tbCurrent is the TextBlock.
Any other ways to get a clickable link as an Inline element?
Best case scenario I can attach a Tapped/Click event handler.
But opening the page via a URI method or so is also good.
I changed to a RichTextBlock and using Blocks I could add a clickable TextBlock.
This works in UWP.
List<Block> ic = new List<Block>();
Paragraph para = new Paragraph();
InlineUIContainer iuic = new InlineUIContainer();
TextBlock hpb = new TextBlock();
hpb.Text = "link text";
hpb.Tag = "some tag to pass on to the click handler";
hpb.Tapped += ClickHandler;
hpb.TextDecorations = TextDecorations.Underline;
hpb.Foreground = new SolidColorBrush((Windows.UI.Color)page.Resources["SystemAccentColor"]);
iuic.Child = hpb;
para.Inlines.Add(iuic);
ic.Add(para);
There is no reason you could not use a HyperlinkButton for this. Quick example:
<HyperlinkButton Click="HyperlinkButton_Click" Content="Click me" />
And the Click handler:
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(MainPage));
}

WinForm TabControl tab page contents does not update until the new tab is shown

I have a tabcontrol which allows the creation of new tabs. Each new tab has a web browser control CEFSharp on it. When the new tab is created it is not shown the previously opened tab is shown; which is what we want.
However, the browser on the newly created tab is only added to the tab page, and only partially runs... it does not go to the loading state until the tab page is shown.
Here is the Tabpage creation code:
private void AddNewBrowser()
{
Log("Adding New Tab and Browser");
UiBrowser browser = new UiBrowser();
TabPage tp = new TabPage();
tp.Controls.Add(browser);
customTabControl1.TabPages.Add(tp);
}
The UiBrowser is a UserControl which contains the CEFSharp Browser Control plus some extra UI.
And here is the Startup code for the Browser itself.
private void UiBrowser_Load(object sender, EventArgs e)
{
Execute();
}
private void Execute()
{
webBrowser = new ChromiumWebBrowser("http://google.co.uk")
{
Dock = DockStyle.Fill,
Text = "Loading...",
Tag = Tag
};
webBrowser.TitleChanged += Browser_TitleChanged;
webBrowser.AddressChanged += Browser_AddressChanged;
webBrowser.ConsoleMessage += Browser_ConsoleMessage;
webBrowser.LoadingStateChanged += Browser_LoadingStateChanged;
webBrowser.StatusMessage += Browser_StatusMessage;
browserPanel.Controls.Add(webBrowser);
Application.DoEvents();
}
The code has been simplified for clarity and I have not found a solution on SO or elsewhere for this problem.
Question:
How do I get the browser control to load the webpage whilst remaining in the background? That is while the TabPage that the control is on is NOT shown to the user.
The Load event will only happen when the control becomes visible the first time:
Occurs before the control becomes visible for the first time.
so try moving your Execute method into the UserControl's constructor code.
There is no "official" way of doing that.
But if you really need it and don't afraid using internals, you may take a look at my answer to WinForms: Respond to BindingSource being applied.
The solution (or hack) is encapsulated in this little helper
public static class ControlUtils
{
static readonly Action<Control, bool> CreateControlFunc = (Action<Control, bool>)Delegate.CreateDelegate(typeof(Action<Control, bool>),
typeof(Control).GetMethod("CreateControl", BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(bool) }, null));
public static void CreateControls(this Control target)
{
if (!target.Created)
CreateControlFunc(target, true);
else
for (int i = 0; i < target.Controls.Count; i++)
target.Controls[i].CreateControls();
}
}
At the end of your form load event, add the following
this.CreateControls();
or
customTabControl1.CreateControls();
and also here
private void AddNewBrowser()
{
Log("Adding New Tab and Browser");
UiBrowser browser = new UiBrowser();
TabPage tp = new TabPage();
tp.Controls.Add(browser);
customTabControl1.TabPages.Add(tp);
if (customTabControl1.Created)
tp.CreateControls();
}

Adding a twin tabPage to tabControl through a user command

I'm a newbie in c# and probably going to ask a very easy question, but I've not been able to find anything on the web to help.
I have a tabControl with a TabPage which is containing a TextBox object; this object, when the event "Text changed" is invoked, will perform the change of the parent tabPage's name.
The textbox where I typed "text changed by me" has a method which is managing changing the name of the tabPage:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
this.tabControl2.SelectedTab.Text = this.textBox1.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
}
Into the current page menu is contained a control to add a new page, which runs this method when the user click on it:
private void addNewPageToolStripMenuItem_Click(object sender, EventArgs e)
{
int numPagine;
string strPagine;
numPagine = this.tabControl2.TabCount;
strPagine = numPagine.ToString();
this.tabControl2.TabPages.Add("new page" + strPagine);
}
...and here is the output, which is expected since I'm just asking to add a new empty tabPage:
So, my question is: how can I make possible that when the user is clicking on "Add new page", rather than creating an empty new tabPage the program is rather creating a page like the first one (i.e. containing a textbox into the same position which has a method to change the text of the parent tabPage that I have just created?
Here is an example.
//..
// create the new page
TabPage tpNew = new TabPage("new page..");
// add it to the tab
this.tabControl2.TabPages.Add(tpNew);
// create one labe with text and location like label1
Label lbl = new Label();
lbl.Text = label1.Text;
lbl.Location = label1.Location;
// create a new textbox..
TextBox tbx = new TextBox();
tbx.Location = textBox1.Location;
tpNew.Controls.Add(lbl);
tpNew.Controls.Add(tbx);
// add code to the new textbox via lambda code:
tbx.TextChanged += ( (sender2, evArgs) =>
{
if (tbx.Text != "")
this.tabControl2.SelectedTab.Text = tbx.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
} );
For more complicated layout you may want to consider creating a user control..
You also may want to create the first page with this code; the, of course with real values for text and positions!
For creating a UserControl you go to the project tag and right click Add-UserControl-UserControl and name it, maybe myTagPageUC. Then you can do layout on it like on a form. A rather good example is right here on MSDN
The problem is that is has no connection to the form, meaning you'll have to code all sorts of references to make it work..
I'm not really sure if you may not be better off writing a complete clonePage method instead. It could work like the code above, but would loop over the Controls of the template page and check on the various types to add the right controls..
It really depends on what is more complicated: the Layout or the ties between the pages and the form and its other controls..

How can I add this WPF control into my WinForm?

I know that I must use an ElementHost to display a WPF control in a WinForm, but as the WPF control is third party software and it only comes with an XML file and a DLL file.
The control is AvalonEdit, I added both the ICSharpCode.AvalonEdit.xml and ICSharpCode.AvalonEdit.dll files to my project, and I went to Project -> Add Reference and added the DLL as a reference. Now I can access the ICSharpCode namespace in my code, all of the classes and methods are exposed, but from this point I am unsure how to actually use the control in my WinForm.
I was expecting a WPF control to appear in the Solution Explorer, but it does not. I tried adding an ElementHost control to my WinForm anyways, but when I try to Select the Hosted Content, no controls appear, so it doesn't know about my WPF control. How can I use the AvalonEdit WPF control in my WinForm?
If you want to be able to set the hosted content at design time the control needs to be part of your solution. One way to achieve that is to create a custom WPF user control which contains the AvalonEdit component you want to use. I.e
Create a WPF User Control library project and create a user control
containing the AvalonEdit component.
Add the User control project to your Winforms solution.
Now you should be able to select your new user control as the hosted content.
Or you could add the AvalonEdit control directly in code like this:
public Form1()
{
InitializeComponent();
ElementHost host= new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100,100);
AvalonEditControl edit = new AvalonEditControl();
host.Child = edit;
this.Controls.Add(host);
}
Not sure what the control is called so replace the AvalonEditControl as appropriate.
You'll want an example on how to do Code Colouring/Syntax Highlighting as well:
public Form1()
{
InitializeComponent();
ICSharpCode.AvalonEdit.TextEditor textEditor = new ICSharpCode.AvalonEdit.TextEditor();
textEditor.ShowLineNumbers = true;
textEditor.FontFamily = new System.Windows.Media.FontFamily("Consolas");
textEditor.FontSize = 12.75f;
string dir = #"C:\Temp\";
#if DEBUG
dir = #"C:\Dev\Sandbox\SharpDevelop-master\src\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\Highlighting\Resources\";
#endif
if (File.Exists(dir + "CSharp-Mode.xshd"))
{
Stream xshd_stream = File.OpenRead(dir + "CSharp-Mode.xshd");
XmlTextReader xshd_reader = new XmlTextReader(xshd_stream);
// Apply the new syntax highlighting definition.
textEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(xshd_reader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
xshd_reader.Close();
xshd_stream.Close();
}
//Host the WPF AvalonEdiot control in a Winform ElementHost control
ElementHost host = new ElementHost();
host.Dock = DockStyle.Fill;
host.Child = textEditor;
this.Controls.Add(host);
}
ElementHost host = new ElementHost();
host.Size = new Size(200, 100);
host.Location = new Point(100, 100);
ICSharpCode.AvalonEdit.TextEditor edit = new
ICSharpCode.AvalonEdit.TextEditor();
host.Child = edit;
this.Controls.Add(host);

How do I remove a specific panel through event handling in my FlowlayoutPanel for my Windows Form Application?

For my C# Windows Form Application, I have created a flowlayoutpanel that contains several panels. Inside the panel, I have a button "Clear" for each and every single panel.
How do I write the event handler for the code for the button "Clear" such that once I have click the button, the panel would sort of be "Removed" from the flowlayoutpanel.
This is a short part of the code of the adding of panels to the flowlayoutpanel.
nFlowPanel.Controls.Add(createNotificationPanel());
nFlowPanel.Controls.Add(createNotificationPanel());
nFlowPanel.Controls.Add(createNotificationPanel());
nFlowPanel.Controls.Add(createNotificationPanelImpt());
nFlowPanel.Controls.Add(createNotificationPanelImpt());
and this is the code for the button "Clear"
Button btnClear = new Button
{
Text = "Clear",
Name = "btnClear",
Location = new Point(416, 17)
};
p.Controls.Add(btnClear);
btnClear.Click += new EventHandler(buttonClear_Click);
So what should i write in the following method to have the effect of removing e.g. the second panel that was added in the first part of code I have written?
void buttonClear_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
EDIT
the code for creating my panel is
var p = new Panel
{
BorderStyle = BorderStyle.FixedSingle ,
Size = new Size(506,100),
Name = "notifyPanel"
};
and the code for creating my FlowLayoutPanel is
var nFlowPanel = new FlowLayoutPanel
{
FlowDirection = FlowDirection.TopDown,
WrapContents = false,
AutoScroll = true,
Size = new Size(530, 377),
Location = new Point(13, 145)
};
and the code for my button clear is
void buttonClear_Click(object sender, EventArgs e)
{
var button = (Control)sender;
var panel = button.Parent.Controls["notifyPanel"];
panel.Dispose();
}
however it gives the error
Object reference not set to an instance of an object.
on the panel.Dispose() line.
anyone can help?
The Controls.Remove() method is very dangerous, it doesn't dispose the control. Which will live on, moved to the so-called parking window, using up both Windows and managed resources. After a bit less than 10,000 times doing this it crashes your program when Windows is no longer willing to let you create any more windows.
Call the control's Dispose() method instead. That also automatically removes the control from its container.
void buttonClear_Click(object sender, EventArgs e)
{
var panel = nFlowPanel.Controls["notifyPanel"];
panel.Dispose();
}
You can do it like this:
nFlowPanel.Controls.Remove((sender as Button).Parent);
I will suggest you to use List for this. Before adding Panels in the FlowLayoutpanel, add them in the List. Then just remove the indexed panel from the flowlayoutpanel.
Panel pnlTemp = (panel)list[index];
nFlowPanel.Controls.Remove(pnlTemp);
To get the index of the button you have to add your buttons also to your list and after clicking any button, search the button in the list and get the index of the button where it is saved in the list. If my code is unclear, let me know. but I feel your task is that complex. I am not sure but this link may be of some help.
Hope it helps.

Categories