"Recently Visited Sites" Application in C# - c#

Let me clear the air first: yes, this is classwork. No, I don't just want the answer. I am just really stuck and looking for some assistance.
I'll be as specific as I can. I think the best way I can do that is to post the prompt the book gives:
Create a project named RecentlyVisitedSites that contains a Form with a list of three LinkLabels that link to any three Web sites you choose. When a user clicks a LinkLabel, link to that site. When a user's mouse hovers over a LinkLabel, display a brief message that explains the site's purpose. After a user clicks a link, move the most recently selected link to the top of the list, and move the other two links down, making sure to retain the correct explanation with each link.
My main problem with this is that I can't find a good way to make a "list" of the LinkLabels that both allows me to use LinkLabels in the first place and lets me rearrange items in a list-fashion. Here is my best go at it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RecentlyVisitedSites
{
public partial class Form1 : Form
{
LinkLabel[] links = new LinkLabel[3];
public Form1()
{
InitializeComponent();
/*LinkLabel[] links = new LinkLabel[3];
links[0] = new LinkLabel();
links[0].Text = "google.com";
links[0].Location = new System.Drawing.Point(100, 25);
links[0].Links.Add(0, 0, "www.google.com");
this.Controls.Add(links[0]);*/
LinkLabel[] links = new LinkLabel[3];
links[0] = googleLabel;
links[1] = facebookLabel;
linksPanel.Controls.Add(links[0]);
linksPanel.Controls.Add(links[1]);
}
private void googleLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("IExplore", "http://www.google.com");
links[0] = googleLabel;
linksPanel.Controls.Remove(links[0]);
linksPanel.Controls.Add(links[0]);
}
private void facebookLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("IExplore", "http://www.facebook.com");
links[0] = facebookLabel;
linksPanel.Controls.Remove(links[0]);
linksPanel.Controls.Add(links[0]);
}
}
}
And I always get stuck either because the links won't show up, or I can't use them the way the prompt seems to want, or some other thing. The commented-out code is something else I tried using after asking my teacher, but I didn't get much out of our conversation and it didn't work either.
What should I be doing to get these links to display on page, in a list, where I can have them be clickable, open the stated web page in a browser, and be able to rearrange them the way the prompt asks?
Again, I'm not just asking to be given an answer, I have just hit a wall and I'm frustrated because I can't seem to find a decent explanation to help me understand what I actually need to be doing. Any help would be appreciated.

Use this format for each LinkLabel on Form1:
LinkLabel.LinkVisited = true;
System.Diagnostics.Process.Start("IExplore, http://www.Facebook.com");

Related

How can I add a tab control to a RichTextBox?

Project description:
Hi, on my project I have 64 client´s they communicate with my main station on a one wire asynchronous bus, now I want to monitor the communication between them. For that I have a RichTextBox , In that box I see the whole data traffic. Now I want to implement the possibility that when I select a client for example nr. 4 that opens a new RichTextBox on a tab control of my main RichTextBox.
My problem is that I work with C# since 4 weeks and I don't know how can I do that, I searched in the internet but I didn´t find any example. So I ask here for help.
Question:
How can I make the described requirement?, Is the requirement possible?
Picture for example:
Sorry for the bad picture, I have only Paint. That is my MainWindow at the red arrow´s I want my tabs.
I use Microsoft Visual Studio 2015 and WindowsFormsApplication
You need to do it the other way arround, add richtextbox to tabpages.
It can be more simple if you make a custom control that inherits from tabpage. The problem with it is that you will be unable to design it via the visual editor. But since you need just one control in it you can add it via code.
The class for the custom tab is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class CustomTab:TabPage
{
public RichTextBox textbox;
public CustomTab()
{
textbox = new RichTextBox();
this.Controls.Add(textbox);
textbox.Dock = DockStyle.Fill;
}
}
}
As you can see it just inherits from tabpage and on the constructor it adds a RichTextBox that is docked in fill so it will cover all the page.
The textbox is public, so you can acces it simply with tab.textbox.
To add the tabs to the tabcontrol you just need to add the tabcontrol to the form and when you want to add the page simply:
tabControl1.TabPages.Add(new CustomTab());
Having a custom tab allows you to have all the data and methods that you want (Wich client it is for example)
If you have any quaestion feel free to ask

C# Embedded Audio Not Playing

I have embedded an audio file (high.wav) in the resx file for my Windows Form (by just double clicking on Form1.resx in solution explorer, Ctrl+F4, then clicking 'Add Resource'), but using
Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.ResourceManager.GetStream("high");
SoundPlayer sp = new SoundPlayer(embeddedfile);
sp.Play();
only plays a system sound, not the embedded one. I have tried all of the combinations of changing the Persistence from 'Linked at compile time' to 'Embedded in .resx', and Build Action from 'None' to 'Embedded Resource'. I have also tried
Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.high;
which doesn't even compile, saying that 'WindowsFormsApplication2.Properties.Resources' does not contain a definition for 'high'
Sorry is this is a dumb question, I am just starting out. If it makes any difference, I am using Windows 8.1 N.
EDIT: I guess I fixed it. Apparently you cannot add resources by going (formname).cs --> (formname).resx . You must go right click the application name, go to properties, and on the sidebar thing go to resources. I don't know what the difference is, as things added the way that works shows up in the place where it doesn't work to add it.
For adding resource right click on the project name from project explorer then click on resources after add resource arrow ==> add Existing file from anyware
and this is code for playing this file added:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Media;
namespace play_Wav
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SoundPlayer sp = new SoundPlayer(play_Wav.Properties.Resources.myFile);
sp.Play();
}
}
}

HTML to Image: The Confusion Begins

Currently I load an HTML string into a webBrowser control's contents and tell the user to print-screen it.
I'd like to somehow either grab the webBrowser contents as an image, or somehow render this HTML to an image file that can be saved. Are there any free libraries to do this?
I have been curious as to how this is accomplished myself. I have not tried to do this, but here is a link to a CodeProject article that seems to describe the process quite well:
HTML to Image in C#
I use a program called Webshot and I absolutely love it. It has a freeware version with upgrade available. It even has a callable command line interface which can be used in an automated fashion.
Link here.
Well, there's bound to be a better solution than this, but I'm pretty sure it's better than your solution (I haven't checked out the other answers), so...
I have a working C# program that saves a bitmap image of a control. Currently, the control is a textbox, but you could easily make it a web browser control. The entire program is below. The form has a button, button1, and a textbox, textBox1.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(textBox1.Width,textBox1.Height);
textBox1.DrawToBitmap(bm,new Rectangle(0,0,bm.Width,bm.Height));
bm.Save("image.bmp");
}
}
}

MessageBox.Show() fonts

Is there a way I can change the font types in a MessageBox.Show() to get bigger size, bold, italic styles?
You can always make your own MessageBox creating a new Windows.Forms class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MessageBoxFont
{
public partial class Message : Form
{
public Message(String text)
{
InitializeComponent();
tbxMessage.Text = text;
btnOK.Focus();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Then you can control the properties (like the font, size, color and the like) shown under the solution explorer. You initialize this form like this:
private void OpenMessageBox()
{
String text = "This is a sample error message";
Message message = new Message(text);
message.Show();
}
Its a work-around, however, easier to implement :)
I believe that those fonts are controlled by the operating system.
You could (however) make a custom dialog and put anything you want in there including custom fonts.
Here is the MSDN resource for custom dialogs.
http://msdn.microsoft.com/en-us/library/2chz8edb(VS.90).aspx
Have you thought of something like a customized message box (www.html-messagebox.com)?
For more customization such as building an irregular shaped message box (Homer Simpson's head), you are better off creating your own MessageBox-like implementation for your project.
Check this http://www.windowsdevelop.com/windows-forms-general/change-font-size-for-messageboxshow-dialogs-62092.shtml

How to auto save and auto load all properties in winforms C#?

How to auto save all properties winforms when closed and auto load all properties winforms when load ? C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SControl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Controls.Count; i++)
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(Controls[i]));
Stream stream = File.Open("test.xml", FileMode.Create);
x.Serialize(stream, Controls[i]);
}
}
}
}
Your question is a little unclear, but
If you require saving/loading of the Form Layout have a look at
Windows Forms User Settings in C#
If you require saving/loading an object/class have a look at
Load and save objects to XML using serialization
EDIT:
This will show you how to persist certain settings for form properties.
Save and Restore Setings of a .NET Form using XML
Also have a look at The Application Automation Layer - Using XML To Dynamically Generate GUI Elements--Forms And Controls
All these will guide you in the direction you need to go.
I think the main objective here is
Figure out when to save and when to
load, and where to store/retrieve
these settings.
Are you storing these settings per
user? In a database? In a xml file?
Next you need to identify which
properties you will be
saving/restoring per control. Simple
location/size settings might not cut
it as controls will have various
complexities (Button, TextBox,
Gridview, ListView)
Now you need to figure out how to
iterate ALL controls on the form.
Buttons, Textboxes, Panels, Controls
in Controls (controls in panels), and
maybe even your User Controls. This
can be done using recursion.
Now you need to decide on the
structure of the xml file (if you opt
to use xml). This should pretty much
look like a tree structure, as you
would look at the form, and its
controls, and their controls, as a
tree structure.
I'm not aware of any automatic method built into the Form base class, but adding it yourself isn't hard.
You could tap the window load and close events to cache off all relevant properties to a backing store and then reload them later.
Register an event handler to the Form.Load and Form.Closing event handlers. When Form.Closing occurs, save the forms state to a file or database. When Form.Load occurs, check to see if a saved state is present and if so, reload the from from the saved state.
You have to manually code which properties to be saved.
A convenient method is to svae these personalized settings to Windows Forms Application Settings.
Sample code snippet:
//save the winform position and size upon closing
private void Form1_FormClosed(
object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.FormPosition = this.Location;
Properties.Settings.Default.FormSize = this.Size;
Properties.Settings.Default.Save();
}
//load the winform position and size upon loading
private void Form1_Load(object sender, EventArgs e)
{
this.Size = Properties.Settings.Default.FormSize;
this.Location = Properties.Settings.Default.FormPosition;
}
More references:
Using Settings in C#
Any good examples of how to use Applications settings
Use Windows Forms Application Settings to Personalize Your Applications
The process of turning objects like forms into something that could be saved is called serialization. Unfortunately I don't think there's an out-of-box way to serialize forms in WinForm. I did find How to Clone/Serialize/Copy & Paste a Windows Forms Control, and since forms are also controls, you might be able to serialize the properties using the code.

Categories