Second form is not showing properly - c#

My main form is a mdi container with a menu strip. When I choose Options-Maintenance I want another mdi to appear. This kind of works. Instead of another mdi container along with the design, a regular smaller form appears and not sure why.
public partial class mdiMain : Form
{
static string sTo = ConfigurationManager.ConnectionStrings["connectionTo"].ToString();
public myDataAccess3 data;
public mdiMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
data = new myDataAccess3(sTo);
frmLogOn frmLogOn = new frmLogOn(data);
if (frmLogOn.ShowDialog().Equals(DialogResult.Cancel))
{
frmLogOn.Close();
frmLogOn = null;
Application.Exit();
return;
}
frmLogOn.Close();
frmLogOn = null;
this.Focus();
}
catch (Exception e1)
{
MessageBox.Show("There was an error " + e1);
}
}
private void maintenanceToolStripMenuItem_Click(object sender, EventArgs e)
{
mdiMaintenance maintenance = new mdiMaintenance(this,data);
maintenance.Enabled = true;
maintenance.Show();
}
}
public partial class mdiMaintenance : Form
{
private myDataAccess3 data;
private mdiMain mdiMain;
public mdiMaintenance()
{
InitializeComponent();
}
public mdiMaintenance(mdiMain mdiMain, myDataAccess3 data)
{
// TODO: Complete member initialization
this.mdiMain = mdiMain;
this.data = data;
}
private void mdiMaintenance_Load(object sender, EventArgs e)
{
}
Thanks for the help

If the form is intended to be an MDI Child then you need to set the MdiParent property:
private void maintenanceToolStripMenuItem_Click(object sender, EventArgs e)
{
mdiMaintenance maintenance = new mdiMaintenance(this,data);
maintenance.Enabled = true;
maintenance.MdiParent = this;
maintenance.Show();
}

Related

Manipulating similar objects in a form using another form

I am new to C# and I want to utilize the forms with one another.
I have 2 forms. (1)MMCMLibrary_home and (2)MMCMLibrary_reserve.
In this project, I'm in the stage of changing the label background colors in Form 1 but can't seem to utilize Form 2 to process it.
These are my necessary codes so far:
FORM 1
namespace MMCM_Library
{
public partial class MMCMLibrary_home : Form
{
public static MMCMLibrary_home instance;
//DCR1 Labels
public Label lbl1_1;
public Label lbl1_2;
public Label lbl1_3;
public Label lbl1_4;
public MMCMLibrary_home()
{
InitializeComponent();
instance = this;
lbl1_1 = lblDCR1_9;
lbl1_2 = lblDCR2_11;
lbl1_3 = lblDCR1_1;
lbl1_4 = lblDCR1_3;
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve();
reserveDCR1.Show();
}
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve();
reserveDCR2.Show();
}
public void btnDCR3_Click(object sender, EventArgs e)
{
var reserveDCR3 = new MMCMLibrary_reserve();
reserveDCR3.Show();
}
public void btnDCR4_Click(object sender, EventArgs e)
{
var reserveDCR4 = new MMCMLibrary_reserve();
reserveDCR4.Show();
}
}
}
FORM 2
when I click any reserve now button in form 1 it will open form 2. However, if I pick a radio button, the background change will always be applied to Discussion Room 1 even I reserved for discussion room 2
namespace MMCM_Library
{
public partial class MMCMLibrary_reserve : Form
{
public static MMCMLibrary_reserve instance;
public MMCMLibrary_reserve()
{
InitializeComponent();
instance = this;
}
private void MMCMLibrary_reserve_Load(object sender, EventArgs e)
{
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
}
private void btnDCR1_Click(object sender, EventArgs e)
{
}
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
}
}
}
Can you help me to device an efficient way to solve this. Can you also suggest a database method suitable for my beginner project.
You've said that:
the background change will always be applied to Discussion Room 1
Well, yes. It seems you don't pass specific object into Form2. There's strightforward:
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
So you'll be always changing backcolor of lbl1_1 of your Form1. What needs to be done is to indicate which room has been selected. You can assign room after you click the button by passing the int parameter:
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve(1);
reserveDCR1.Show();
}
or:
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve(2);
reserveDCR2.Show();
}
Then, in Form2, at the very top, add something like:
int room; to be able to assign the room number in Form2:
public MMCMLibrary_reserve(int roomNumber)
{
InitializeComponent();
room = roomNumber;
instance = this;
}
And then you could just select room you clicked, by:
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
if(room == 1)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
else if(room == 2)
{
MMCMLibrary_home.instance.lbl1_2.BackColor = System.Drawing.Color.Red;
}
else if(room == 3)
{
MMCMLibrary_home.instance.lbl1_3.BackColor = System.Drawing.Color.Red;
}
else if(room == 4)
{
MMCMLibrary_home.instance.lbl1_4.BackColor = System.Drawing.Color.Red;
}
}
else if(radioButton2.Checked)
{
//etc.
}
else if(radioButton3.Checked)
{
//etc.
}
else if(radioButton4.Checked)
{
//etc.
}
}
I think that was the problem. Try it and let us know.

How in main form Cancel backgroundWorker other form

I need to cancel the BakgroundWorker from the main form that was launched in another form. I am trying to solve this problem with the help of delegates. However, I cannot undo the BakgroundWorker. Help solve this problem. If there are other solutions, please write.
I give for example the code of the main form
public partial class MainForm : Form
{
public MainForm(string FIO)
{
//some code
}
public event EventHandler<EventArgs> Canceled;
private void Button5_Click(object sender, EventArgs e)
{
if (Canceled != null)
Canceled(sender, e);
}
}
Code of the form where it was launched backgroundWorker
public partial class CarriageForm : Form
{
public CarriageForm(ToolStripProgressBar toolStripProgressBar1, ToolStripLabel toolStripLabel1)
{
//some code
}
private void CarriageForm_Load(object sender, EventArgs e)
{
progBar.Visible = false;
if (!backgroundWorker1.IsBusy)
{
progBar.Visible = true;
progBar.Maximum = GetTotalRecords();
string GetCarriage = "Select dc.ID, dc.CarNumber [Номер вагона],dc.AXIS [Осность],do.ID [OwnerID], do.Name [Собственник],do.FullName [Собственник полное наименование] From d__Carriage dc Left Join d__Owner do on do.ID = dc.Owner_ID";
MainForm mainForm = new MainForm(null);
mainForm.Canceled += new EventHandler<EventArgs>(Button2_Click);
backgroundWorker1.RunWorkerAsync(GetCarriage);
}
//BackgroundWorker1_DoWork...
//BackgroundWorker1_ProgressChanged...
//BackgroundWorker1_RunWorkerCompleted..
public void Button2_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Stop the Background Thread execution
Application.UseWaitCursor = false;
System.Windows.Forms.Cursor.Current = Cursors.Default;
backgroundWorker1.CancelAsync();
progBar.Value = 0;
progBar.Visible = false;
TlStpLabel.Text = "Пользователь умышленно отменил";
}
}
}
For clarity

avoid initializing list with data

So, I have two forms. The form1 has a button to open the form2. In the form2 I have a list of elements that I fill with elements I create in the form2. My problem is, when I close my form2 and reopen it, my list is empty. I know that is because Im initializing my list again (ListaComida = new List<Comida>();), so I get my data erased but I dont see how to solve this.
My code
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addDia_Click(object sender, EventArgs e)
{
string dia = (DateTime.Today.ToString("dd/MM/yyyy"));
TabPage myTabPage = new TabPage(dia);
tabControl1.TabPages.Add(myTabPage);
}
private void AddComida_Click(object sender, EventArgs e)
{
FormAddComida addComida = new FormAddComida();
DialogResult resultaod = addComida.ShowDialog();
}
}
Form2
public partial class FormAddComida : Form
{
public List<Comida> ListaComida;
public FormAddComida()
{
InitializeComponent();
ListaComida = new List<Comida>();
}
private void addComidaAdicionar_Click(object sender, EventArgs e)
{
Comida comidaAdicionada = new Comida(tbNome.Text,
Convert.ToInt32(tbCalorias.Text),
Convert.ToInt32(tbHidratos.Text),
Convert.ToInt32(tbProteinas.Text),
Convert.ToInt32(tbGorduras.Text)
);
ListaComida.Add(comidaAdicionada);
RefreshListaComida();
}
private void RefreshListaComida()
{
lbListaComida.Items.Clear();
lbListaComida.Items.AddRange(ListaComida.ToArray());
}
private void AddComidaCancelar_Click(object sender, EventArgs e)
{
this.Close();
}
}
You can use MemoryCache, even if you close your form your List will stay in Memory and you can retrieve by the Key. But if you need to save this data permanently (or long time running the app )i recommend you store in a DB.
using System.Runtime.Caching;
private ObjectCache cache = MemoryCache.Default;
public class Food
{
public string Name { get; set; }
public double Price { get; set; }
}
public void AddFood()
{
FoodList.Add(new Food { Name = "Pizza", Price = 10 });
FoodList.Add(new Food { Name = "Fries", Price = 5 });
cache.Add("UserCacheFood", FoodList, DateTimeOffset.MaxValue);
}
public List<Food> ReturnListFromCache()
{
return (List<Food>)cache.Get("UserCacheFood");
}
private void button1_Click(object sender, EventArgs e)
{
AddFood();
var result = ReturnListFromCache();
}
private void button2_Click(object sender, EventArgs e)
{
var ret2 = ReturnListFromCache();
}

c# Many Parent forms one child

I have Parent_1, Parent_2, Parent_3 and etc. and have one Child form
in Parent_1, Parent_2, Parent_3 ... I have:
private string strText;
public string pubText {
get { return strText; }
set { strText = value; } }
on button:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this);
form.ShowDialog();
}
in child form I have the code:
private Parent_1 logicalParent;
public fChild(Parent_1 parent)
{
InitializeComponent();
logicalParent = parent;
this.FormClosed += new FormClosedEventHandler(child_FormClosed);
}
and
void child_FormClosed(object sender, FormClosedEventArgs e)
{
logicalParent.pubText = this.textBox1.Text;
}
This works only for 1 parent form, How Can I use this for other Parent forms???
Please Help
You can create a common interface for the three parent forms:
public interface IParentForm
{
string PubText {get; set;}
}
public class Parent_1 : Form, IParentForm
{
public string PubText
{
get { return this.pubText; }
set { this.pubText = value; }
}
}
//same for Parent_2 and 3
then in your child form declare logicalParent to be of type IParentForm, and change the constructor of the child form to be public fChild(IParentForm parent)
At the button click do the following:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this); // this is not more needed
form.ShowDialog();
pubText = form.pubText;
}

Can't change TextBox.Text from another form

I have the following C# code in my winform application:
FormPrincipale
private void butFornitore_Click(object sender, EventArgs e)
{
try
{
FormFornitore Dialog = new FormFornitore();
Dialog.ShowDialog();
}
catch(Exception excDial)
{
MessageBox.Show("DIALOG: " + excDial.Message);
}
}
public void getFornitore(string Codice, string Descrizione)
{
this.txtFornitore.Text = Descrizione;
Fornitore = Codice;
}
FormFornitore
private void gridFornitori_DoubleClick(object sender, EventArgs e)
{
try
{
var Codice = gridView2.GetFocusedDataRow()["codconto"].ToString();
var RagSoc = gridView2.GetFocusedDataRow()["dscconto1"].ToString();
FormPrincipale Form = new FormPrincipale();
Form.getFornitore(Codice, RagSoc);
this.Close();
}
catch(Exception excGrid)
{
MessageBox.Show("GRID: " + excGrid.Message);
}
}
The code works (i used breakpoints to check if code was executed) but the Text property of the TextBox doesn't change. I put Modifiers TextBox property on Public, so this is ok too. I'm using Dev Express Grid Control, but i don't think this is the problem. Thank's for help.
To pass the instance of your parent form, you could do something like this:
class FormFornitore: Form
{
protected FormPrincipale parent;
FormFornitore(FormPrincipale parent)
{
this.parent = parent;
}
private void gridFornitori_DoubleClick(object sender, EventArgs e)
{
try
{
var Codice = gridView2.GetFocusedDataRow()["codconto"].ToString();
var RagSoc = gridView2.GetFocusedDataRow()["dscconto1"].ToString();
/// REMOVE THIS FormPrincipale Form = new FormPrincipale();
parent.getFornitore(Codice, RagSoc);
this.Close();
}
catch(Exception excGrid)
{
MessageBox.Show("GRID: " + excGrid.Message);
}
}
}
Then in your "FormPricipale" use it like this
private void butFornitore_Click(object sender, EventArgs e)
{
try
{
FormFornitore Dialog = new FormFornitore(this); // Notice the argument
Dialog.ShowDialog();
}
catch(Exception excDial)
{
MessageBox.Show("DIALOG: " + excDial.Message);
}
}
public void getFornitore(string Codice, string Descrizione)
{
this.txtFornitore.Text = Descrizione;
Fornitore = Codice;
}

Categories