I try to build an application using WinForms and I need something like a JDialog frame to insert several TextBoxes in it (JTextField in Java) along with two buttons (OK and Cancel), but I haven't found yet any appropriate Windows form. Any suggestion?
There is no prompt dialog box in C#. You can create a custom prompt box to do this instead.
public static class Prompt
{
public static int ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 100;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox);
prompt.ShowDialog();
return (int)inputBox.Value;
}
}
Then call it using:
int promptValue = Prompt.ShowDialog("Test", "123");
I got this from here
Or use this:
using( MyDialog dialog = new MyDialog() )
{
DialogResult result = dialog.ShowDialog();
switch (result)
{
// put in how you want the various results to be handled
// if ok, then something like var x = dialog.MyX;
}
}
Related
I think the extra wrinkles here make this question not a duplicate of other centering questions.
I would like to use the approach of #BSharp given here for a self-closing message box:
var w = new Form() { Size = new Size(0, 0) };
Task.Delay(TimeSpan.FromSeconds(10))
.ContinueWith((t) => w.Close(), TaskScheduler.FromCurrentSynchronizationContext());
MessageBox.Show(w, message, caption);
However, in my two-monitor setup, the application is running on the right monitor and the message box is being displayed on the left monitor. How to get it to display in the same location but on the right monitor?
private static Form CreateDummyForm(Form mainForm) {
Form dummy = new Form(); // { Opacity = 0, ShowInTaskbar = false };
dummy.Location = mainForm.Location;
IntPtr hwnd = dummy.Handle; // force handle creation
mainForm.Load += delegate {
IntPtr blah = dummy.Handle;
};
mainForm.LocationChanged += delegate {
dummy.Location = mainForm.Location;
};
return dummy;
}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form mf = new Form() { Size = new Size(400, 400) };
Button btn = new Button { Text = "Button" };
Form dummy = CreateDummyForm(mf);
btn.Click += delegate {
Task.Delay(TimeSpan.FromSeconds(5)).ContinueWith((t) => { dummy.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
MessageBox.Show(dummy, "blah", "blah", MessageBoxButtons.OKCancel);
dummy = CreateDummyForm(mf); // Close disposes the dummy form
};
mf.Controls.Add(btn);
Application.Run(mf);
}
This is a C# web form project I am starting with after a long time away from IDE coding...
I am trying to make a simple custom dialog box class. This is my code.
public static class Dialogo
{
public static int show ()
{
Form dialogo = new Form();
dialogo.Width = 300;
dialogo.Height = 300;
Button btnSim = new Button() { Text = "Sim", Left = 30, Width = 100 };
Button btnNao = new Button() { Text = "Não", Left = 150, Width = 100 };
dialogo.Controls.Add(btnSim);
dialogo.Controls.Add(btnNao);
dialogo.ShowDialog();
// the following two lines are the problematic ones
btnSim += new EventHandler(btnSim_Click);
btnNao += new EventHandler(btnNao_Click);
return -1;
}
}
It's underlining the text within parenthesis and the message says:
The name btnSim_Click' does not exist in the current context
The problem is that I tried to add the following in my code but it doesn't let me put it anywhere (it always says that is something wrong):
private int btnNao_Click (object sender, EventArgs e)
{
return 0;
}
private int btnSim_Click (object sender, EventArgs e)
{
return 1;
}
My objective is that each of the both buttons btnSim and btnNao return a different value (say 1 and 0).
What am I doing wrong?
EventHandler is a delegate for a method that returns void.
Your methods return int.
Try something like this:
public static int show()
{
int returnValue = -1;
using (Form dialogo = new Form())
{
dialogo.Width = 300;
dialogo.Height = 300;
Button btnSim = new Button() { Text = "Sim", Left = 30, Width = 100 };
Button btnNao = new Button() { Text = "Não", Left = 150, Width = 100 };
dialogo.Controls.Add(btnSim);
dialogo.Controls.Add(btnNao);
btnSim.Click += (s, e) => { returnValue = 0; dialogo.DialogResult = DialogResult.OK; };
btnNao.Click += (s, e) => { returnValue = 1; dialogo.DialogResult = DialogResult.OK; };
dialogo.Disposed += (s, e) =>
{
btnSim?.Dispose();
btnSim = null;
btnNao?.Dispose();
btnNao = null;
};
dialogo.ShowDialog();
}
return returnValue;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a class called "AddChips" it's basically Prompt Dialog with some buttons and a TextBox the user enter's some data in it and i want to get the data that he just entered and send it back to some other class let's say "Form1" and give the data to integer called Chips
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 150;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
Button leaveApp = new Button() { Text = "No", Left = 250, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { new Form1() { Chips = int.Parse(textBox.Text) }; };
leaveApp.Click += (sender, e) => { Application.Exit(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(leaveApp);
prompt.Controls.Add(textLabel);
textLabel.Width = 300;
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
I have used on prompt form Like this:
public string ValueIWant { get; set; }
private void btnSetValueIWant_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtbxValue.Text))
{
ValueIWant= txtbxValue.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
Close();
}
}
and on the other form i get Value:
frmValue f= new frmValue ();
if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string ValueIWantFromProptForm=f.ValueIWant ;
}
It Works for me.
So for this assignment in class I'm making a richtextbox editor in C# winforms, in this assignment I have to include a find function, but I can't seem to get the hang of it
EDIT: it closes every time I click the search button, but that's not what I want, I want it to close when the user closes the actual dialog via the X in the top right, the search button should just highlight the found string by using the .Find() method
Here's my code up until now:
private void zoekenToolStripMenuItem_Click(object sender, EventArgs e)
{
string searchValue = SearchDialog();
Search(searchValue);
}
public string SearchDialog()
{
Form findDialog = new Form();
findDialog.Width = 500;
findDialog.Height = 142;
findDialog.Text = "Zoeken";
Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.ShowDialog();
return (string)inputBox.Text;
}
void Search(string searchValue)
{
rtxtInhoud.Find(searchValue);
}
The part:
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
is what I'm really stuck on
Thanks in advance
EDIT: here's something that I tried to do, which didn't work
public string SearchDialog()
{
Form findDialog = new Form();
findDialog.Width = 500;
findDialog.Height = 142;
findDialog.Text = "Zoeken";
Label textLabel = new Label() { Left = 10, Top = 20, Text = "Zoek naar:", Width = 100 };
TextBox inputBox = new TextBox() { Left = 150, Top = 20, Width = 300};
Button search = new Button() { Text = "Zoek", Left = 350, Width = 100, Top = 70 };
Button findNext = new Button() { Text = "Volgende", Left 250, Width = 100, Top = 70};
search.Click += (object sender, EventArgs e) => { rtxtInhoud.Find(inputBox.Text); };
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.ShowDialog();
return (string)inputBox.Text;
}
This waits for the Dialog to be closed before highlighting the found string. Which is NOT what I want, I want it to keep the Dialog open, but still highlight the text
it closes every time I click the search button
Yes, that's because you added an event handler to the search button that closes the form:
search.Click += (object sender, EventArgs e) => { findDialog.Close(); };
but that's not what I want, I want it to [...]
Then write the code in that handler such that it does what you actually want it to do, instead of having it close the form.
In your main form code, create a method that searches your text and highlights a found string. Then hook this method up to your search dialog button like the sample below. Also, I think you should call form.Show() instead of .ShowDialog(), so the other form can respond to input.
private void HighlightString(string stringToHighlight)
{
// Code here to search your text and highlight a string.
}
private void SearchDialog()
{
var findDialog = new Form {Width = 500, Height = 142, Text = "Zoeken"};
var textLabel = new Label() {Left = 10, Top = 20, Text = "Zoek naar:", Width = 100};
var inputBox = new TextBox() {Left = 150, Top = 20, Width = 300};
var search = new Button() {Text = "Zoek", Left = 350, Width = 100, Top = 70};
search.Click += (object sender, EventArgs e) => HighlightString(inputBox.Text);
findDialog.Controls.Add(search);
findDialog.Controls.Add(textLabel);
findDialog.Controls.Add(inputBox);
findDialog.Show();
}
Use this code:
Application.Exit();
I need to create a sort of a dialog box or something like a popup screen. I have this array of items and then I need to create a buttons for each of them on the dialogbox so that i could navigate with a button click.
Whats the best way to do it in C#? can someone guide me on this please
If you are using winforms, then place FlowLayoutPanel on your form. Then add all controls to it at runtime.
foreach(var item in items)
{
Button button = new Button();
// setup button properties
// subscribe to events
flowLayoutPanel.Controls.Add(button);
}
FlowLayoutPanel will arrange your controls automatically.
Consider that you Dialog or similar parent element is called sp and ar is the array of elements that you want to use to create the buttons:
for(YourObject obj : ar)
{
System.Windows.Controls.Button newBtn = new Button();
newBtn.Content = obj.YourProperty;
newBtn.Name = "Button" + obj.YourProperty;
sp.Children.Add(newBtn);
}
I was playing with some concepts and one part of it seem to be exactly example of creating dynamic dialog. You can add dynamic button creation to it, combine it and properly format it using table or flow layout panel
DialogResult result;
using (var popup = new Form())
{
popup.Size = new Size(1000, 500);
popup.Location = new Point(Convert.ToInt32(this.Parent.Width / 2) - 500, Convert.ToInt32(this.Parent.Height / 2) - 250);
popup.FormBorderStyle = FormBorderStyle.FixedDialog;
popup.MinimizeBox = false;
popup.MaximizeBox = false;
popup.Text = "My title";
var lbl = new Label() { Dock = DockStyle.Top, Padding = new Padding(3), Height = 30 };
lbl.Font = new Font("Microsoft Sans Serif", 11f);
lbl.Text = "Do you want to Continue?";
// HERE you will add your dynamic button creation instead of my hardcoded
var btnYes = new Button { Text = "Yes", Location = new Point(700, 400) };
btnYes.Click += (s, ea) => { ((Form)((Control)s).Parent).DialogResult = DialogResult.Yes; ((Form)((Control)s).Parent).Close(); };
var btnNo = new Button { Text = "No", Location = new Point(900, 400) };
btnNo.Click += (s, ea) => { ((Form)((Control)s).Parent).DialogResult = DialogResult.No; ((Form)((Control)s).Parent).Close(); };
popup.Controls.AddRange(new Control[] { lbl, btnYes, btnNo });
result = popup.ShowDialog(this);
}
if (result == DialogResult.Yes)
{
// do this
}
else
{
// do that
}
This was example of how to create dynamic dialog with dialog result output.