How to create windows forms dialog with dynamic number of buttons? - c#

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.

Related

How to save state of Windows Form Application (or save data in text boxes with arrays)

I have been working a program that is used as a guide/way to memorize items when studying terms for whatever (test, exam, etc.). It generates a set amount of textboxes inside of a group box (which has the subject name set as its text property) in which you can write the term name and definition. I was wondering how I would save what was written in the text boxes after being generated. Perhaps being able to save the state of the application after pressing save and being able to set the application to its previous state when wanted (Kind of like snapshots that you use in virtual machines). Another way I thought of is to perhaps make a group of each subject somehow and within that store an array of text in each term name box and the associated term definition. Here is the code inside of the button I press to generate the text boxes. There is also a photo of the form: Photo of form Here is one of the program running: Image of Program running Edit: I am not asking for the straight up entire code. I would like just a guideline/idea of how I would go about doing this.
GroupBox groupBox1 = new GroupBox();
TextBox textTest = new TextBox();
textTest.Location = new Point(15, 40);
groupBox1.Controls.Add(textTest);
Button buttonForBoxes = new Button();
NumericUpDown numberUpDown1 = new NumericUpDown();
groupBox1.Controls.Add(buttonForBoxes);
buttonForBoxes.Location = new Point(140, 40);
buttonForBoxes.Text = "moretext";
numberUpDown1.Location = new Point(15, 15);
groupBox1.Controls.Add(numberUpDown1);
groupBox1.AutoSize = true;
var numVal = numericUpDown1.Value;
var numDo2 = 40;
var numDo1 = 120;
var inSubjectBox = subjectBox.Text;
//Makes boxes however many times you specify
for (int i = 0; i < numVal; i++)
{
numDo2 += 110;
TextBox text1 = new TextBox();
text1.Location = new Point(15, numDo1);
groupBox1.Controls.Add(text1);
numDo1 += 110;
TextBox textThing = new TextBox();
textThing.Location = new Point(15, numDo2);
textThing.Multiline = true;
textThing.Size = new System.Drawing.Size(600, 60);
groupBox1.Controls.Add(textThing);
}
// Set the Text and Dock properties of the GroupBox.
groupBox1.Text = inSubjectBox;
groupBox1.Dock = DockStyle.Top;
// Enable the GroupBox (which disables all its child controls)
groupBox1.Enabled = true;
// Add the Groupbox to the form.
this.Controls.Add(groupBox1);
Maybe you can try to save the textbox info into Settings.
First, go to Project -> Properties -> Settings and add new items(type of StringCollection) in Settings.
Then, modify the code like this(save the location of the TextBox in the format of "x;y"):
private void Addtextbox_Click(object sender, EventArgs e)
{
Properties.Settings.Default.text1Collection.Clear();
Properties.Settings.Default.textThingCollection.Clear();
var numVal = 2;
// code omitted
// ...
for (int i = 0; i < numVal; i++)
{
numDo2 += 110;
TextBox text1 = new TextBox();
text1.Location = new Point(15, numDo1);
groupBox1.Controls.Add(text1);
// save info to Settings
Properties.Settings.Default.text1Collection.Add(String.Format("{0};{1}", text1.Location.X, text1.Location.Y));
numDo1 += 110;
TextBox textThing = new TextBox();
textThing.Location = new Point(15, numDo2);
textThing.Multiline = true;
textThing.Size = new System.Drawing.Size(600, 60);
groupBox1.Controls.Add(textThing);
// save info to Settings
Properties.Settings.Default.textThingCollection.Add(String.Format("{0};{1}", textThing.Location.X, textThing.Location.Y));
// call Save()
Properties.Settings.Default.Save();
}
// code omitted
// ...
}
private void LoadtextboxFromSettings_Click(object sender, EventArgs e)
{
foreach (string text1str in Properties.Settings.Default.text1Collection)
{
TextBox text1 = new TextBox
{
Location = new Point(Convert.ToInt32(text1str.Split(';')[0]), Convert.ToInt32(text1str.Split(';')[1]))
};
groupBox1.Controls.Add(text1);
}
foreach (string textThingstr in Properties.Settings.Default.textThingCollection)
{
TextBox textThing = new TextBox
{
Multiline = true,
Location = new Point(Convert.ToInt32(textThingstr.Split(';')[0]), Convert.ToInt32(textThingstr.Split(';')[1])),
Size = new Size(600, 60)
};
groupBox1.Controls.Add(textThing);
}
}
Besides, if you get the exception System.NullReferenceException: 'Object reference not set to an instance of an object.', try to add a default value for each "setting".
Update:
The way to set Settings default value.
I used to do something similar when storing the positions of forms and the size of some controls on them. Then it was necessary for the application to open on restart in the same form as it was last used.
All forms and controls data I saved to XML file, when closing application. When then application has been started I read this XML file and set positions of forms and controls.

Check all items in a specified checked list box c#

I have created a small kitchen display program that display food orders. So I created dynamically a panel that contains a table layout panel that contains a checked list box and a check all button . My problem is... I have a check all button in each table layout panel created dynamically and every time I click it, it checks all items in the last created CheckedListBox not the clicked one.
This is my code:
p = new Panel();
p.Size = new System.Drawing.Size(360, 500);
p.BorderStyle = BorderStyle.FixedSingle;
p.Name = "panel";
tpanel = new TableLayoutPanel();
tpanel.Name = "tablepanel";
clb = new CheckedListBox();
tpanel.Controls.Add(b1 = new Button() { Text = "CheckAll" }, 1, 4);
b1.Name = "b1";
b1.Click += new EventHandler(CheckAll_Click);
b1.AutoSize = true;
private void CheckAll_Click(object sender, EventArgs e)
{
var buttonClicked = (Button)sender;
var c = GetAll(this, typeof(CheckedListBox));
for (int i = 0; i < c.Count(); i++)
{
\\any help
}
}
public IEnumerable<Control> GetAll(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetAll(ctrl, type)).Concat(controls).Where(c =>
c.GetType() == type);
}
First I will describe the struct
Order = TableLayoutPanel
TableLayoutPanel has 1 CheckAll Button and CheckListBox
And you want when you click to CheckAll Button it will checks exactly all items in current TableLayoutPanel.
So try this code
class XForm : Form {
// create Dictionary to store Button and CheckListBox
IDictionary<Button, CheckListBox> map = new Dictionary<Button, CheckListBox> ();
// when you create new order (new TableLayoutPanel)
// just add map Button and CheckListBox to map
private void CreateOrder () {
var panel = new Panel ();
panel.Size = new System.Drawing.Size (360, 500);
panel.BorderStyle = BorderStyle.FixedSingle;
panel.Name = "panel";
var table = new TableLayoutPanel ();
var checklistBox = new CheckedListBox ();
var button = new Button () { Text = "CheckAll" };
table.Controls.Add (button, 1, 4);
button.Name = "b1";
button.Click += new EventHandler (CheckAll_Click);
button.AutoSize = true;
map[button] = checklistBox;
}
// and on event handle
private void CheckAll_Click (object sender, EventArgs e) {
var buttonClicked = (Button) sender;
var c = map[buttonClicked];
if (c == null) return;
for (int i = 0; i < c.Items.Count; i++)
{
c.SetItemChecked(i, true);
}
}
}
And dont for get remove it from map when remove the order.
Hope it helps

What is the equivalent JDialog for WinForms(Windows Forms)?

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;
}
}

Ok and Cancel Button Not showing

Why my Ok and Cancel Button are not showing up on the UI screen? I see the form, the label and the text box but I can't see the Cancel and OK buttons.
To give you a background I am creating this dialog box programmatically and all I need a a couple of text boxes , and their labels of course. And an OK and Cancel button .
All these sizes that I have used here is by trial and error as I am not much experienced in the UI control area of Visual C# 2010.
public void function x ()
{
var fileNameDialog = new Form();
fileNameDialog.Text = "Save New Name";
Label fileLabel = new Label();
fileLabel.Size = new System.Drawing.Size(150, 40);
fileLabel.Text= "Enter Person Name";
fileNameDialog.Controls.Add(fileLabel);
TextBox fileTextBox = new TextBox();
fileTextBox.Location = new System.Drawing.Point(fileLabel.Location.X + 300, fileLabel.Location.Y);
fileTextBox.Size = new System.Drawing.Size(220, 40);
fileNameDialog.Controls.Add(fileTextBox);
fileTextBox.TextChanged += TextBox_TextChanged;
fileTextBox.Text= textboxValue;
Button okButton = new Button();
okButton.Visible = true;
okButton.Text = "OK";
okButton.Location = new System.Drawing.Point(fileTextBox.Location.X, fileTextBox.Location.Y - 80);
fileNameDialog.Controls.Add(okButton);
okButton.Click += new EventHandler(okButton_Click);
Button cancelButton = new Button();
cancelButton.Visible = true;
cancelButton.Text = "Cancel";
fileNameDialog.Controls.Add(cancelButton);
cancelButton.Click += new EventHandler(cancelButton_Click);
cancelButton.Location = new System.Drawing.Point(fileTextBox.Location.X+50, fileTextBox.Location.Y - 80);
fileNameDialog.ShowDialog();
}
Your fileTextbox.Location.Y is zero, so subtracting 80 puts in above the form.
Try fileTextBox.Bottom + 4 or something like that.
Using the designer to create this dialog form is probably the better route to take. Along with the placement of the controls, you can use Anchors to make the controls relate to the size of the form.

Visual Studio-style undo drop-down button - custom ToolStripSplitButton

I'm looking to implement a Visual Studio-style undo drop-down button:
I've looked all over the internet, and can't seem to find any real implementations of this.
I've started by deriving from ToolStripSplitButton, but don't really know where to go from there. Its DropDown property is a ToolStripDropDown, but that doesn't seem to have anything regarding multiple items being selected, much less scrolling, and the text at the bottom.
So instead of the default ToolStripDropDown, I'm thinking maybe the whole drop down part should be a custom control, based on a combobox. The question then, is how to cause the right-side (drop down arrow) button to do something other than show its default drop down?
Am I on the right track here? Thanks!
Yes, I think you're on the right track. And in this case, ToolStripControlHost is your friend.
You don't necessarily need to derive from it (unless you are making your own control), but try just subscribing to the ToolStripSplitButton's DropDownOpening event:
Working example:
private ListBox listBox1;
public Form1()
{
InitializeComponent();
listBox1 = new ListBox();
listBox1.IntegralHeight = false;
listBox1.MinimumSize = new Size(120, 120); \\ <- important
listBox1.Items.Add("Item 1");
listBox1.Items.Add("Item 2");
}
private void toolStripSplitButton1_DropDownOpening(object sender, EventArgs e) {
ToolStripControlHost toolHost = new ToolStripControlHost(listBox1);
toolHost.Size = new Size(120, 120);
toolHost.Margin = new Padding(0);
ToolStripDropDown toolDrop = new ToolStripDropDown();
toolDrop.Padding = new Padding(0);
toolDrop.Items.Add(toolHost);
toolDrop.Show(this, new Point(toolStripSplitButton1.Bounds.Left,
toolStripSplitButton1.Bounds.Bottom));
}
Here is the result:
For your application, you would need to replace the ListBox with your own UserControl, so you can contain whatever your want in it. The ToolStripControlHost can only hold one control, and it's important to set the MinimumSize property, or else the dropped control isn't sized correctly.
Extra thanks to LarsTech! (I didn't know about ToolStripControlHost a few hours ago)
Here is my implementation, which is really close to the VS drop down...
You should be able to just drop this delegate & function into your Form:
public delegate void UndoRedoCallback(int count);
private void DrawDropDown(ToolStripSplitButton button, string action, IEnumerable<string> commands, UndoRedoCallback callback)
{
int width = 277;
int listHeight = 181;
int textHeight = 29;
Panel panel = new Panel()
{
Size = new Size(width, textHeight + listHeight),
Padding = new Padding(0),
Margin = new Padding(0),
BorderStyle = BorderStyle.FixedSingle,
};
Label label = new Label()
{
Size = new Size(width, textHeight),
Location = new Point(1, listHeight - 2),
TextAlign = ContentAlignment.MiddleCenter,
Text = String.Format("{0} 1 Action", action),
Padding = new Padding(0),
Margin = new Padding(0),
};
ListBox list = new ListBox()
{
Size = new Size(width, listHeight),
Location = new Point(1,1),
SelectionMode = SelectionMode.MultiSimple,
ScrollAlwaysVisible = true,
Padding = new Padding(0),
Margin = new Padding(0),
BorderStyle = BorderStyle.None,
Font = new Font(panel.Font.FontFamily, 9),
};
foreach (var item in commands) { list.Items.Add(item); }
if (list.Items.Count == 0) return;
list.SelectedIndex = 0;
ToolStripControlHost toolHost = new ToolStripControlHost(panel)
{
Size = panel.Size,
Margin = new Padding(0),
};
ToolStripDropDown toolDrop = new ToolStripDropDown()
{
Padding = new Padding(0),
};
toolDrop.Items.Add(toolHost);
panel.Controls.Add(list);
panel.Controls.Add(label);
toolDrop.Show(this, new Point(button.Bounds.Left + button.Owner.Left, button.Bounds.Bottom + button.Owner.Top));
// *Note: These will be "up values" that will exist beyond the scope of this function
int index = 1;
int lastIndex = 1;
list.Click += (sender, e) => { toolDrop.Close(); callback(index); };
list.MouseMove += (sender, e) =>
{
index = Math.Max(1, list.IndexFromPoint(e.Location) + 1);
if (lastIndex != index)
{
int topIndex = Math.Max(0, Math.Min(list.TopIndex + e.Delta, list.Items.Count - 1));
list.BeginUpdate();
list.ClearSelected();
for (int i = 0; i < index; ++i) { list.SelectedIndex = i; }
label.Text = String.Format("{0} {1} Action{2}", action, index, index == 1 ? "" : "s");
lastIndex = index;
list.EndUpdate();
list.TopIndex = topIndex;
}
};
list.Focus();
}
You can set it up and test like this, assuming you have a blank form (Form1) with a toolStrip that has 1 ToolStripSplitButton (toolStripSplitButton1) added:
public Form1()
{
InitializeComponent();
// Call DrawDropDown with:
// The clicked ToolStripSplitButton
// "Undo" as the action
// TestDropDown for the enumerable string source for the list box
// UndoCommands for the click callback
toolStripSplitButton1.DropDownOpening += (sender, e) => { DrawDropDown(
toolStripSplitButton1,
"Undo",
TestDropDown,
UndoCommands
); };
}
private IEnumerable<string> TestDropDown
{
// Provides a list of strings for testing the drop down
get { for (int i = 1; i < 1000; ++i) { yield return "test " + i; } }
}
private void UndoCommands(int count)
{
// Do something with the count when an action is clicked
Console.WriteLine("Undo: {0}", count);
}
Here is a better example using the Undo/Redo system from: http://www.codeproject.com/KB/cs/AutomatingUndoRedo.aspx
public Form1()
{
InitializeComponent();
// Call DrawDropDown with:
// The Undo ToolStripSplitButton button on the Standard tool strip
// "Undo" as the action name
// The list of UndoCommands from the UndoRedoManager
// The Undo method of the UndoRedoManager
m_TSSB_Standard_Undo.DropDownOpening += (sender, e) => { DrawDropDown(
m_TSSB_Standard_Undo,
"Undo",
UndoRedoManager.UndoCommands,
UndoRedoManager.Undo
); };
}
*Note: I did modify the Undo & Redo methods in the UndoRedoManager to accept a count:
// Based on code by Siarhei Arkhipenka (Sergey Arhipenko) (http://www.codeproject.com/KB/cs/AutomatingUndoRedo.aspx)
public static void Undo(int count)
{
AssertNoCommand();
if (CanUndo == false) return;
for (int i = 0; (i < count) && CanUndo; ++i)
{
Command command = history[currentPosition--];
foreach (IUndoRedo member in command.Keys)
{
member.OnUndo(command[member]);
}
}
OnCommandDone(CommandDoneType.Undo);
}
I'd suggest implementing the popup separately from the toolbar button. Popups are separate windows with a topmost-flag which auto-close when losing focus or pressing escape. If you code your own popup window that frees you from having to fit your behaviour to a preexisting model (which is going to be hard in your case). Just make a new topmost window with a listbox and status bar, then you are free to implement the selection behavior on the listbox like you need it.
Vs 2010 is a WPF application. If you are in the beginning of this application development than use WPF as a core technology. WPF drop down button is implemented in WPF ribbon. Source code is available on CodePlex.

Categories