Enable/disable an ordinary button on Form2 from Form1 - forms

I need some help. I've been reading this site for days, and have read a lot of tips about controlling for example a button's property from another form. There's even a video on Youtube, which works to me as stand alone, but when I implenet it in my application it throws a NullReferenceException.
Let's say that I have a toolstrip menu on Form1. A click on the Kalibracio option opens the second form (also called Kalibracio - not Form2). Then, click on Proba in the menu should disable an ordinary button on the Kalibracio form which propery is set to public. The code on Form1 is as follows:
private void kalibracioToolStripMenuItem_Click(object sender, EventArgs e)
{
Kalibracio Kalibr = new Kalibracio(this);
Kalibr.Owner = this;
Kalibr.Show();
}
private void probaToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Application.OpenForms.OfType<Kalibracio>().Any())
(this.Owner as Kalibracio).button1.Enabled = false;
// the above line throws a NullReferenceExcteption if Kalibracio form is open (Kalibracio is null)
}
What am I missing?

LoL just had to declare globaly an instance of Kalibracio, open it and then access it's properties from all other methods.
I tried this approach at first, but my problem was that I was creating and instance locally, then I had to create another one in some other method because I couldn't address the former one created locally, and ofc it didn't work...

Related

C# Having panels show with push of button, toggle between them not working

So I'm trying to load forms inside of my panel with the click of a button. But the problem I'm having is toggling between them. I can click one and it shows, but clicking the other one won't load anything. What am I doing wrong? I want to be able to just click a button (i have 5 different ones) and have it load the forms inside of the panel(replacing the old one I loaded). I can't seem to find an answer that helps me. Can someone help me or show me in the right direction?
My main form has the buttons on the left side and I have my container (panel from the toolbox) to the right which I'm trying to load the forms inside.
Below I included 2 sample buttons I have. All the other ones have the same code inside them. No other codes anywhere else for this function.
I'm new to c#, this may be the reason I'm struggling with such a simple task probably.
private void btn1_Click(object sender, EventArgs e)
{
test1 test1 = new test1();
test1.TopLevel = false;
test1.Dock = DockStyle.Fill;
this.pContainer.Controls.Add(test1);
test1.Show();
}
private void btn2_Click(object sender, EventArgs e)
{
test2 test2 = new test2();
test2.TopLevel = false;
test2.Dock = DockStyle.Fill;
this.pContainer.Controls.Add(test2);
test2.Show();
}

How can i open form in class with C#?

I want to write class codes to open form. How can I make changes the codes to work?
My Class Codes:
public void formac(Form frm, Form formName)
{
formName frm = new formName();
frm.Show();
}
Button Click Codes:
openFormClass MyClass = new openFormClass();
MyClass.formac(frm,Form1);
Hello and welcome to StackOverflow!
Unfortunately your question is not very concise on what you want to achieve. Maybe have a look at How do I ask a good question?.
From what you've shown, I'm guessing that you're just want to show another instance of Form1 on a button click. Then the following should be sufficient:
private void button1_Click(object sender, System.EventArgs e)
{
var frm = new Form1();
frm.Show();
}
i want to write code to open form in class (parameter will be "Form Name")

TabControl avoid changing to another tab

I have a tab control with 2 tabs.. When the user clicks on the second tab, it does some validation and then if that validation returns false, the user gets a message indicating to go back. Now, here's my problem, it changes tabs anyways with the code below:
Although the user doesn't see the tab 2, it is showing as changed.
private void tabprincipal_SelectedIndexChanged(object sender, EventArgs e)
{
if (!saved_plan)
{
MessageBox.Show("You need to save a plan first.");
return;
}
How can I avoid this behavior? I want to display the message and the user to remain in the first tab
I think I'm looking for an event prior to the selectedindexchanged to detect that the user clicked tab2 and then don't let him move..
I actually found a way using the Deselecting method of the TabControl
private void tabprincipal_Deselecting(object sender, TabControlCancelEventArgs e)
{
if (!saved_plan)
{
MessageBox.Show("You need to save a plan first");
e.Cancel = true;
}
}

Postback parent page when using an ASP.NET ModalPopup control

I have a custom UserControl that displays a modal popup (from the Ajax Toolkit). The control allows the user to add a note to a customer record which the parent page displays in a GridView.
I'm unable to force the parent page to reload the grid after the user clicks the "Add Note" button on the modal popup and closes it. The note is added to the database correctly, but I have to manually refresh the page to get it to display instead of it automatically refreshing when I save+close the popup.
You can use a delegate to fire an event in parent page after note is added to the database.
// Declared in Custom Control.
// CustomerCreatedEventArgs is custom event args.
public delegate void EventHandler(object sender, CustomerCreatedEventArgs e);
public event EventHandler CustomerCreated;
After note is added, fire parent page event.
// Raises an event to the parent page and passing recently created object.
if (CustomerCreated != null)
{
CustomerCreatedEventArgs args = new CustomerCreatedEventArgs(objCustomerMaster.CustomerCode, objCustomerMaster.CustomerAddress1, objCustomerMaster.CustomerAddress2);
CustomerCreated(this, args);
}
In parent page, implement required event to re-fill grdiview.
protected void CustomerCreated(object sender, CustomerCreatedEventArgs e)
{
try
{
BindGridView();
}
catch (Exception ex)
{
throw ex;
}
}
In your case, you can not use any custom event args, and use EventArgs class itself.

.Net CF Prevent Overzealous, Impatient Clicking (while screen is redrawing)

.Net Compact Framework
Scenario: User is on a screen. Device can't finds a printer and asks the user if they want to try again. If they click "No", the current screen is closed and they are returned to the parent menu screen. If they click the "No" button multiple times, the first click will be used by the No button and the next click will take effect once the screen has completed redrawing. (In effect clicking a menu item which then takes the user to another screen.)
I don't see a good place to put a wait cursor...there isn't much happening when the user clicks "No" except a form closing. But the CF framework is slow to redraw the screen.
Any ideas?
you can skip pending clicks by clearing the windows message queue with
Application.DoEvents();
We use the following custom Event class to solve your problem (preventing multiple clicks and showing a wait cursor if necessary):
using System;
using System.Windows.Forms;
public sealed class Event {
bool forwarding;
public event EventHandler Action;
void Forward (object o, EventArgs a) {
if ((Action != null) && (!forwarding)) {
forwarding = true;
Cursor cursor = Cursor.Current;
try {
Cursor.Current = Cursors.WaitCursor;
Action(o, a);
} finally {
Cursor.Current = cursor;
Application.DoEvents();
forwarding = false;
}
}
}
public EventHandler Handler {
get {
return new EventHandler(Forward);
}
}
}
You can verify that it works with the following example (Console outputs click only if HandleClick has terminated):
using System;
using System.Threading;
using System.Windows.Forms;
class Program {
static void HandleClick (object o, EventArgs a) {
Console.WriteLine("Click");
Thread.Sleep(1000);
}
static void Main () {
Form f = new Form();
Button b = new Button();
//b.Click += new EventHandler(HandleClick);
Event e = new Event();
e.Action += new EventHandler(HandleClick);
b.Click += e.Handler;
f.Controls.Add(b);
Application.Run(f);
}
}
To reproduce your problem change the above code as follows (Console outputs all clicks, with a delay):
b.Click += new EventHandler(HandleClick);
//Event e = new Event();
//e.Action += new EventHandler(HandleClick);
//b.Click += e.Handler;
The Event class can be used for every control exposing EventHandler events (Button, MenuItem, ListView, ...).
Regards,
tamberg
Random thoughts:
Disable the some of the controls on the parent dialog while a modal dialog is up. I do not believe that you can disable the entire form since it is the parent of the modal dialog.
Alternatively I would suggest using a Transparent control to catch the clicks but transparency is not supported on CF.
How many controls are on the parent dialog? I have not found CF.Net that slow in updating. Is there any chance that the dialog is overloaded and could be custom drawn faster that with sub controls?
override the DialogResult property and the Dispose method of the class to handle adding/remvoing a wait cursor.