Control properties don't allow to be changed - c#

My mainForm contains two buttons(btnLoad & btnChange) and a panel
When the btnLoad is clicked, it loads the other forms(there are 5 different froms with different controlers) into the panel. Let me assume one of it named Form2 which contains a label(labelMessage)
My problem is, when I click the btnChange the following statement won't work.
f2.labelMessage.Text = "Button Change Clicked";
My codes are
// codes on mainFrom
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = new From2();
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
private void btnLoad_Click(object sender, EventArgs e)
{
panelDock.Controls.Clear();
Form f2 = new Form2();
f2.TopLevel = false;
panelDock.Controls.Add(f2);
f2.Show();
}
is this wrong?

Since Form2 is already shown you should use Application.OpenForms instead of creating a new instance of Form2
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)Application.OpenForms["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
From your comment that Form2 is in a panel you can try
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)panel1.Controls["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}

Related

c# add rows from another form

I got 2 forms, Form1 contains the datagridview and button "add", Form2 contains the textboxs and button "save",
I want to add row and form2 appears when add button is clicked, then save informations from form2 in the datagridview when save button is clicked
this is the code i'm using for both add and save buttons, but when i do, it only saves informations wrote from form1 (save button doesn't do much if not updating datagridview)
private void AddButton_Click(object sender, EventArgs e)
{
Form2 windowAdd = new Form2();
windowAdd.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);
windowAdd.ShowDialog();
var frm2 = new Form2();
frm2.AddGridViewRows(textName.Text, textDescription.Text, textLocation.Text, textAction.Text);
textName.Focus();
this.stockData.Product.AddProductRow(this.stockData.Product.NewProductRow());
productBindingSource.MoveLast();
}
private void SaveButton_Click(object sender, EventArgs e)
{
productBindingSource.EndEdit();
productTableAdapter.Update(this.stockData.Product);
this.Close();
}
Try this approach.
Form2 could accept some parameters for construction. You will have to resolve the reference to the productBindingSource and productDataAdaptor.
public partial class Form2 : Form
{
private DataRow _theRow;
private bool _isNew;
public Form2(DataRow theRow, bool isNew)
{
InitializeComponent();
_theRow = theRow;
_isNew = isNew;
}
private void Form2_Load(object sender, EventArgs e)
{
textName.Text = _theRow["Name"];
// Etc
}
private void btnSave_Click(object sender, EventArgs e)
{
// This is your add / edit record save button
// Here you would do stuff with your textbox values on form2
// including validation
if (!ValidateChildren()) return;
if (_isNew)
{
// Adding a record
productBindingSource.EndEdit();
productTableAdapter.Update();
}
else
{
// Editing a record
}
this.Close();
}
}
This changes your call in the Form1.Add Button event. I have shown below the way to utilize a using block to show a form.
private void btnAdd_Click(object sender, EventArgs e)
{
DataRow newRow = stockData.Product.NewProductRow();
using (var addForm = new Form2(newRow, true))
{
addForm.StartPosition = FormStartPosition.CenterParent;
addForm.ShowDialog(this);
// Here you could access any public method in Form2
// You could check addForm.DialogResult for the status
}
}
This isn't the best way to do this, but this direction might be a easier way to try...
Hope it helps

Opening new form over another form

I have main form which has several buttons, each button opens new form. When i click button1, form1 opens and when i click button2, form2 opens but form1 goes back to the main form. I want functionality such that each new form opens over the parent form and the most recent form on the top.
This is my code
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.Show();
}
Each form has a topmost property, just set them to true
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.Show();
form.TopMost = true;
form.Activate();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
form.TopMost = true;
form.Activate();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.Show();
form.TopMost = true;
form.Activate();
}

How can I refresh One form when the other form is closed?

I have been developing project in c#.
It has 2 form and these are connected with between each other.
I want to do that when second form is closed, first form refresh.
If I use Thread's Sleep program will be tired. I want to do this with closing events. How can I do ?(Like java's repaint)
Codes are below:
Form1
public static Form1 form;
public Form1()
{
InitializeComponent();
form = this;
}
private void button11_Click(object sender, EventArgs e)
{
Form2 yeniform = new Form2();
yeniform.Show();
}
Form2(Close Button)
private void button1_Click(object sender, EventArgs e)
{
Form1.form.Invalidate();
Form1.form.Refresh();
this.Close();
}
Bind Form_Closing event in your first form.
//Form1
private void button11_Click(object sender, EventArgs e)
{
Form2 yeniform = new Form2();
yeniform.FormClosing += new FormClosingEventHandler(this.Form2_FormClosing);
yeniform.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//Do your stuff here.
}
' button in the second form which is to be closed
' form1 is the form which is to be re loaded when form 2 is closed
private void btn_close_Click(object sender, EventArgs e)
form1.close() 'unload form 1 before closing form2
form1.show() ' form 1 reloading
unload(me) 'closing form2
end sub
This is a working sample. In parent form
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
VendorsAddForm f = new VendorsAddForm();
f.StartPosition = FormStartPosition.CenterScreen;
f.FormClosed += new FormClosedEventHandler(child_FormClosed);
f.Show();
}
void child_FormClosed(object sender, FormClosedEventArgs e)
{
this.Refresh();
var query = dbContext.AccountObjects.Where(p => p.IsVendor == true).ToList();
accountObjectsBindingSource.DataSource = new BindingList<AccountObject>(query);
}
Note: child form is VendorsAddForm
Thank to https://www.daniweb.com/posts/jump/1302760 , I learned from there.

ShowDialog issue while opening form

I have 2 forms
Form1
Form2
I have one button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.ShowDialog();
f2.Dispose();
}
but issue is while opening form it's bliking and diasparing
i have tried to use show() also but not solved the problem
If i have not used Disposed method then first time when run the form it appering and disappered but sencond time onward by clicking on button it's working fine...
In Form2_Load event i am using this two property
private void Form2_Load(object sender, EventArgs e)
{
this.RightToLeft = RightToLeft.Yes;
this.RightToLeftLayout = true;
}
Don't change the form layout while its loading. Change it before you launch. Remove the code from Form2_Load and put it in button1_Click:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.RightToLeft = RightToLeft.Yes;
f2.RightToLeftLayout = true;
f2.ShowDialog();
}
I would guess you want to show and close the form2 using the same button. And I doubt your initial problem description
"issue is while opening form it's bliking and diasparing"
I think form2 is not 'blinking' while opening, but is 'blinking' while you try to click the button again in form1
ShowDialog() will exit your execution after u called it. Mean, it will exit the execution after you click the button.
Thus, you should try Show() with conditional statement within the button click event
In form1.cs
bool flag = false;
Form2 frm2;
private void button1_Click(object sender, EventArgs e)
{
if (flag == false)
{
frm2 = new Form2();
frm2.Show();
frm2.Load += new EventHandler(frm2_Load);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
flag = true;
}
else
{
frm2.Close();
flag = false;
}
}
void frm2_Load(object sender, EventArgs e)
{
//set what ever properties you like
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
flag = false;
}
See also: A dialog disables all of the windows that your program displays
Remove this Property
this.RightToLeft = RightToLeft.Yes;
and run your form...
Try This :
private void button1_Click(object sender, EventArgs e)
{
using(Form2 f2 =new Form2())
{
f2.ShowDialog();
}
}

Change the Form Text from another Form

I have 2 Winforms (on Visual C#). On Form1 I have a button and when the user clicks it, I want the text of Form2 to change,before I open Form2 (The text that appears on the top left cornet of my Winform).
I tried these (1) (2) but they don't work.
On Form2 I have
public string formtext
{
get {return this.Text;}
set {this.Text = value;}
}
This is my button on Form1
public void kryptonButton2_Click(object sender, EventArgs e)
{
// Form2
Form2 form2 = new Form2();
form2.Text = "Η πόλη του Πειραιά";
}
Note that I click that button to change the text and then I click on another button to open Form2.
Form2 opens, but the text isn't changed.
Your Form2 instance has to be accessible from your text changing routine.
private Form2 m_form2;
public Form1() {
InitializeComponent();
m_form2 = null;
}
Now that you have the ground work laid, you will need to call your m_form2 object using Show() and NOT ShowDialog():
private void ShowForm2(string optionalText) {
if (m_form2 == null) {
m_form2 = new Form2();
m_form2.Show();
} else {
m_form2.Focus();
}
if (!String.IsNullOrEmpty(optionalText)) {
m_form2.Text = optionalText;
}
}
With this setup, your button should work for Form2 by modifying your routine to do this:
public void kryptonButton2_Click(object sender, EventArgs e) {
ShowForm2(null);
m_form2.Text = "Η πόλη του Πειραιά";
}
OR using the optionalText parameter:
public void kryptonButton2_Click(object sender, EventArgs e) {
ShowForm2("Η πόλη του Πειραιά");
}
You could also do this using delegates. This is a very powerful feature of C#. If you would like to see a code example of that, look at my answer to this question here:
https://stackoverflow.com/a/19146929/153923
UPDATE
It sounds like you may only need an updated version of Mike Cheel's answer.
Try:
public void kryptonButton2_Click(object sender, EventArgs e) {
Form2 form2 = new Form2();
form2.formtext = "Η πόλη του Πειραιά";
form2.Show(); // Mike left this part out
}
Try:
public void kryptonButton2_Click(object sender, EventArgs e)
{
// Form2
Form2 form2 = new Form2();
form2.formtext = "Η πόλη του Πειραιά";
}
There are a couple of reasons why your approach doesn't work.
public void kryptonButton2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(); //<-- this is a new instance for Form2
form2.Text = "Η πόλη του Πειραιά"; //<-- and this is not your propery
//(as pointed out by #MikeCheel)
}
If you have no instance of Form2 then you could try and get it from Application.OpenForms but that is a HACK.
var frm2 = Application.OpenForms.Cast<Form>()
.FirstOrDefault(c => c.Name == "Form2");
if(frm2 != null)
form2.formtext= "Η πόλη του Πειραιά";
When you click the other button to open the form you need to hold onto the reference to it, so that your other button click event can use it:
private Form2 child;
public void openOtherForm_Click(object sender, EventArgs e)
{
child = new Form2();
child.Show();
}
Now you can use that field to manipulate it:
public void kryptonButton2_Click(object sender, EventArgs e)
{
form2.formtext = "Η πόλη του Πειραιά";
}

Categories

Resources