I am a begginer in C# and I want to create a button which creates button.
but these buttons never appear...
please find my code :
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
}
Thanks a lot
You have to add the button (or any other controls) on the form using the Controls property, for sample:
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
// add control
this.Controls.Add(addstrat3_2);
}
You need to add the button to the form.
this.Controls.Add(addstrat3_2);
Related
when i individual add two forms to two pages controls,
how set form owner?
Because owner is empty, could not set data from form's event to another form's textbox text, how slove it, please help me .
-main form-
//tabpage add controls
private void TestForm2_Shown(object sender, EventArgs e)
{
//var DataUpdate = new DataUpdate();
var DataUpdate = new TestForm3();
DataUpdate.TopLevel = false;
//DataUpdate.Visible = true;
//DataUpdate.Top = 0;
//DataUpdate.Left = 0;
DataUpdate.Dock = DockStyle.Fill;
DataUpdate.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
tabPage1.Controls.Add(DataUpdate);
//var SystemSetting = new SystemSetting();
var SystemSetting = new TestForm4();
SystemSetting.TopLevel = false;
//SystemSetting.Visible = true;
//SystemSetting.Top = 0;
//SystemSetting.Left = 0;
SystemSetting.Dock = DockStyle.Fill;
SystemSetting.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
tabPage2.Controls.Add(SystemSetting);
SystemSetting.Show();
}
// In your TestForm3,
private void button1_Click(object sender, EventArgs e)
{
TestForm4 frm = new TestForm4();
frm.owner=this;
frm.Show();
}
// In your TestForm4,
private void button1_Click(object sender, EventArgs e)
{
TestForm3 mainForm=(TestForm3)this.owner;
mainForm.LabelText = textBox1.Text;
}
private void btnShowCausali_Click(object sender, EventArgs e)
{
DataGridView Dati = new DataGridView();
Dati.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Dati.Location = new System.Drawing.Point(120, 40);
Dati.Name = "Dati";
Dati.RowTemplate.Height = 24;
Dati.Size = new System.Drawing.Size(979, 458);
Dati.TabIndex = 1;
Dati.Visible = true;
Dati.Columns.Add("id", "ID");
Dati.Columns.Add("causaliname", "Nome Causale");
Dati.Columns.Add("Identificationcode", "Codice Identificativo");
Dati.Columns.Add("expired", "Data di Scadenza");
grpDatore.Controls.Add(Dati);
}
When I run this code the DataGridView does not appear on the form. Just in case it could create problem, the button btnShowCausali get created on the form load.
public void Form1_Load(object sender, EventArgs e)
{
Button btnShowCausali = new Button();
btnShowCausali.Text = "Causali";
btnShowCausali.Location = new Point(20, 20);
btnShowCausali.Size = new Size(120, 40);
}
By the way, I don't know why the button actually gets created but the DataGridView does not.
Your event handler btnShowCausali_Click is not attached to the button in Form1_Load. Are you sure it is called?
I also do not see adding this button to any container (Form, Panel, ...)
Your DataGridView will be added to grpDatore control (not form) so have that in mind when you set Location.
Attach event:
btnShowCausali.Click += btnShowCausali_Click;
First of all, if the button is created dynamically, add it to the UI.
Button btnShowCausali = new Button();
btnShowCausali.Text = "Causali";
btnShowCausali.Location = new Point(20, 20); // Make shure there aren't other controls in this point
btnShowCausali.Size = new Size(120, 40);
this.Controls.Add(btnShowCausali); //Adding the button to the form
Then, you have to attach an event handler for the Click event of the button. In short, you have to tell to the button what to do when it is clicked. You have two options to add an event handler for the click event:
Code: add btnShowCausali.Click += btnShowCausali_Click; after the InitializeComponent function call (in the constructor) or in the Load event of the form. Then add the function btnShowCausali_Click:
private void btnShowCausali_Click(object sender, EventArgs e)
{
DataGridView Dati = new DataGridView();
Dati.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
Dati.Location = new System.Drawing.Point(120, 40);
Dati.Name = "Dati";
Dati.RowTemplate.Height = 24;
Dati.Size = new System.Drawing.Size(979, 458);
Dati.TabIndex = 1;
Dati.Visible = true;
Dati.Columns.Add("id", "ID");
Dati.Columns.Add("causaliname", "Nome Causale");
Dati.Columns.Add("Identificationcode", "Codice Identificativo");
Dati.Columns.Add("expired", "Data di Scadenza");
grpDatore.Controls.Add(Dati); // DataGridView added to grpDatore, not form. Make shure grpDatore is visible.
}
Designer: double click on the button. Visual Studio will do the magic for you.
I am new to C# and I am using windows forms.
I have flowLayoutPanel and I programmatically add some buttons to it .
What I am trying to do is: I want to save the first button located in the flowLayoutPanel into ButtonToSave object.
flowLayoutPanel1.FlowDirection= FlowDirection.LeftToRight
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button ButtonToSave = new Button();
ButtonToSave = "First button in flowLayoutPanel1"
}
Anyone knows how to save the first button located in flowLayoutPanel1 into ButtonToSave when StoreTheFirstButton event is raised?
Thank you
Try code below. You may want to look at my response at following posting (How to (create and) add components to a Table Layout?) :
private void AddButtons_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn.Click += new EventHandler(StoreTheFirstButton_Click);
flowLayoutPanel1.Controls.Add(btn);
}
private void StoreTheFirstButton_Click(object sender, EventArgs e)
{
Button button = sender as Button;
Button ButtonToSave = button;
//ButtonToSave = "First button in flowLayoutPanel1";
}
I am a beginner in C#, so please bear with me. I have a form where I have a tab control, this tab control has 3 tabs. I am trying to show a form with each tab page. I am able to show a form on the first tab, but for some reason, the other two tabs do not load the forms. This is the code that I have. Am I doing something wrong? Do you have any suggestions?
private void MainTab_Load(object sender, EventArgs e)
{
HomeScreen fHome = new HomeScreen();
fHome.TopLevel = false;
fHome.Visible = true;
fHome.FormBorderStyle = FormBorderStyle.None;
fHome.Dock = DockStyle.Fill;
MainOptions.TabPages[0].Controls.Add(fHome);
}
private void CustomerTab_Click(object sender, EventArgs e)
{
CustomerScreen fCustomer = new CustomerScreen();
fCustomer.TopLevel = false;
fCustomer.Visible = true;
fCustomer.FormBorderStyle = FormBorderStyle.None;
fCustomer.Dock = DockStyle.Fill;
MainOptions.TabPages[1].Controls.Add(fCustomer);
}
edit:
More:
Also, in the tabcontrol- InitializeComponent I have the following
// HomeTab
//
this.HomeTab.Location = new System.Drawing.Point(4, 22);
this.HomeTab.Name = "HomeTab";
this.HomeTab.Size = new System.Drawing.Size(677, 452);
this.HomeTab.TabIndex = 0;
this.HomeTab.Text = "Home";
this.HomeTab.UseVisualStyleBackColor = true;
this.HomeTab.Click += new System.EventHandler(this.MainTab_Load);
//
// CustomerTab
//
this.CustomerTab.Location = new System.Drawing.Point(4, 22);
this.CustomerTab.Name = "CustomerTab";
this.CustomerTab.Padding = new System.Windows.Forms.Padding(3);
this.CustomerTab.Size = new System.Drawing.Size(677, 452);
this.CustomerTab.TabIndex = 1;
this.CustomerTab.Text = "Customer";
this.CustomerTab.UseVisualStyleBackColor = true;
this.CustomerTab.Click += new System.EventHandler(this.CustomerTab_Click);
I think you're just missing the Show() method...
And a suggestion: I don't know what kind of Pattern you're using, but as a beginner, you should declare your forms outside those methods, as a variable within the class scope, so you can access then later...
private HomeScreen fHome = new HomeScreen();
private CustomerScreen fCustomer = new CustomerScreen();
private void MainTab_Load(object sender, EventArgs e)
{
fHome.TopLevel = false;
fHome.Visible = true;
fHome.FormBorderStyle = FormBorderStyle.None;
fHome.Dock = DockStyle.Fill;
MainOptions.TabPages[0].Controls.Add(fHome);
fHome.Show(); // add this
}
private void CustomerTab_Click(object sender, EventArgs e)
{
fCustomer.TopLevel = false;
fCustomer.Visible = true;
fCustomer.FormBorderStyle = FormBorderStyle.None;
fCustomer.Dock = DockStyle.Fill;
MainOptions.TabPages[1].Controls.Add(fCustomer);
fCustomer.Show(); // add this
}
There's a lot to improve here, but that's a start.
I am trying to make a button that when clicked pops up a new form with a yes and no button and then make an if statement based on what button is pressed. Here is my current code:
private YesNoMessageBoxResized newBoxResized;
private string buttonClickResult;
public void YesNoNewMessageBox(string title, string message,string buttonYes, string buttonNo)
{
YesNoMessageBoxResized msgResized = new YesNoMessageBoxResized(title, message, buttonYes, buttonNo);
msgResized.StartPosition = FormStartPosition.CenterScreen;
msgResized.TopMost = true;
Button yesButtonResize = new Button();
Button noButtonResize = new Button();
//yes button
yesButtonResize.Text = buttonYes;
yesButtonResize.Size = new Size(150, 80);
yesButtonResize.Font = new Font("Arial", 26);
yesButtonResize.Location = new Point(100, 150);
//no button
noButtonResize.Text = buttonNo;
noButtonResize.Size = new Size(150, 80);
noButtonResize.Font = new Font("Arial", 26);
noButtonResize.Location = new Point(300, 150);
//make a copy of the current form
newBoxResized = msgResized;
//eventhandlers
yesButtonResize.Click += YesButtonResizeClicked;
noButtonResize.Click += noButtonResizeClicked;
newBoxResized.Controls.Add(yesButtonResize);
newBoxResized.Controls.Add(noButtonResize);
msgResized.Show();
}
private void YesButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "true";
this.newBoxResized.Close();
}
private void noButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "false";
this.newBoxResized.Close();
}
private void buttonRestoreDefaults_Click(object sender, EventArgs e)
{
YesNoNewMessageBox("Restore Defaults?", "Restore Defaults?", "Yes", "No");
if (this.buttonClickResult == "true")
this.restoreDefaults();
}
My problem is that after hitting yes and closing the form that pops up, buttonClickResult is not seen as true and therefore the restore default function I am calling is not called. Only when clicking on the "RestoreDefaults" button again is the function called. So, it seems that the onclick event for buttonRestoreDefaults_Click isn't reconizing the onclick for the yes or no buttons in the form that popups until clicking on it again. Is there a way around this or some sort of implementation to fix this? Thank you.
Also, here is the code for the class. I was thinking about using delegates and event handlers, but I am not sure if I actually need that since what I have works, but just doesn't update the variable on closing correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class YesNoMessageBoxResized : Form
{
private Label labelMessage;
//no default button specified
public YesNoMessageBoxResized(string title, string message, string buttonYes, string buttonNo)
{
InitializeComponent();
this.Text = title;
this.labelMessage.Text = message;
this.Deactivate += MyDeactivateHandler;
}
public YesNoMessageBoxResized()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.labelMessage = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// labelMessage
//
this.labelMessage.AutoSize = true;
this.labelMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelMessage.Location = new System.Drawing.Point(12, 31);
this.labelMessage.Name = "labelMessage";
this.labelMessage.Size = new System.Drawing.Size(165, 29);
this.labelMessage.TabIndex = 3;
this.labelMessage.Text = "labelMessage";
//
// YesNoMessageBoxResized
//
this.ClientSize = new System.Drawing.Size(572, 268);
this.Controls.Add(this.labelMessage);
this.Name = "YesNoMessageBoxResized";
this.ResumeLayout(false);
this.PerformLayout();
}
protected void MyDeactivateHandler(object sender, EventArgs e)
{
this.Close();
}
public delegate void buttonYes_ClickResultEvent(object o, EventArgs sEA);
public event buttonYes_ClickResultEvent choiceResult;
}
public class buttonYes_ClickResultEvent : EventArgs
{
public buttonYes_ClickResultEvent(bool choice)
{
this.buttonResult = choice;
}
public bool buttonResult;
}
**I posted this on codereview, but then they told me to post it here since it deals with solving a problem.
just for a test, try using a global variable and set that to true within your yesbuttonresizedclicked event.
something like
public static bool yestest = false;
private void YesButtonResizeClicked(object o, EventArgs sEA)
{
yestest = true;
}
I sort of fixed it by adding a function that had the if statement withing the other button events. I am open to other solutions though:
private YesNoMessageBoxResized newBoxResized;
private string buttonClickResult;
public void YesNoNewMessageBox(string title, string message,string buttonYes, string buttonNo)
{
YesNoMessageBoxResized msgResized = new YesNoMessageBoxResized(title, message, buttonYes, buttonNo);
msgResized.StartPosition = FormStartPosition.CenterScreen;
msgResized.TopMost = true;
Button yesButtonResize = new Button();
Button noButtonResize = new Button();
//yes button
yesButtonResize.Text = buttonYes;
yesButtonResize.Size = new Size(150, 80);
yesButtonResize.Font = new Font("Arial", 26);
yesButtonResize.Location = new Point(100, 150);
//no button
noButtonResize.Text = buttonNo;
noButtonResize.Size = new Size(150, 80);
noButtonResize.Font = new Font("Arial", 26);
noButtonResize.Location = new Point(300, 150);
//make a copy of the current form
newBoxResized = msgResized;
//eventhandlers
yesButtonResize.Click += YesButtonResizeClicked;
noButtonResize.Click += noButtonResizeClicked;
newBoxResized.Controls.Add(yesButtonResize);
newBoxResized.Controls.Add(noButtonResize);
newBoxResized.Show();
}
private void YesButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "true";
this.DoIfStatement();
this.newBoxResized.Close();
}
private void noButtonResizeClicked(object o, EventArgs sEA)
{
this.buttonClickResult = "false";
this.DoIfStatement();
this.newBoxResized.Close();
}
private void DoIfStatement()
{
if (buttonClickResult == "true")
this.restoreDefaults();
}
private void buttonRestoreDefaults_Click(object sender, EventArgs e)
{
YesNoNewMessageBox("Restore Defaults?", "Restore Defaults?", "Yes", "No");
}
I think I need it to be more modular though. I don't always want the same if statement there. So, I don't always want to be a yes and no with regards to restore defaults. I may want it to do something else with another function call and have yes and no buttons do something else.
You can just use MessageBox to do this for you:
if (MessageBox.Show("Yes or no?", "", MessageBoxButtons.YesNo)
== DialogResult.Yes)
You could change the form to a modal dialog form. Make the Accept and Cancel buttons properties equal the appropriate button. Then change the dialog result of each button to the appropriate value. Now when you use the ShowDialog the form will return the appropriate DialogResult.