I'm trying to close a dynamically created form with a dynamic button (this is the simplest of my jobs, I am also adding other buttons to do other jobs but I figured this is a good place to start).
As of now I can create the form, button and the click event for that button, but I don't know what to add within the click event function to close the host of that button. I am guessing I can somehow access the buttons parent through the click function? Or maybe pass the form control as an argument in the function? Any help is appreciated!
//Create form
Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;
//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += new EventHandler(saveButton_buttonClick);
//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();
And my click event function looks pretty much normal:
void saveButton_buttonClick(object sender, EventArgs e)
{
}
Unfortunately I don't know what to add for the function to work! Thanks in advance for any help someone can give me! I feel like this should be a straight-forward problem to solve but I haven't been able to figure it out...
While it's certainly possible to do this with a named function, it's generally simpler to just use an anonymous function in cases like this:
Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;
//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += (_,args)=>
{
SaveSnapshot();
};
//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
exitButton.Click += (_,args)=>
{
snapshot.Close();
};
//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();
A simple way is to use a lambda method:
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Click += (s, e) => { shapshot.Close(); };
Related
In my application I dynamically create buttons and add them to a flow control that is later cleared. I have this on a timer to refresh every X seconds to clear then add buttons. This is all being done on the main form.
The problem is when I have a child form launched the main form will steal focus every time the controls are added to the flow control.
Here is the code I have that dynamically clears and adds the controls on the main form.
I call this before adding the controls
flw_users.Controls.Clear();
This is what I call to dynamically create/add the buttons to the flow control.
private void DisplayNewMobileUser(string MobileUserName)
{
// Set Button properties
Button button = new Button();
button.Text = MobileUserName;
button.Size = new System.Drawing.Size(171, 28);
button.Name = MobileUserName;
button.BackColor = System.Drawing.Color.White;
button.FlatAppearance.BorderColor = System.Drawing.Color.White;
button.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold);
button.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button.ForeColor = System.Drawing.Color.Black;
button.Margin = new System.Windows.Forms.Padding(0, 1, 0, 1);
button.TextAlign = System.Drawing.ContentAlignment.TopLeft;
button.Click += new EventHandler(MobileUserName_OnClick);
flw_users.Controls.Add(button);
}
Is there a way to add buttons to a flow control with out it always stealing focus ?
Thanks to LarsTech I researched how to properly dispose each control added to flw_users. The main issues was fixed by changing the OnClick event to change focus to a label, which in turn didn't cause the main form to gain topmost every time the controls were cleared and re added. So everytime I clicked a button that button still had focus while the new form appeared.
Thanks everyone !
Here is the code I used to properly clear the controls
private void ClearUsers()
{
List<Control> ctrls = new List<Control>();
foreach (Control c in flw_users.Controls)
{
ctrls.Add(c);
}
flw_users.Controls.Clear();
foreach (Control c in ctrls)
{
c.Dispose();
}
}
I have UserControls that are "Buttons" for Menu purposes. Those Buttons shall automatically add to the right Panel.
So basically, if I write:
MenueButton button1 = new MenueButton();
the button shall automatically be added to MenuePanel on GUI Form. (Maybe Some easy action handler?)
Is there a way to achieve this?
Try something like
MenueButton button1 = CreateButton();
button1.Click+=...
MenueButton button2 = CreateButton();
button2.Text="ABC";
MenueButton CreateButton()
{
MenueButton b= new MenueButton();
panel.Controls.Add(b);
return b;
}
This way the CreateButton function creates and automatically adds the button to the panel and you can use the newly created button in your code
If you want to do the same thing to the buttons you can add parameters to your function
MenueButton button1 = CreateButton("Button 1 Text");
button1.Click+=...
MenueButton button2 = CreateButton("XYZ");
MenueButton CreateButton(string buttonText)
{
MenueButton b= new MenueButton();
b.Text = buttonText;
panel.Controls.Add(b);
return b;
}
Done button added to pickerview toolbar,
But on click done button click event is not working.
public override void ViewDidLoad(){
myPickerView = new UIPickerView (RectangleF.Empty){
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
ShowSelectionIndicator = true,
Model = model,
Hidden = true,
BackgroundColor = UIColor.Clear
};
toolbar = new UIToolbar();
toolbar.BarStyle = UIBarStyle.Default;
toolbar.Translucent = true;
toolbar.SizeToFit();
// Create a 'done' button for the toolbar and add it to the toolbar
UIBarButtonItem doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done,
(s, e) => {
Console.WriteLine ("Calling Done!");
txt_RegistrationType.Text = selectedType;
txt_Email.ResignFirstResponder();
txt_UserName.ResignFirstResponder();
txt_Password.ResignFirstResponder();
txt_PhoneNumber.ResignFirstResponder();
txt_ConformPassword.ResignFirstResponder();
});
toolbar.SetItems(new UIBarButtonItem[]{doneButton}, true);
model.PickerChanged += (sender, e) => {
Console.WriteLine("selected vlaue {0}",e.SelectedValue);
txt_RegistrationType.Text = e.SelectedValue;
};
this.myPickerView.AddSubview (toolbar);
myPickerView.Frame = PickerFrameWithSize (myPickerView.SizeThatFits (SizeF.Empty));
View.AddSubview (myPickerView);
myPickerView.AddSubview (toolbar);
}
On click selected items
its shows pickerView "PickerView.Hidden = false" which appears picker view and toolbar with done button. When click the done button on toolbar its click event is not working.
Please let me know for getting an event on click done button.
Firstly, I want to point out that you're adding your "toolbar" to your current "UIPickerView" twice. Once with:
this.myPickerView.AddSubview (toolbar);
and another time with:
myPickerView.AddSubview (toolbar);
Secondly, why the toolbar? It only adds complexity and dependencies to your project.
What I suggest, is scrap the whole block of code, and make two separate controls on this view:
A button
A UIPickerView
public override void ViewDidLoad()
{
UIButton button = new UIButton (new RectangleF (5, 5, frame_width, 30));
button.TouchUpInside += Method;
UIPickerView pickerView = new UIPickerView (new RectangleF (5, 45, frame_width, 180));
pickerView.Model = model
this.View.AddSubviews (new UIView[]{button, pickerView});
}
void Method (object sender, eventargs e)
{
Console.WriteLine ("Done");
/*If you want to make the pickerView disappear,
you can add a pickerView.Hide() here, and then call a method
to redraw the controls on the current view.*/
}
I hope this helps. Good luck!
I have a form with a simple calculator with the size 245 x 359.
I currently have a show/hide button for scientific functions.
I want to be able to click a button and have it show the scientific functions.
With the button size 300 x 400.
In your button click event you can do the following:
this.Size = new Size(300,400);
In the designer, just make these buttons, but then drag it's size to how you want it before the specific button is pressed.
Add a event handler for the button you want, then do this:
bool buttonPressed = false;
private void onChangeSizeButton_pressed(EventArgs e, object o)
{
if (buttonPressed)
{
this.Size = new Size(this.Size.Width, DEFAULT HEIGHT HERE);
buttonPressed = false;
}
else
{
this.Size = new Size(this.Size.Width, NEW HEIGHT HERE);
buttonPressed = true;
}
}
In your button click event, set each scentific function to:
btnMC.Visbile = true;
btnMC.Size = new Size(300, 400);
btnMR.Visbile = true;
btnMR.Size = new Size(300, 400);
ect...
That should do the trick. You might want to considered resizing your form when they click the button as well.
In your example, do not need to hide and show button but only need to change text.
Add Fix button on panel or user control and change the text. If require then user Button.Visible = false and true. Share your sample code for work on it.
I've got a page in ASP.NET, and I'm dynamically adding a subclass of WebControls.Button to the Controls data member of a pre-existing static TableCell. The button displays fine in the browser as expected. But when I click the button, the event handler I added for button.Click is not being called. Any suggestions as to why this is?
var controls = this.displaytable.Rows[i].Cells[j].Controls;
var button = new TableButton(j, i);
button.Click += new EventHandler(this.button_Click);
button.UseSubmitBehavior = false;
button.Text = "Available";
controls.Add(button);
Dynamically added buttons must be created on every request, most likely it is sufficient before raising postback events (e.g. OnLoad). Button needs to have an explicit ID sometime:
var controls = this.displaytable.Rows[i].Cells[j].Controls;
var button = new TableButton(j, i);
button.Click += new EventHandler(this.button_Click);
button.UseSubmitBehavior = false;
button.Text = "Available";
button.Id = string.Format("TableButton_{0}_{1}", j, i);
controls.Add(button);
This SO answer may help little bit: ASP.NET dynamic Command Button event not firing.