I need to create a simple input box on my website in C#.
It should pop up, when i call it in the code like that
String input = InputBox("Name the file");
and then I need the use string the users enters later in the code
In a .net application, it is pretty easy to accomplish, but how can i make it work in a web application? I think it should be possible with ajax, but it seems pretty complicated for such a (seemingly) trivial thing.
Is there any kind of library or framework, that I can use for this right away?
thanks in advance
It sounds to me like the behavior you are looking for is to get a popup window with a text box for the user to enter a value and click ok. Is that right?
You're right in saying that it is more complicated with a web app. In a windows app, whenever you run the C# code, whatever happens, happens at that moment. It's pretty straightforward. However, in a web app, all of the C# runs before the page is even rendered in the browser. Therefore, C# in web forms can't really pop up a window.
In order to get a popup, you'll need to do that with JavaScript. The textbox inside the popup should be an <asp:Textbox> control. You can use the Ajax Control Toolkit if youre most comfortable with .NET controls. If youre comforatble with jQuery, you should check out jQuery UI.
I'm assuming you use webforms.
The most simple thing is to make a webform with one input box (<asp:textbox runat="server" id="inputfield" />). Add a button with an onclick event (<asp:button runat="server" id="button" onclick="OnClick" />. In the onclick eventhandler you do something with the value.
protected void OnClick(object sender, EventArgs args){
string input = inputfield.Text;
// do something
}
It should pop up, when i call it in the code like that
And when exactly is this? Keep in mind that there's a fundamental difference between the disconnected nature of web development vs. application development. All of your server-side C# code has already finished executing before the web page renders in the browser. So when are you going to call this code? Also, how are you going to pass the data back to the server? A form post? An AJAX call?
If you want it to "pop up" and to post back with AJAX, I recommend the jQuery UI Dialog as the actual pop-up. Then on its close event you can make the AJAX call to the server to post the data.
If you are looking for a simple solution for a POPUP that will get user input, I recomend checking out JQuery's dialog widget. In particular the modal form, here is a link to some more information: http://jqueryui.com/demos/dialog/#modal-form
ASP.NET has a TextBox control that does just this. All items with runat="server" are accessible through server-side code.
public class InputBox
{
public static DialogResult Show(string title, string promptText, ref string value)
{
return Show(title, promptText, ref value, null);
}
//Fuction
public static DialogResult Show(string title, string promptText, ref string value,
InputBoxValidation validation)
{
Form form = new Form();
Label label = new Label();
TextBox textBox = new TextBox();
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
if (validation != null)
{
form.FormClosing += delegate(object sender, FormClosingEventArgs e)
{
if (form.DialogResult == DialogResult.OK)
{
string errorText = validation(textBox.Text);
if (e.Cancel = (errorText != ""))
{
MessageBox.Show(form, errorText, "Validation Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
textBox.Focus();
}
}
};
}
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
}
public delegate string InputBoxValidation(string errorMessage);
private void button_updations_Click(object sender, EventArgs e)
{
InputBoxValidation validation = delegate(string val)
{
if (val == "")
return "Value cannot be empty.";
if (!(new Regex(#"^[a-zA-Z0-9_\-\.]+#[a-zA-Z0-9_\-\.]+\.[a-zA-Z]{2,}$")).IsMatch(val))
return "Email address is not valid.";
return "";
};
string value = "";
if (InputBox.Show("Enter your email address", "Email address:", ref value, validation) == DialogResult.OK)
{
if (value == "thazime7#gmail.com")
{
dataGridView1.Visible = true;
button_delete.Visible = true;
button1.Visible = true;
button_show.Visible = true;
label6.Visible = true;
label4.Visible = true;
label5.Visible = true;
textBox_uemail.Visible = true;
textBox_uname.Visible = true;
textBox_upassword.Visible = true;
textBox_delete.Visible = true;
button_deleteTable.Visible = true;
button_updatep.Visible = true;
textBox_updateall.Visible = true;
}
MessageBox.Show(value);
}
else
{
MessageBox.Show("You are not authenticated");
}
}
Related
How can I implement custom dialog result for the following code, what all the changes I need to make in the following code to get the dialog result?
private void addButton(enumMessageButton MessageButton)
{
switch (MessageButton)
{
case enumMessageButton.OK:
{
//If type of enumButton is OK then we add OK button only.
Button btnOk = new Button(); //Create object of Button.
btnOk.Text = "OK"; //Here we set text of Button.
btnOk.DialogResult = DialogResult.OK; //Set DialogResult property of button.
btnOk.FlatStyle = FlatStyle.Popup; //Set flat appearence of button.
btnOk.FlatAppearance.BorderSize = 0;
btnOk.SetBounds(pnlShowMessage.ClientSize.Width - 80, 5, 75, 25); // Set bounds of button.
pnlShowMessage.Controls.Add(btnOk); //Finally Add button control on panel.
}
break;
case enumMessageButton.OKCancel:
{
Button btnOk = new Button();
btnOk.Text = "OK";
btnOk.DialogResult = DialogResult.OK;
btnOk.FlatStyle = FlatStyle.Popup;
btnOk.FlatAppearance.BorderSize = 0;
btnOk.SetBounds((pnlShowMessage.ClientSize.Width - 70), 5, 65, 25);
pnlShowMessage.Controls.Add(btnOk);
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.DialogResult = DialogResult.Cancel;
btnCancel.FlatStyle = FlatStyle.Popup;
btnCancel.FlatAppearance.BorderSize = 0;
btnCancel.SetBounds((pnlShowMessage.ClientSize.Width - (btnOk.ClientSize.Width + 5 + 80)), 5, 75, 25);
pnlShowMessage.Controls.Add(btnCancel);
}
break;
}
}
internal static void ShowBox(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton)
{
frmShowMessage frmMessage = new frmShowMessage();
frmMessage.setMessage(messageText);
frmMessage.Text = messageTitle;
frmMessage.addIconImage(messageIcon);
frmMessage.addButton(messageButton);
frmMessage.ShowDialog();
}
Main.cs
frmShowMessage.ShowBox("This is message box which represent message with title, custome button and custom icon.", "This is message title", enumMessageIcon.Question, enumMessageButton.OKCancel);
Now how do further I implement code to get dialog result?
Instead of
internal static void ShowBox(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton)
{
frmShowMessage frmMessage = new frmShowMessage();
frmMessage.setMessage(messageText);
frmMessage.Text = messageTitle;
frmMessage.addIconImage(messageIcon);
frmMessage.addButton(messageButton);
frmMessage.ShowDialog();
}
try this
internal static DialogResult ShowBox(string messageText, string messageTitle, enumMessageIcon messageIcon, enumMessageButton messageButton)
{
frmShowMessage frmMessage = new frmShowMessage();
frmMessage.setMessage(messageText);
frmMessage.Text = messageTitle;
frmMessage.addIconImage(messageIcon);
frmMessage.addButton(messageButton);
return frmMessage.ShowDialog();
}
You have the formMessage object, adding controls and calling showDialog().
Note that when the ShowDialog returns, you still have the object frmMessage and
you can access the methods and properties on that object.
So when the corresponding buttons are clicked, you could set a property in your
fromMessage class on what button is pressed. Perhaps you have some additional fields that you can set.
After the showDialog returns, you can access those properties, perhaps construct your own DialogResult and return that result
I'm designing an article editor for my company and I'd like to be able to show a live preview of the article in a separate WebBrowser window/control. The WebBrowser control needs to refresh the page every time the user changes anything in one of the fields for the article.
Previously, I had the WebBrowser control on the same form, but for space reasons, I had to break it out onto a separate form and access it using a button on the editor form. However, since I moved that control into a separate form, the WebBrowser gains focus on every refresh, meaning I can type one character and then I have to click back to the textbox I was typing in.
My question: Is there a way to refresh that preview page in the background without it stealing the focus so that I can update the preview to reflect what the user is typing without interrupting the user while typing?
Here are the methods for showing and refreshing the preview, respectively:
private void buttonShowPreview_Click(object sender, EventArgs e)
{
if (buttonShowPreview.Tag == null)
{
Form browserForm = new Form();
browserForm.FormClosing += new FormClosingEventHandler(delegate(Object form, FormClosingEventArgs args)
{
if (args.CloseReason == CloseReason.UserClosing)
{
args.Cancel = true;
browserForm.Hide();
previewShowing = false;
}
});
browserForm.Size = new System.Drawing.Size(1024, 768);
browserForm.DesktopLocation = new System.Drawing.Point(0, 0);
browserForm.Text = "Article Preview";
preview = new WebBrowser();
browserForm.Controls.Add(preview);
preview.Dock = DockStyle.Fill;
preview.Navigate("about:blank");
buttonShowPreview.Tag = browserForm;
}
Form previewForm = buttonShowPreview.Tag as Form;
previewForm.Show();
previewShowing = true;
RefreshPreview();
}
private void RefreshPreview(string jumpToAnchor)
{
if (preview != null)
{
preview.Document.OpenNew(true);
preview.Document.Write(structuredContent.GetStructuredContentHTML(content, jumpToAnchor, false));
preview.Refresh();
}
}
Based on the answer by Robberechts here, try disabling the parent Form, updating your WebBrowser, then re-enabling the parent Form again in the DocumentCompleted() event:
private void buttonShowPreview_Click(object sender, EventArgs e)
{
if (buttonShowPreview.Tag == null)
{
Form browserForm = new Form();
browserForm.FormClosing += new FormClosingEventHandler(delegate(Object form, FormClosingEventArgs args)
{
if (args.CloseReason == CloseReason.UserClosing)
{
args.Cancel = true;
browserForm.Hide();
}
});
preview = new WebBrowser();
preview.DocumentCompleted += preview_DocumentCompleted; // handle the DocumentCompleted() event
browserForm.Controls.Add(preview);
preview.Dock = DockStyle.Fill;
preview.Navigate("about:blank");
buttonShowPreview.Tag = browserForm;
}
Form previewForm = buttonShowPreview.Tag as Form;
previewForm.Size = new System.Drawing.Size(1024, 768);
previewForm.DesktopLocation = new System.Drawing.Point(0, 0);
previewForm.Text = "Article Preview";
RefreshPreview();
previewForm.Show();
}
private void RefreshPreview(string jumpToAnchor)
{
if (preview != null && preview.Parent != null)
{
preview.Parent.Enabled = false; // disable parent form
preview.Document.OpenNew(true);
preview.Document.Write(structuredContent.GetStructuredContentHTML(content, jumpToAnchor, false));
preview.Refresh();
}
}
private void preview_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
if (wb.Parent != null)
{
wb.Parent.Enabled = true; // re-enable parent form
}
}
I have a radio buttons and one text box on a panel made dynamically . Now, I want to disable the text box when the second radio button is checked, means they are both connected. how can I make the event for it. I want to have the event working.
Thanks a lot in advanced.
this is my code which is not working:
Panel pnl = new Panel();
pnl.Name = "pnl_";
pnl.Size = new Size(630, 80);
RadioButton rd = new RadioButton();
rd.Name = "rd_" + dr[i]["Value_Name"].ToString();
rd.Text = dr[i]["Value_Name"].ToString();
rd.Location = new Point(i,i*2);
pnl.Controls.Add(rd);
TextBox txt = new TextBox();
txt.Name = "txt_" + Field_Name+"_"+dr[i]["Value_Name"].ToString();
txt.Size = new Size(171, 20);
txt.Text = Field_Name + "_" + dr[i]["Value_Name"].ToString();
txt.Location = new Point(20, 30);
pnl.Controls.Add(txt);
////// ???? ////////
rd.CheckedChanged += new EventHandler(eventTxt(txt));
void eventTxt(object sender,EventArgs e,TextBox txt)
{
RadioButton rd = (RadioButton)sender;
txt.Enabled = rd.Checked;
}
Use a lambda to close over the relevant variable(s):
rd.CheckedChanged += (s, args) => txt.Enabled = rd.Checked;
If you had more than a one line implementation, you could call out to a method accepting whatever parameters you've closed over, instead of including it all inline.
I would suggest to set the Tag of the radio button and get walk down the dependencies.
rd.Tag = txt;
In the event handler use this:
TextBox txt = (sender as Control).Tag as TextBox;
txt.Enabled = ...
you can use code given
rd.CheckedChanged += (s,argx) => txt.Enabled = rd.Checked;
Here's how you could create an event for it:
//if you are using Microsoft Visual Studio, the following
//line of code will go in a separate file called 'Form1.Design.cs'
//instead of just 'Form1.cs'
myTextBox.CheckChangedEventHandeler += new EventHandeler(checkBox1_CheckChanged); //set an event handeler
public void checkBox1_CheckChanged(object sender, EventArgs e) //what you want to happen every time the check is changed
{
if(checkBox1.checked == true) //replace 'checkBox1' with your check-box's name
{
myTextBox.enabled = false; //replace 'myTextbox' with your text box's name;
//change your text box's enabled property to false
}
}
Hope it helps!
I want to display a textbox inside my Message dialog to take user inputs & click OK button to retrieve textbox value in my MainPage.cs,
private async void join_btn_Click(object sender, RoutedEventArgs e)
{
var messageDialog = new MessageDialog(" Enter your secure code Here");
messageDialog.Title = "Join session";
messageDialog.Commands.Add(new UICommand(
"OK",
new UICommandInvokedHandler(this.CommandInvokedHandlerOKFunction)));
messageDialog.DefaultCommandIndex = 0;
messageDialog.CancelCommandIndex = 1;
await messageDialog.ShowAsync();
}
Any suggestion about how to do it??
Instead of using MessageDialog you can use InputBox. Here you can get the textbox value. try the below code,
string message, title, defaultValue;
string myValue;
message = "Enter Message Here :";
title = "Title Name";
myValue = Interaction.InputBox(message, title, defaultValue, 450, 450);
this myvalue string is return what ever your entered in the input Textbox value.
i hope this will help you.
I have a desktop application in C# .In this I have a form in which I show different messages .
I have one message who say : "The output file was generated in : C:\Work\result.txt".
How can I show this path to the file as a link and when the form with this message is shown to see the path as a link and when the user click the link to open the specified path/file?
I tried :
The output file was generated in : C:\Work\result.txt
But doesn't work.
Thanks !
You can have an event for on-onclick, and then you can open file using the below code.
System.Diagnostics.Process.Start(#"C:\Work\result.txt"); //or like
System.Diagnostics.Process.Start(#"C:\Work\result.docx");
Here, the default program must be there for the file. Then only shell will run associated program reading it from the registry, like usual double click does in explorer.
MessageBox.Show() method takes the caption, text, icon, buttons and default button of the dialog.However there's nothing mentioned in the .NET Framework documentation that says anything about adding links to a MessageBox
However, you can do the effect you want by creating a new class inherited from System.Windows.Forms.Form and add a button (or more if you like), an icon, a label and a LinkButton. Then use the ShowDialog() Method of the Form class to display the message box in modal form. You may also create a class called MyErrorBox (static class in C# 2 or just sealed in C# 1) that contains only one static method called Show() which creates a form, adds the needed controls and displays the form in modal mode. A demonstration of the last method is shown below. Then you can use this class whenever you want and wherever you please!
using System;
using System.Windows.Forms;
using System.Drawing;
namespace MessageBoxes{
public sealed class MyErrorBox{
private MyErrorBox(){}
private static Form frm;
private static string detailsStore;
private static TextBox txt;
public static DialogResult Show(string caption, string text, string details, Icon icon){
frm = new Form(); frm.Size = new Size(510, 195);
frm.Text = caption; frm.ShowInTaskbar = false; frm.ControlBox = false;
frm.FormBorderStyle = FormBorderStyle.FixedDialog;
PictureBox icon1 = new PictureBox(); icon1.Location = new Point(8,16);
icon1.Size = new Size(icon.Width, icon.Height);
icon1.Image = icon.ToBitmap();
frm.Controls.Add(icon1);
Label lbl = new Label(); lbl.Text = text; lbl.Location = new Point(88,8);
lbl.Size = new Size(400,88); frm.Controls.Add(lbl);
LinkLabel btn1 = new LinkLabel(); btn1.Text = "View Details";
btn1.Size = new Size(72,23); btn1.Location = new Point(416,96);
btn1.Click += new EventHandler(btn1_Click); frm.Controls.Add(btn1);
//Ofcourse you can add more buttons than just the ok with more DialogResults
Button btn2 = new Button(); btn2.Text = "&Ok";
btn2.Size = new Size(72,23); btn2.Location = new Point(224,130);
btn2.Anchor = AnchorStyles.Bottom; frm.Controls.Add(btn2);
frm.AcceptButton = btn2; btn2.Click += new EventHandler(btn2_Click);
btn2.DialogResult = DialogResult.OK; detailsStore = details;
return frm.ShowDialog();
}
private static void btn1_Click(object sender, EventArgs e) {
frm.Size = new Size(510,320);
txt = new TextBox(); txt.Multiline = true;
txt.ScrollBars = ScrollBars.Both; txt.Text = detailsStore;
txt.Size = new Size(488,128); txt.Location = new Point(8,120);
txt.ReadOnly = true; frm.Controls.Add(txt);
LinkLabel lnk = (LinkLabel)(sender); lnk.Text = "Hide Details";
lnk.Click -= new EventHandler(btn1_Click);
lnk.Click += new EventHandler(btn1_ReClick);
}
private static void btn2_Click(object sender, EventArgs e) {
frm.Close();
}
private static void btn1_ReClick(object sender, EventArgs e) {
frm.Controls.Remove(txt); frm.Size = new Size(510, 195);
LinkLabel lnk = (LinkLabel)(sender); lnk.Text = "View Details";
lnk.Click -= new EventHandler(btn1_ReClick);
lnk.Click += new EventHandler(btn1_Click);
}
}
}
There's no standard MessageBox functionality that'll be able to do this via link label. What I suggest is you use the Yes/No Messagebox buttons and from the option selected you then apply an event
Something like this:
if (MessageBox.Show(
"The file is saved at the following link: link here", "Success", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk
) == DialogResult.Yes)
{
System.Diagnostics.Process.Start(#"C:\TestLocation\SavedFiles");
}