I have a C# Windows form which based on user input will fetch multiple download links. Now I am facing difficulties in displaying this links to user so that they can click on their desired link to download the files.
I can display everything using MessageBox but could not make links in MessageBox and as the download link is quite long it is not user friendly.
I tried LinkLabel following example from http://msdn.microsoft.com/en-us/library/aa288420(v=vs.71).aspx. This can work but only for 1 link.
Any idea how I can do this for multiple links or is there any other method?
Create your own form to display message to user. Also, use TableLayoutPanel and LinkLabel to display multiple links in the created custom message form like below.
string[] links = new string[10];
TableLayoutPanel panel = new TableLayoutPanel();
panel.RowCount = links.Length;
panel.ColumnCount = 1;
int currentRow = 0;
foreach (var link in links)
{
LinkLabel linkLabel = new LinkLabel();
linkLabel.Text = "Click here to get more info.";
linkLabel.Links.Add(6, 4, link);
linkLabel.OnLinkClicked += OnLinkClicked;
panel.Controls.Add(linkLabel, 0, currentRow++);
}
this.Controls.Add(panel);
The event handler looks like below,
void OnLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start(e.Link);
}
Refer the example code in this msdn link. http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel.link.aspx
Try this:
Process.Start(e.Link.LinkData.ToString());
Related
This is the code I have for the button. I want the configbutton to display a pop up window when clicked on. I have looked up and tried a lot of different codes but none of them work. I feel like I am missing a key component but I am not sure what that is. Appreciate the help!
tr = new TableRow();
// Create the cell
tc = new TableCell();
tc.Width = Unit.Point(300);
tc.BorderWidth = 0;
tc.BorderStyle = BorderStyle.None;
Button ConfigButton = new Button();
ConfigButton.Text = "Configuration";
ConfigButton.Visible = true;
tc.Controls.Add(ConfigButton);
tr.Cells.Add(tc);
tbl.Controls.Add(tr);
Using JavaScript Along with ASP.NET you will do the following:
// when you create the button, you can add attributes
Button ConfigButton = new Button();
// this example will display alert dialog box
ConfigButton.Attributes.Add("onclick", "javascript:alert('ALERT ALERT!!!')");
// to get you popup windows
// you would use window.open with the BLANK target
ConfigButton.Text = "Configuration";
ConfigButton.Visible = true;
I recommend looking into using the AJAX Control Toolkit ModalPopupExtender. This is what I use for my pop ups in ASP.NET.
Here is a link to he AJAX Control Toolkit samples website showing how this control works:
http://www.ajaxcontroltoolkit.com/ModalPopup/ModalPopup.aspx
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..
I am dynamically creating buttons in C# with this logic
for (int i = 1; i <= vap; ++i)
{
newButtons[i] = new Button();
newButtons[i].BackColor = Color.Gray;
newButtons[i].Name = "Button4" + i.ToString();
newButtons[i].Click += new EventHandler(NewButtons_Click);
newButtons[i].Location = new System.Drawing.Point(width,height);
newButtons[i].Size = new System.Drawing.Size(76, 38);
tabPage5.Controls.Add(newButtons[i]);
}
This is creating a button and the click event is also working but my problem is I don't know how to get the text of the newly created button. On form load I am putting the text of button from database and this also happening correctly, but I want to know how to get the text of dynamically created buttons.
You won't be able to get the text until after you populate it from the database (careful not to try and get the text too early).
But this should work:
string buttonText = FindControl("Button41").Text;
Update
Since you want the button text from within the click event, you can access the sender object:
Button button = sender as Button;
string buttonText = button.Text;
You just have to set the Text property of the button when you add it.
Using something along the lines of...
string BtnTxt = FindControl("ExampleButton1").Text;
should work fine.
This may cause problems later on however if you are trying to pull text content of buttons in a random order.
I am working on a web browser project I want to make a web browser I used ToolStrip to put all the functions of the web browser(favorite, history, home, GO, back, forward). What I want now is to make tha Tabs.
1) what do you think the best way to implement the tabs is it TabControl or is there another way.
2) how do I make to click on a label next to each tab and I open the new tab with a label next to it. So I can open a third one and so on.
I found this code, but it does not add dynamically and it add the second tab with leaving the label on the first tab
this.tabControl1.SelectedTab = tabPage2;
1) i made a tabcontrol and deleted all the tabs in the form
2)i made a button look like a plus and one looks like a minus and added this code:
int Counter = 1;
this.tabControl1.TabPages.Add("Page " + Counter);
this.tabControl1.SelectTab(Counter - 1);
Counter = Counter + 1;
this will add a new tab with title page (1,2,3,4,..,n) and then i put a code when i press go to the specified Url:
RequestAndResponsHelper RS = new RequestAndResponsHelper(Url.Text);
StringBuilder s = new StringBuilder();
s = RS.GetRequest();//get the request from a different class
string HtmlString = s.ToString();
rtb = new RichTextBox();
rtb.AppendText(HtmlString);
rtb.Name = "RichText";
rtb.Dock = DockStyle.Fill;
this.tabControl1.SelectedTab.Controls.Add(rtb);
Ok, I hope I can explain this well enough.
I have one or more third party Up/Down Spinner+Textbox controls on my page that are black boxes that I can't change the source for.
I want the user to change the UpDownControl contents to choose a quantity and then click a calendar button which will:
Add the quantity of all Up/Down boxes.
Call a javascript popup to display a calendar with the count from step 1 in the url "...calendar.asp?qty=5".
My problem is getting the two steps to execute in the same click. As it stands I can click the button once and it counts
the items and adds them to the popup string and then I have to click it a second time to actually execute the JS popup window.
The code was originally written to "load up" the counts into a second button and then programmatically click it but that looks
like a popup to the browsers since the user didn't click that button.
Here is what I have so far that almost works --
On my page:
<asp:ImageButton ID="btnPrepCal" runat="server" Text="PrepCal" OnClick="btnPrepCal_Click" ImageUrl="~/images/Calendar.gif"/>
In code behind:
public void btnPrepCal_Click(object sender, EventArgs e)
{
StringBuilder sbParams = new StringBuilder();
int TotalQty = 0;
int basketItemCount = 0;
int rowIndex = 0;
string Sku = string.Empty;
foreach (GridViewRow varRow in VariantGrid.Rows)
{
int qnty = GetControlValue(varRow, "Quantity", 0);
if (qnty > 0)
{
basketItemCount++;
string optionList = (string)VariantGrid.DataKeys[rowIndex].Value;
ProductVariant variant = _VariantManager.GetVariantFromOptions(optionList);
if (variant != null)
{
BasketItem basketItem = GetBasketItem(optionList, varRow);
if (basketItem != null)
{
TotalQty += basketItem.Quantity;
Sku = variant.Sku;
}
}
}
rowIndex++;
}
if(Sku.Length > 4) Sku = Sku.Substring(0,4);
sbParams.Append(string.Format("?sku={0}&Qty={1}", Sku, TotalQty));
string popup = string.Empty;
popup = string.Format("window.open('http://trustedtours.org/store/egalaxycalendar.asp{0}','Reservation Calendar','width=265,height=465')",sbParams.ToString());
btnPrepCal.OnClientClick = popup;
}
I'm new to .NET and web programming so I'm probably going at it totally backwards so any help is appreciated. I apologize if it's not clear what I'm trying to do or how. If you need any more info please ask - the rest of the file is a lot of shopping cart mumbo jumbo so I left it out, I hope it's enough.
---- update ----
After looking at the referenced pages I get:
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
StringBuilder cstext1 = new StringBuilder();
cstext1.Append("<script type=text/javascript>" + popup + "<script>");
cs.RegisterStartupScript(cstype, "PopupCalendar", cstext1.ToString());
And I believe this is added after I set the value of popup near the bottom of my Click handler above, removing the OnClientClick part, right?
Should this popup the other window on a page reload after clicking the button? (I hate being a newb and asking what's probably obvious questions.)
You can accomplish what you're aiming for using the ClientScriptManager.RegisterStartupScript method. Instead of assigning the OnClientClick method of the button to your JS popup code, set that code to run when the page is reloaded using the RegisterStartupScript method.
This page has some good examples: http://dotnetslackers.com/articles/aspnet/JavaScript_with_ASP_NET_2_0_Pages_Part1.aspx
Ken is correct. To add to his answer and clarify why your code was not working - you were assigning the click-handler of your button to do a popup, but only after it was clicked. This is why you only saw the popup after the 2nd click - the handler was not there the first time you clicked it.