This is my form:
namespace Secretary_1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
This is my class:
namespace Secretary_1._0
{
public partial class Client
{
public static Form1 formCall = new Form1();
public static void Clients_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.clientLabel.Visible = true;
formCall.addClientButton.Visible = true;
formCall.clientListPanel.Visible = true;
formCall.clientListPanel.BringToFront();
formCall.addClientLabel.Visible = false;
formCall.clientInfoPanel.Visible = false;
}
public static void addClientButton_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.addClientLabel.Visible = true;
formCall.clientInfoPanel.Visible = true;
formCall.clientInfoPanel.BringToFront();
formCall.addClientButton.Visible = false;
formCall.clientListPanel.Visible = false;
formCall.clientAddPropertyPanel.Visible = false;
}
}
}
Edited:
My question is how do I call the Button_Click Event from the Client Class?
When the Client button is clicked in Form1 I want to call the events located in the Client Class.
Is this possible? Am I missing something? Ive searched every where but I seem not to understand.
I plan to create a lot of buttons and would like to create a class for certain buttons so that my form1.cs isnt so long.
Any help would be appreciated. Thanks in advance.
Just do this
//Set Access Modifier of that button to public or internal for same namespace
namespace Secretary_1._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void StartClient()
{
var client = new Client(this);
client.RequiredMethod(); //Call here method of client
}
}
}
Now
namespace Secretary_1._0
{
public partial class Client
{
public Form1 formCall;
//Constructor
public Client(Form1 form1)
{
formCall = form1;
formCall.someButton.Click += OnSomeButtonClick;
}
public void OnSomeButtonClick(object sender, EventArgs e)
{
//Code here to on form1 button click ...
}
public static void Clients_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.clientLabel.Visible = true;
formCall.addClientButton.Visible = true;
formCall.clientListPanel.Visible = true;
formCall.clientListPanel.BringToFront();
formCall.addClientLabel.Visible = false;
formCall.clientInfoPanel.Visible = false;
}
public static void addClientButton_Click(object sender, EventArgs e)
{
formCall.clientPanel.Visible = true;
formCall.addClientLabel.Visible = true;
formCall.clientInfoPanel.Visible = true;
formCall.clientInfoPanel.BringToFront();
formCall.addClientButton.Visible = false;
formCall.clientListPanel.Visible = false;
formCall.clientAddPropertyPanel.Visible = false;
}
}
}
Edit: Follow this post to resolve issue using event.
1-open Form1.Designer.Cs
2-search about your button generated code
3-the last line in button code must be like that
this.btn_insert.Click += new System.EventHandler(this.button1_Click);
Note : button1 = your button name
4-Replace The last line i just mentioned above and add this one instead
this.btn_insert.Click += new System.EventHandler(this.Clients_Click);
Hope This will Help You :)
i have to mention this process mean passing a method to the delegate(EventHandler) Which necessary to the event to work correctly.
I assume you are using Visual Studio. Go into the solution explorer. Click to expand Form1.cs. Find the Form1 class in the tree and expand it. Click into one of your components there to cause VS to open the code for your Form1.designer.cs. At the bottom of that page should be a list of your components. All of them are private. Change the ones you need from private to internal, and you will have access to them from other classes in your program. Be warned, though, I think you might get some undocumented behavior doing this. But it will let you get at those forms objects from another class in the program.
Related
My problem is exactly that the inheritance of the forms, when opening, closing or compiling the child form the buttons behave as if they had a life of their own, they move from place
Here I attach a link to see what happens to me, I know that putting links is a bad practice, but I need to explain my problem as best as possible
https://1drv.ms/v/s!ApYO6DZzi_l-8zp-Fvr4nFkgta5J?e=d482SE
These are the information for a minimum and reproducible example, I use DevExpress, hopefully it is not a problem
Inherit Parent Form
public partial class BaseMantenimiento1 : DevExpress.XtraEditors.XtraForm
{
public BaseMantenimiento1()
{
InitializeComponent();
}
private void Nuevo_Click(object sender, EventArgs e)
{
Guardar.Enabled = true;
Cancelar.Enabled = true;
Editar.Enabled = false;
Buscar.Enabled = false;
Nuevo.Enabled = false;
}
public void Guardar_Click(object sender, EventArgs e)
{
Nuevo.Enabled = true;
Guardar.Enabled = false;
Cancelar.Enabled = false;
Buscar.Enabled = true;
Editar.Enabled = true;
}
private void Cancelar_Click(object sender, EventArgs e)
{
Guardar.Enabled = false;
Cancelar.Enabled = false;
Nuevo.Enabled = true;
Editar.Enabled = true;
Buscar.Enabled = true;
}
private void Editar_Click(object sender, EventArgs e)
{
Nuevo.Enabled = false;
Buscar.Enabled = false;
Editar.Enabled = false;
Guardar.Enabled = true;
Cancelar.Enabled = true;
}
}
}
Inherit Child Form
public partial class Form1 : BaseMantenimiento1
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
My guess (I cannot access your link to see what the actual problem is) is that the assignment of the event handlers is only happening in the parent form's InitializeComponent, not the child form. Since it is generated by the designer you could either wire them up in the designer for the child form or in the constructor of the child form after InitializeComponent is called.
I have class in my project named Visibility.cs and I wanna use method named HospodaVisible() in Form1 but it doesnt work. It isn't showing usercontrol.
here is code of class and form1:
public static class Visibility
{
static Form1 f = new Form1();
public static void HospodaVisible()
{
f.hospoda1.Visible = true;
f.arena1.Visible = false;
f.podzemi1.Visible = false;
}
public static void ArenaVisible()
{
f.hospoda1.Visible = false;
f.arena1.Visible = true;
f.podzemi1.Visible = false;
}
public static void PodzemiVisible()
{
f.hospoda1.Visible = false;
f.arena1.Visible = false;
f.podzemi1.Visible = true;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
hospoda1.Visible = false;
arena1.Visible = false;
podzemi1.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
Visibility.HospodaVisible();
}
}
See if this works for you:
public static class Visibility
{
public static void HospodaVisible(Form1 f)
{
f.hospoda1.Visible = true;
f.arena1.Visible = false;
f.podzemi1.Visible = false;
}
}
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Visibility.HospodaVisible(this); // passes a reference to the "current" form, i.e. Form1 itself
}
}
this is a reference to the current instance of a class inside the class, in other words it's self.
In the code above you passes a reference to the active, current form to a static helper method which accepts a reference to a form (doesn't instantiates one) and makes the necessary changes to it. Basically, it says: "hey helper, do something with this form".
I am working on c# window forms and stuck since long ago to solve a situation.
The situation is :
I have a GUI Form1.cs[Design] which consists of a Button and textbox (txtmsg here).
I have created a class Testing.cs in the visual studio winform project which contains the code like this :
namespace smallTesting
{
class Testing
{
public Testing()
{
MessageBox.Show("Connection String Did not found");
Form1 frm = new Form1(); //I do this in order to have access to
//renderMessage() so that i will be able to update my output to
//textbox(txtMsg) in this function definition by calling it.
int i = 1;
for(;;)
{
if (i == 50)
{
break;
}
frm.renderMessage(i.ToString());
i++;
}
}
}
}
And Form1.cs class is:
namespace smallTesting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e) //It should work on button click.
{
btnStart.Enabled = false;
Testing tst = new Testing();//Instantiate the class
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
public void renderMessage(string str)
{
this.txtMsg.Text = str;
MessageBox.Show("str :" + txtMsg.Text); //It should update my Textbox by 1 to 50 . BUT IT DONT DO.Whereas i can see the counting in the message box popuped.
}
}
}
I was expecting the function call to renderMessage(string str) from class Testing must have updated the txtMsg but it don't do so. Why ? (whereas messagebox popuped shows that the string is updated for every call to this function) . Why the txtMsg is not updated in my GUI for each call? How to update it.
Note: Please note that this txtMsg box updation mechanism must go from testing.cs to Form1.cs (Not Form1.cs to Testing.cs)
Change your Testing class to receive the instance of Form1 that you want to update the textbox
namespace smallTesting
{
class Testing
{
public Testing(Form1 currentInstance)
{
MessageBox.Show("Connection String Did not found");
int i = 1;
while(i < 50)
{
currentInstance.renderMessage(i.ToString());
i++;
}
}
}
}
Now in the Form1 constructore change how do you initialize the Testing instance
namespace smallTesting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e) //It should work on button click.
{
btnStart.Enabled = false;
// Pass the reference of the instance of Form1 that you
// want to update. Do not let the Testing class creates its
// own instance of form1, instead use THIS ONE.
Testing tst = new Testing(this);
}
......
}
}
I have no idea about how this error was made. Here's my code and error.
I created a new form in solution explorer and wrote these codes in my main form. I'm sure that the hitbox which I used below is right.
public partial class HomePageForm : Form
{
OptionsPageForm frmOptions;
}
private void HomePageForm_MouseClick(object sender, MouseEventArgs e)
{
if (this.homePageOptionsButtonHitBox.Contains(e.Location))
{
this.Enabled = false;
frmOptions = new OptionsPageForm(this);
frmOptions.Show();
}
}
And these are codes i wrote in my "frmOptions" - which is the form i want to call.
public partial class OptionsPageForm : Form
{
OptionsPageForm frmHomePage;
public OptionsPageForm(HomePageForm frmCreator)
{
InitializeComponent();
frmHomePage =frmCreator;
}
}
The error given by visual studio is:
Cannot implicitly convert type "My Application_.MainPageForm" to "My Application_.OptionsPageForm".
And this is another form-calling I did in this application, it has the same structure as my call to frmOptionsPage, but it works perfectly.
public partial class HomePageForm : Form
{
GamePageForm frmGame;
}
private void HomePageForm_MouseClick(object sender, MouseEventArgs e)
{
if (this.homePageStartButtonHitBox.Contains(e.Location))
{
this.Hide();
frmGame = new GamePageForm(this);
frmGame.Show();
}
}
(In Gamepage form)
public partial class GamePageForm : Form
{
HomePageForm frmHomePage;
public GamePageForm(HomePageForm frmCreator)
{
InitializeComponent();
frmHomePage = frmCreator;
}
}
I now really want to get some help, please.
I think you need to move you Mouse Click event handler into the class like this
public partial class HomePageForm : Form
{
GamePageForm frmGame;
private void HomePageForm_MouseClick(object sender, MouseEventArgs e)
{
if (this.homePageStartButtonHitBox.Contains(e.Location))
{
this.Hide();
frmGame = new GamePageForm(this);
frmGame.Show();
}
}
}
Before I used MDI and it worked fine, I could show my ListForm in MainForm. Now that I don't want to use MDI, it didn't work.
Before, with Mdi:
public partial class Le_MainForm : DevExpress.XtraEditors.XtraForm
{
public Le_MainForm()
{
InitializeComponent();
this.IsMdiContainer = true;
this.Name = "MainUSER";
if (Program.IsFA) barButtonItem_OrdList.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_ListeOrdres_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Liste_Ordres f_Liste = new Liste_Ordres();
f_Liste.MdiParent = this;
f_Liste.Show();
}
private void barButtonItem_CreatOrdreAller_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
Close_AllForm();
Program.AllerRetour = "Ordre Aller";
Fiche_Ordre f_Fiche = new Fiche_Ordre();
f_Fiche.MdiParent = this;
f_Fiche.Show();
}
Now, after I eliminated the Mdi //this.IsMdiContainer = true;
and all Forms inherited from MainForm:
public partial class Liste_Ordres : Le_MainForm
{
.....
I cannot show my ListeForm in MainFrom
public partial class Le_MainForm : DevExpress.XtraEditors.XtraForm
{
public Le_MainForm()
{
InitializeComponent();
//this.IsMdiContainer = true;
this.Name = "MainUSER";
if (Program.IsFA) barButtonItem_OrdList.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
Liste_Ordres f_Liste = new Liste_Ordres();
// f_Liste.MdiParent = this;
f_Liste.Show();
}
Someone has any idea ?
If you want your MainForm to be something like a master page, you can use only the MainForm and design all other masks not as Forms but as Controls you put on that MainForm.
Okay, I think I figured a way to open an inheriting form on Initialization.
First of all, in my MainForm I created an integer outside of any functions:
private int a = 1;
Then for my mainform I created a protected virtual on_Load event:
protected virtual void Le_MainForm_Load(object sender, EventArgs e)
{
if (a == 1)
{
Liste_Ordres frm = new Liste_Ordres();
frm.Show();
a = 0;
}
}
Next in my inheriting form, I overrided the on_Load event:
protected override void Form1_Load(object sender, EventArgs e)
{
}
That at least got both forms open without using Mdi (though in a rather round about way), but now another problem remains:
When both forms open, MainForm opens in front of inheriting (no matter if you try using functions BringToFront() and SendToBack()).
Perhaps when I figure that problem out (If I do), then I will edit this answer, but for now this is the end.
Hope this works!