Opening link using Delegate in C# - c#

I am trying to enable my links to be opened in a web browser.
Everything works fine with the delegate in the for loop but inside the eventGen() method I tried to obtain the name of the link clicked in which one link brings the string nameOfLink as ...
"System.Windows.Forms.LinkLabel, Text: Quora For Programmers"
on using an if statement to verify the selected name and open the link all the if statements are always skipped even if i change the text to
nameOfLink.Equals("Quora For Programmers");
namespace VisitedSites
{
public partial class frmRecentlyVisitedSites : Form
{
// Create an array of Link labels
LinkLabel[] sites = new LinkLabel[3];
// Create links with the tool tips
LinkLabel lnkQuora = new LinkLabel();
ToolTip quora = new ToolTip();
// Create links with the tool tips
LinkLabel lnkIndeed = new LinkLabel();
ToolTip indeed = new ToolTip();
// Create links with the tool tips
LinkLabel lnkHackerRank = new LinkLabel();
ToolTip hackerRank = new ToolTip();
public frmRecentlyVisitedSites()
{
InitializeComponent();
}
private void frmRecentlyVisitedSites_Load(object sender, EventArgs e)
{
// Populate the array with links
sites[0] = lnkQuora;
quora.SetToolTip(lnkQuora, "Quora For Questions!");
sites[1] = lnkIndeed;
indeed.SetToolTip(lnkIndeed, "Qualified For Jobs!");
sites[2] = lnkHackerRank;
hackerRank.SetToolTip(lnkHackerRank, "Comfortable Coding?");
// Configure the LinkLabel's properties.
this.lnkQuora.Location = new System.Drawing.Point(37, 36);
this.lnkQuora.AutoSize = true;
this.lnkQuora.Text = "Quora For Programmers";
// Configure the LinkLabel's properties.
this.lnkIndeed.Location = new System.Drawing.Point(37, 56);
this.lnkIndeed.AutoSize = true;
this.lnkIndeed.Text = "Indeed For The Skilled";
// Configure the LinkLabel's properties.
this.lnkHackerRank.Location = new System.Drawing.Point(37, 76);
this.lnkHackerRank.AutoSize = true;
this.lnkHackerRank.Text = "Hacker Rank For The Robust";
for (int i = 0; i < sites.Length; i++)
{
this.Controls.Add(sites[i]);
sites[i].LinkClicked += new LinkLabelLinkClickedEventHandler(EventGen);
}
}
private void EventGen(object sender, LinkLabelLinkClickedEventArgs e)
{
// Determine the name of the link
string nameOfLink = sender.ToString();
// process the link depending on the name
if(nameOfLink.Equals("System.Windows.Forms.LinkLabel, Text:Quora For Programmers", StringComparison.InvariantCultureIgnoreCase))
{
lnkQuora.LinkVisited = true;
System.Diagnostics.Process.Start("https://www.quora.com/");
}
else if(nameOfLink.Equals("System.Windows.Forms.LinkLabel, Text:Indeed For The Skilled", StringComparison.InvariantCultureIgnoreCase))
{
lnkQuora.LinkVisited = true;
System.Diagnostics.Process.Start("https://www.indeed.com/");
}
else if(nameOfLink.Equals("System.Windows.Forms.LinkLabel, Text:Hacker Rank For The Robust", StringComparison.InvariantCultureIgnoreCase))
{
lnkQuora.LinkVisited = true;
System.Diagnostics.Process.Start("https://www.hackerrank.com/");
}
}
}
}

Just tried your code, you are missing one space
is
System.Windows.Forms.LinkLabel, Text: Quora For Programmers
instead of
System.Windows.Forms.LinkLabel, Text:Quora For Programmers

why not add the links, e.g. "https://www.quora.com/" to the linkdata like link.LinkData = "https://www.quora.com/", and then open that link using Process.Start(e.Link.LinkData as string)?

Related

How to create many series

Create an object in the class "Series ema = new Series("ema");"
With the button "indicator_Click" download modal dialog.
In the window using the "ComboBox" select options: dig.type, dig.period.
Series ema = new Series("ema");
**********************************************
private void indicator_Click(object sender, EventArgs e)
{
ModalDialogBox dig = new ModalDialogBox();
{
if (dig.ShowDialog() == DialogResult.OK)
{
string formulaName = dig.type;
FinancialFormula formula = (FinancialFormula)Enum.Parse(typeof(FinancialFormula), formulaName, true);
chart1.Series.Add(ema);
chart1.DataManipulator.FinancialFormula(formula, dig.period, "price", "ema");
chart1.Series["ema"].Color = Color.Red;
chart1.Series["ema"].ChartType = SeriesChartType.Line;
chart1.Series["ema"].BorderWidth = 2;
chart1.Series["ema"].ShadowOffset = 1;
}
}
}
Next, if the indicator is not needed, delete it.
private void delete_Click(object sender, EventArgs e)
{
chart1.Series.Remove(ema);
}
I need to be able to add a second, third indicator.
If I try to add a series of the second indicator, an error occurs because the indicator already exists.
You can of course create multiple objects in a class:
Series ema1 = new Series("ema1");
Series ema2 = new Series("ema2");
Series ema3 = new Series("ema3");
but I do not like this method, because I do not know in advance how many indicators will be needed

C# Winforms App requires two clicks on Button_Click Event

The button requires two clicks to fire up the event. Here is an image and the code.There is a combobox which triggers the button with different items, but when I click the button to show an item in a panel on the page, I have to click it twice so it can trigger the event. After selecting an item once by twice-clicking it, every next time i click it works with one click, just like it should.
Here is the image of the combobox which triggers the button
And there is the code :
namespace Carbon
{
public partial class ucAnaliza : MetroFramework.Controls.MetroUserControl
{
static ucAnaliza _instance;
public static ucAnaliza Instance3
{
get
{
if (_instance == null)
_instance = new ucAnaliza();
return _instance;
}
}
public MetroFramework.Controls.MetroPanel MetroAnaliza
{
get { return mPanelAnaliza; }
set { mPanelAnaliza = value; }
}
public ucAnaliza()
{
InitializeComponent();
}
private void ucAnaliza_Load(object sender, EventArgs e)
{
}
private void mPotvrdiElementi_Click(object sender, EventArgs e)
{
switch (((ComboBox)mDropAnaliza).SelectedItem.ToString())
{
case "Главна рамка":
_instance = this;
ucGlavna uc = new ucGlavna();
uc.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc);
break;
case "Челна рамка":
_instance = this;
ucCelna uc2 = new ucCelna();
uc2.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc2);
break;
case "Подолжна рамка":
_instance = this;
ucPodolzna uc3 = new ucPodolzna();
uc3.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc3);
break;
}
}
}
}
Here is the code from the designer for the button :
// mPotvrdiElementi
//
this.mPotvrdiElementi.BackColor = System.Drawing.Color.Transparent;
this.mPotvrdiElementi.CausesValidation = false;
this.mPotvrdiElementi.Cursor = System.Windows.Forms.Cursors.Hand;
this.mPotvrdiElementi.ForeColor = System.Drawing.SystemColors.MenuBar;
this.mPotvrdiElementi.Image = global::Carbon.Properties.Resources.Checked_Checkbox_24px;
this.mPotvrdiElementi.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.mPotvrdiElementi.ImageSize = 24;
this.mPotvrdiElementi.Location = new System.Drawing.Point(758, 34);
this.mPotvrdiElementi.Name = "mPotvrdiElementi";
this.mPotvrdiElementi.Size = new System.Drawing.Size(80, 25);
this.mPotvrdiElementi.Style = MetroFramework.MetroColorStyle.Orange;
this.mPotvrdiElementi.TabIndex = 4;
this.mPotvrdiElementi.TabStop = false;
this.mPotvrdiElementi.Text = "Потврди";
this.mPotvrdiElementi.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.mPotvrdiElementi.UseCustomBackColor = true;
this.mPotvrdiElementi.UseCustomForeColor = true;
this.mPotvrdiElementi.UseSelectable = true;
this.mPotvrdiElementi.UseStyleColors = true;
this.mPotvrdiElementi.Click += new System.EventHandler(this.mPotvrdiElementi_Click);
I know it is a long time ago but I was having the same problem...
But I found a solution to the problem and is working every time and not killing the usability.
private int focusFlag = 0;
private void MainForm_MouseEnter(object sender, EventArgs e)
{
if (focusFlag < 1)
{
this.FocusMe();
++focusFlag;
}
}
This will not always try to focus on that form when trying to go to other forms or something else, it will just focus once and that is enough... after that it will behave normally :)
It seems the MetroForm doesn´t get Focus until you click within the form and it is just a bug from the developers of the MetroFramework when using certain Metro Controls within the Form.
I have seen others posting the same problem when they are using the MetroFramework.
Hopefully this will help.

C# / .Net Youtube V3 API, Issue listing items to control

I currently am working on a small test project with the YouTube API.
What I am trying to achieve is pretty simple : I do a search request with the API get the title and video ID and I put those in a DataTable.
From there I want to make a button out of each result and add these to a FlowLayoutPanel so what I figured was use a foreach loop. But the issue that's presenting itself here is when I got over 100+ results it seems to throw errors (I guess windows forms doesn't like making 100+ buttons)
What I want to know is can I make my code more efficient and can I for example add 2 buttons below to show the "next" and "previous" 100 results. So it will only load 100 at a time.
Below is my code it may be a bit messy since I am pretty new to c#.
This is the button I use to start the search.
private void Youtube_Search_Video_Button_Click(object sender, EventArgs e)
{
string Search_Vid = Youtube_SearchVideo_Box.Text;
if (Youtube_SearchVideo_Box.Text == "Search video" || Youtube_SearchVideo_Box.Text == "")
{
Youtube_SearchVideo_Box.Text = "Search video";
}
if (Search_Vid != null && Search_Vid != "Search video" && Search_Vid != Last_Search_Vid)
{
Last_Search_Vid = Youtube_SearchVideo_Box.Text;
search_results_vids.Clear();
if (search_results_vids.Columns.Count.Equals(0)) {
search_results_vids.Columns.Add("VideoTitle");
search_results_vids.Columns.Add("VideoID");
}
flowLayoutPanel1.Controls.Clear();
toolStripStatusLabel1.Text = "Status : Searching...";
backgroundWorker1.RunWorkerAsync();
}
}
Which starts the backgroundworker below. (Oh and of course the datatable creating before that.)
public DataTable search_results_vids = new DataTable();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
YouTubeService youtube = new YouTubeService(new BaseClientService.Initializer()
{
ApplicationName = this.GetType().ToString(),
ApiKey = "MyApiKeY",
});
var nextPageToken = "";
while (nextPageToken != null)
{
var listRequest = youtube.Search.List("snippet");
listRequest.Q = Youtube_SearchVideo_Box.Text;
listRequest.MaxResults = 50;
listRequest.Type = "video";
listRequest.PageToken = nextPageToken;
var resp = listRequest.Execute();
List<string> videos = new List<string>();
foreach (SearchResult result in resp.Items)
{
switch (result.Id.Kind)
{
case "youtube#video":
object[] newsearchresult = { result.Snippet.Title, result.Id.VideoId};
search_results_vids.Rows.Add(newsearchresult);
break;
}
}
nextPageToken = resp.NextPageToken;
}
}
And when it finishes.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
foreach (DataRow row in search_results_vids.Rows)
{
Button button = new Button();
button.Text = row["VideoTitle"].ToString();
if (button.Text.Length >= 35)
{
button.Text.Remove(button.Text.Length - (button.Text.Length - 35));
}
button.Tag = row["VideoID"];
button.TextImageRelation = TextImageRelation.ImageBeforeText;
button.FlatStyle = FlatStyle.Flat;
button.ForeColor = Color.LightSteelBlue;
button.BackColor = Color.SteelBlue;
button.Width = (flowLayoutPanel1.Width - 120);
button.TextAlign = ContentAlignment.MiddleLeft;
button.Height = 35;
button.Font = new Font(button.Font.FontFamily, 10);
flowLayoutPanel1.Controls.Add(button);
}
toolStripStatusLabel1.Text = "Status : Listing Videos, Please Wait...";
toolStripStatusLabel2.Text = "Results : " + search_results_vids.Rows.Count.ToString();
}
I've tried adding the Foreach loop into the DoWork part of the backgroundworker but then it seems to skip it all together. Any help is very welcome and if I did anything wrong please let me know (Still learning!)
Edit : To clarify it's the button creation part I am stuck at. Listing the items from the table to a listview works fine. All the button settings seem to have to do with the loading time of them. So that's why I want to try an approach where I load only 100 per "page" from the DataTable. I only have no idea how to approach this.
Yes You can Add Buttons in the Same Loop(backgroundWorker1_DoWork) ,Just Make Sure that Adding/Changing UI should be in different Thread ...Otherwise You Would get Cross-Thread Exception.
so One Way to do is
Action actUI = ()=>{
Button button = new Button();
button.Text = get Data from newsearchresult ;
if (button.Text.Length >= 35)
{
button.Text.Remove(button.Text.Length - (button.Text.Length - 35));
}
button.Tag = get Data from newsearchresult ;;
button.TextImageRelation = TextImageRelation.ImageBeforeText;
button.FlatStyle = FlatStyle.Flat;
button.ForeColor = Color.LightSteelBlue;
button.BackColor = Color.SteelBlue;
button.Width = (flowLayoutPanel1.Width - 120);
button.TextAlign = ContentAlignment.MiddleLeft;
button.Height = 35;
button.Font = new Font(button.Font.FontFamily, 10);
flowLayoutPanel1.Controls.Add(button);
};
if(flowLayoutPanel1.InvokeRequired)
flowLayoutPanel1.BeginInvoke(actUI);
else
flowLayoutPanel1.Invoke(actUI);
I think you should be looking into bringing your results in pages.
YouTube API - Pagination

VSTO Word Content Control - adding multiple RichTextContentControls at run time

I'm creating a Word add-in which allows the user to select various text in a Word document and click a button on the ribbon which will wrap that text with a Content Control (rich text). Eventually these content controls will then be mapped to XML.
The code so far is like this:
public partial class Ribbon1
{
private RichTextContentControl titleRichTextControl;
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
private void addTitle_Click(object sender, RibbonControlEventArgs e)
{
AddRichTextControlAtSelection();
}
private void AddRichTextControlAtSelection()
{
word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
currentDocument.ActiveWindow.Selection.Range.Select();
Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
titleRichTextControl = extendedDocument.Controls.AddRichTextContentControl("titleRichTextControl");
titleRichTextControl.PlaceholderText = "Enter the title";
titleRichTextControl.Title = "Title";
titleRichTextControl.Tag = "title";
}
}
All this is fine and it works for the first time the button is clicked. However, if there is more than one 'title' (in this case) that needs adding, and the user presses the button a second time, it throws the error:
The control cannot be added because a control with the name titleRichTextControl already exists in the Controls collection.
It's clear why it complains, but i can't think of the correct way to go to allow multiple clicks of the button to generate multiple content controls of the same type (rich text content control) and the same name ("title" for example).
Can anybody point me in the right direction please.
OK this was how i did it in the end:
private void addTitle_Click(object sender, RibbonControlEventArgs e)
{
AddRichTextControlAtSelection();
}
int count = 0;
private void AddRichTextControlAtSelection()
{
word.Document currentDocument = Globals.ThisAddIn.Application.ActiveDocument;
Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
if (currentDocument.ContentControls.Count > 0)
{
currentDocument.ActiveWindow.Selection.Range.HighlightColorIndex = word.WdColorIndex.wdYellow;
currentDocument.ActiveWindow.Selection.Range.Select();
richTextControls = new List<RichTextContentControl>();
foreach (word.ContentControl nativeControl in currentDocument.ContentControls)
{
if (nativeControl.Type == Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
{
count++;
RichTextContentControl tempControl = extendedDocument.Controls.AddRichTextContentControl("VSTORichTextControl" + count.ToString());
richTextControls.Add(tempControl);
tempControl.Title = "Title";
tempControl.Tag = "title";
break;
}
}
}
else
{
RichTextContentControl VSTORichTextControl;
VSTORichTextControl = extendedDocument.Controls.AddRichTextContentControl("VSTORichTextControl");
VSTORichTextControl.PlaceholderText = "Enter the DM title";
VSTORichTextControl.Title = "Title";
VSTORichTextControl.Tag = "title";
}
}

Combobox closes before i can select a checkbox

I'm currently working in VS 2012.
.NET 4.5 and working on an mmc snap-in. (i know right?!)
so i followed this topic:
Is there a simple way to implement a Checked Combobox in WinForms
as i want something similar to the scheduled task manager.
But that solution does not seem to work for me.
the listview pops up but when i try to click on a checkbox in my listview.
It gives me a big middle finger and closes my dropdown.
is there any way i can suppress the combobox's "focus lost" close event?
i can, not hide the list but then it never hides.
For Example:
// designer class
//
// comboBox1
//
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.comboBox1.DropDownHeight = 1;
this.comboBox1.DropDownWidth = 1;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.IntegralHeight = false;
this.comboBox1.Location = new System.Drawing.Point(256, 371);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(238, 21);
this.comboBox1.TabIndex = 5;
this.comboBox1.DropDown += new System.EventHandler(this.comboBox1_DropDown);
this.comboBox1.DropDownClosed += new System.EventHandler(this.comboBox1_DropDownClosed);
//
// lstWeekDays
//
this.lstWeekDays.CheckBoxes = true;
this.lstWeekDays.Location = new System.Drawing.Point(50, 63);
this.lstWeekDays.Name = "lstWeekDays";
this.lstWeekDays.Size = new System.Drawing.Size(263, 97);
this.lstWeekDays.TabIndex = 13;
this.lstWeekDays.Tag = "lstlstWeekDays";
this.lstWeekDays.UseCompatibleStateImageBehavior = false;
this.lstWeekDays.View = System.Windows.Forms.View.SmallIcon;
this.lstWeekDays.Visible = false;
// Code behind
public Form1()
{
InitializeComponent();
this.lstWeekDays.Items.Add("Monday");
this.lstWeekDays.Items.Add("Tuesday");
this.lstWeekDays.Items.Add("Wednesday");
this.lstWeekDays.Items.Add("Thursday");
this.lstWeekDays.Items.Add("Friday");
this.lstWeekDays.Items.Add("Saturday");
this.lstWeekDays.Items.Add("Sunday");
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
lstWeekDays.Visible = true;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
lstWeekDays.Visible = false;
}
Add the checkboxes to this list instead of the panel.

Categories

Resources