Question: How can I make a list of link buttons so that when a user clicks on a link it opens it up in windows explorer.
So I have a rich text box that contains a list of all folder names and I have a list of all the folder paths. I want to be able to click on the folder name and have it linked using the path to the correct folder in windows explorer.
LinkLabel link = new LinkLabel();
link.Text = transfer2;
//link.Text = "something";
link.Name = dirName;
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
this.Display_Rich_Text_Box.Controls.Add(link);
this.Display_Rich_Text_Box.AppendText(link.Text + " ");
this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;
I started with this code. I am using a foreach statement to get the folder names and path. I tried to change the name of link so it will appear to the user that they are clicking on folder A, but when you click on folder A it uses the path to open the windows explorer where the folder is from.
Any ideas or help would be greatly appreciated.
Update
I changed the code a little so now it will display, but I cant scroll down. It appears to be only on the surface as I ran something in the rich textbox and it was scrollable while the link stayed on the surface.
I also added a picture so you can see what the problem is. I scrolled a little bit so it would b easy to see.
LinkLabel link = new LinkLabel();
link.Text = dirName;
//link.Text = "something";
link.Name = transfer2;
//link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
this.Display_Rich_Text_Box.Controls.Add(link);
this.Display_Rich_Text_Box.AppendText(link.Text + "\n");
this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;
Update: I am trying to make essentially a list of hyperlinks so I don't think I can use linklabel as I think it is in a fixed position.
For the first part of your problem, opening Explorer, you can do this on the click event for each item in the list (or the click event of the whole list area, as I describe later):
System.Diagnostics.Process.Start("explorer.exe", "\"" + path + "\"");
(the quote/slash thing is to make sure paths with spaces work)
For the UI bits, I've never even seen LinkLabels before, so I don't know how you got on the road to that, lol! I'm not sure whether you're using WinForms or WPF but in either you'd generally want to use something like a ListBox (or a custom control that behaves/looks precisely how you want, but I'd guess you aren't ready for that). In WPF you could set the ListBox's ItemsSource to your data and DisplayMemberPath to whatever property the text comes from (if it is only strings then just don't set DisplayMemberPath). You'd then set up an event for clicking on the ListBox and respond to that by checking what item is selected and running the code above to open Explorer.
If you want to get your UI working with minimal changes, try replacing the LinkLabels with Buttons (you can style them to look like links if you want, at least in WPF) since those work the same way.
Related
I have been reading up on dynamic link labels, and yet, I have not found my answer. In my code, I am reading off of a .csv file, with the basic setup that checks the lines and if every third one is filled, its a link. This all is in a tableLayoutPanel mind you. I'm creating the label like so:
tableLayoutPanel1.Controls.Add(new LinkLabel() { Text = "TEST",Name = count.ToString(),Tag = #"N:\reuther", Anchor = AnchorStyles.Left, AutoSize = true }, 2, 3);
The problem that I'm having, is that some of these columns could be empty, meaning I really don't know how many in total I will have at any moment. Any notes I have seen online, name the dynamic link, and then proceed to use a private function detecting that the particular link by name was clicked. I can't do that because I will never know how many links will be needed until runtime. I can(and did) in my example, name the link, is there anyway to use a generic .Click event that will detect any click, at which point I can just have it open the path by tag? Is there any other way to go around this?
Thank you.
With a little more tinkering, I solved the issue.
LinkLabel[] linkLabel = new LinkLabel[100];
linkLabel[count] = new LinkLabel();
linkLabel[count].Tag = #"N:\reuther";
linkLabel[count].Text = "Click Me";
tableLayoutPanel1.Controls.Add(linkLabel[count], 3, 4);
private void LinkedLabelClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string filepath = ((LinkLabel)sender).Tag.ToString();
System.Diagnostics.Process.Start(filepath);
}
In this method, I created an array that stores 100 link labels. As I create them, I use a counting method to count how many links have been created. With each link, I used .Tag to set the filepath, and finally set the string filepath to the Tag, which allowed me to open it with the line afterwards. Thank you.
I Have Created a Simple form in which i have added Adobe Reader from toolbox using steps
right click in toolbox - Choose Items
choose COM Components tab and there "Adobe PDF Reader"
Now Drag&Drop the Adobe PDF Reader Control into an UserControl
I have successfully added this, opened up a pdf file also. Now it automatically provides with vertical scrollbars for scrolling through the pdf document.
What i want to achieve is instead of using the given scrollbars or mouse to scroll, i want to use a button to scroll scroll the pdf, So there will be two buttons, One for Scroll Up And the other for scroll down.
I have gone through many forums, pages, etc. Havnt found anythn that i could use.
I have Tried Simulating key presses with
SendKeys.Send("{DOWN}");
But as i press the button, the focus is lost on the adobe reader so it doesnt work
Pls help me... I have spent almost half a day searchin for a solution
given that you have provided only a simple piece of code you have tried, i am going to try offer you a generic solution - where you will need to replace the specified variables:
button names
your web app name
as for first of the focus you need to specify where it will be, something along the lines of:
var pFocus = webapplication.formname.pdf_document.focus();
// or webapplication.focus(pdf_document);
again i am just writing this as an ideal layout as i have said you will need to replace the listed variables for this to work and possibly tweak the focus code as i haven't tested that - the buttons however provided you insert your variable names will work as i have tested these:
var buttonAction = ((IJavaScriptExecutor)webapplication).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 5)");
var buttonAction_2 = ((IJavaScriptExecutor)webapplication).ExecuteScript("window.scrollTo(0, document.body.scrollHeight 0)");
//button action will scroll to x co-ordinate 0(far left), y co-ordinate( 5px from bottom)
//buttonAction_2 will return you to the very top left of page, you can edit these values to mess around and try different settings.
so altogether it should look somewhat similar to (if you are using a method for the click just insert the code under there:
var pFocus = webapplication.formname.pdf_document.focus();
if (button.click = true)
webapplication = pFocus;
var buttonAction = ((IJavaScriptExecutor)webapplication).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 5)");
pFocus.execute(buttonAction);
then for button 2
var pFocus = webapplication.formname.pdf_document.focus();
if (button_2.click = true)
webapplication = pFocus;
var buttonAction_2 = ((IJavaScriptExecutor)webapplication).ExecuteScript("window.scrollTo(0, document.body.scrollHeight 0)");
pFocus.execute(buttonAction_2);
hope this helps to some extent.
So I have a folder browser dialog and am having issues with the selected path.
So I want the previous folder that was opened to be selected, and I want it to be scrolled down so that the previous folder is visible.
Now the strange thing is that this works fine, but only sometimes. It's completely random. The path is always highlighted, but it doesn't always scroll down.
Sometimes when I start debugging and click browse, it will open up and be scrolled down to where I want. Then I click ok, click browse again and it's completely random whether or not it's scrolled down to where it should be.
Any thoughts?
Edit: I searched around a lot and found this. It seems to be quite similar to my problem.
"I have tried the test app on Vista 32, XP 32, Win 7 32 & 64. It works fine on everything except Windows 7. Both 32 and 64 appear to have the same issue."
They're saying it's a glitch with Windows 7...?
I ended up using the Ookii dialogs folder browser dialog. Honestly it's just much better than the default folder browser. It also comes with an example, showing you how to use it.
this works for me
folderBrowserDialog1.Reset();
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
folderBrowserDialog1.SelectedPath = WorkingFolder;
but only after the second use of the dialog
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.Description = "Select Chase 6 Installation Folder";
folderBrowser.RootFolder = Environment.SpecialFolder.ProgramFiles;
folderBrowser.ShowNewFolderButton = false;
if (Directory.Exists(Properties.Settings.Default.defaultChasePath))
{
string x = Properties.Settings.Default.defaultChasePath;
//Use API Flag to set correct path, following tahter a catch all better to check
//enum for full list
RootSetter.SetRootFolder(folderBrowser, RootSetter.CsIdl.FlagDontVerify);
folderBrowser.SelectedPath = x;
}
if (folderBrowser.ShowDialog(this) == DialogResult.OK)
{
string huz = folderBrowser.SelectedPath;
}
I got from this link
How do I open a FolderBrowserDialog at the selected folder?
Set your selected path to the last folder path so that it will scroll down.
if (folderDialog.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.Path = folderDialog.SelectedPath;
Properties.Settings.Default.Save();
}
Change the code inside if condition.
I have two quick, easy questions on C# in Visual Studio. First, is there anything like the label, but for an area of text in the program? I would like to have multiple lines of text in my program, but can only seem to accomplish it with a DotNetBar label with wordwrap turned on.
Second, is there any way to have a hyperlink in the middle of the text without using a link label? If I wanted to generate text like "An update is available, please visit http://example.com to download it!", is it possible to make the link clickable without having to position a link label in the middle of the text?
You can use a LinkLabel and set its LinkArea property:
//LinkArea (start index, length)
myLinkLabel.LinkArea = new LinkArea(37, 18);
myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";
The above will make the http://example.com a link whilst the rest of the text in normal.
Edit to answer comment:
There are various ways of handling the link. One way is to give the link a description (the URL) and then launch the URL using Process.Start.
myLinkLabel.LinkArea = new System.Windows.Forms.LinkArea(37, 18);
myLinkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(myLinkLabel_LinkClicked);
myLinkLabel.Text = "An update is available, please visit http://example.com to download it!";
myLinkLabel.Links[0].Description = "http://example.com";
And the event handler can read the description and launch the site:
void myLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(e.Link.Description);
}
You may try RichTextBox control.
string text = "This is the extract of text located at http://www.google.com and http://www.yahoo.com";
richTextBox1.Text = text;
richTextBox1.ReadOnly = true;
richTextBox1.LinkClicked += (sa, ea) =>
{
System.Diagnostics.Process.Start(ea.LinkText);
};
You can use the normal label and make the AutoSize property as false.
And then adjust your width and height it will wrap by it self
I assume you are using doing a windows application, and not a web application.
In C# you can create a normal textbox by dragging and dropping it onto your form, change its property to multi-line, and make it read only. Thats what I always do.
As for adding a link to the text without a linklabel. There is a way to add links to textboxes. You can check out a pretty good tutorial at http://www.codeproject.com/KB/miscctrl/LinkTextBox.aspx/
I need to open image in new tab exactly as it's being done by clicking right button on it and then selecting View Image in firefox. Copying pic url and using browser.Goto doesn't give me the result i need due to some tricky javascript. Could anyone give me some suggestions? Thanks
Old question, some answer for reference only.
Get the src attribute for the image you are looking for and then open a new instance of IE with that address (you will probably need to append the base website address plus whatever there is in the src attribute for the image you are looking for. Some code for that:
Image btnLogin = Window.Image(Find.ByName("TheImageNameOrAnyOtherAttribute"));
string imageAddress = btnLogin.GetAttributeValue("src");
var newBrowser = new IE();
newBrowser.GoTo("http://www.somesite.com/"+imageAddress);