C# PerformClick method in invisible form - c#

I am stuck on using a PerformClick method. I have a main form which is called mymainform, and I have some subforms. When loading main form, I am creating subforms and hide them with Form Visibility and access some elements on subforms.
My problem is clicking a button on subform1 from MainForm. I have written the below code and it didn't work. Normally DayModeButton changes the boolen; isDay = !isDay
After clicking the button1 on mainform it doesn't change the isDay boolen.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
}
If I write this code it works, but I don't want to show and hide the form because it is not a good view for users.
private void button1_Click(object sender, EventArgs e)
{
mysubform1.Visible=true;
mysubform1.DayModeButton.PerformClick();
button1.Text = mysubform1.isDay.ToString();
mysubform1.Visible=false;
}
Can anyone help me in performing a Click event in invisible forms?
Thanks

Make sure your DayModeButton is Public and not Private.

Related

Dynamic Button Click on Tab Control - Tab Pages C#

I have an application that needs to click on button located on tab pages. So for example:
Tabcontrol has Tabpage1, Tabpage2, .... TabpageN
and each tabpage has a buttonX that performs a taskX.
Now supposing, I'm on TabPage1 and I want to Click ButtonX on Tabpage3 without changing tabindex/selectedindex. How can I do that?
Generally one would use PerformClick but I've noticed it failing on TabControl. So if not dependent on EventArgs, create a sub method for the click event e.g.
private void button1_Click(object sender, EventArgs e)
{
Button1PerformClick();
}
private void Button1PerformClick()
{
MessageBox.Show("Button1");
}
Than calling
private void button2_Click(object sender, EventArgs e)
{
Button1PerformClick();
}
Doesn't matter in regards to which tab the button is on in this case.

Close Form when not clicking on it

I want to know if there is any event when you click on the rest of the screen and not the Windows Form the Form closes. Is it because of the ShowDialog()?
My Main Form is just with a notifyIcon and when I click it I call Form1.ShowDialog();
Main.cs:
private void ShowForm1(object sender, EventArgs e)
{
form1.Left = Cursor.Position.X;
form1.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form1.Height;
form1.ShowDialog();
}
Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Test";
}
You need to run the dialog box non-modally, not modally. Think about it, when you run it modally, the dialog box takes over the UI and plays games with mouse-clicks elsewhere, preventing you from running. You don't want it to be modal anyway.
I created a simple Windows form with a button that includes this handler to open a small AutoCloseDialog form I created (and populated with a few controls):
private void button1_Click(object sender, EventArgs e)
{
var dlg = new AutoCloseDialog();
dlg.Show(this); //NOTE: Show(), not ShowDialog()
}
Then, in the AutoCloseDialog form, I wired up the Deactivate event. I did it in the designer (where this code is generated):
this.Deactivate += new System.EventHandler(this.AutoCloseDialog_Deactivate);
Finally, here is the handler for the Deactivate event.
private void AutoCloseDialog_Deactivate(object sender, EventArgs e)
{
this.Close();
}
I think this does what you are asking.
Yes, you can create a similar function like this, which closes the Form if the Form lost focus (in Form1.cs)
public void Form_LostFocus(object sender, EventArgs e)
{
Close();
}
and then you add the LoseFocus EventHandler (in Form1.Designer.cs):
this.LostFocus += new EventHandler(Form_LostFocus);

Override Hide of main form with button in second form

I have a stupid and i think dumb question.
i am building an windows application to go to an login page for an website but it has 2 forms 1 to go to the login and redirect to the stock exachange acount of the user that works fine but when the user clicks the stock exchage rate bunnton the main forn wil hide.
Now for the seccond form there i have a button that need to override that hide but it does'nt
code for the button on de Koers Form to go back to the Belegen form
have tried if else statements but they dont work.
private void Belegen_Click(object sender, EventArgs e)
{
Belegen.Show();
this.Hide();
}
The button om the Belegen form to open stock reates Koers form
private void Koersen_Click(object sender, EventArgs e)
{
Koers Koers = new Koers();
Koers.Show();
this.Hide();
}
Can some one help me out.
basicly what i want is when i click the Koers button Bellegen need to hide and when ik click Belegen in the Koers form Koers needs to hide and belegen needs to be unhidden but Koers need to be opend als well and both forms need to be usable not just one.
I don't really understandy your description, but looking at the code, you should use instances of the forms, and not static calls on the classes themselves.
public class Test {
private Belegen belegen; // do not forget to initialize
private Koers koers; // do not forget to initialize
private void Belegen_Click(object sender, EventArgs e)
{
this.belegen.Show();
this.Hide();
}
private void Koersen_Click(object sender, EventArgs e)
{
koers.Show();
this.Hide();
}
}
You might have split those two two forms in your example. Then you just need to have a reference to Koersen in the Belegen form, and vice versa.

Designing a dynamic panel

I am new to C# and I want to design a GUI for a image processing application in c#. I have a very basic rudimentary layout designed as shown below
Here, the image plane is fixed and it will show a live stream video. I have designed all the buttons frame and the side panel. But I do not know how to dynamically change the side panel for each button I click. For example, If I click button1_1, I want some things in the side panel and for button1_2, some other things in it. How do I go about doing it.
EDIT:
Thanks for the answers. I see tab controls is an option. But I want a new panel evertime a click a button. which can further open forms. Is it possible?
OK, let's see. It's easy to do with "TabControl" or array of "Panel"s.
1.Do it with TabControl.
You can design GUI in TabControl in multiple subTabs(if you don't know how please ask.). Then you change it in button click event, to make subTab you wanna show(which means make it visiable and not visiable for other subTabs.)
2.Do it with array of panel.
You can use panel[] panels. In button click event, you hide other panels and show the one you want.
Hope answer helps you!
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 0;
tabControl1.TabPages[0].Text = "First";
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 1;
}
private void button3_Click(object sender, EventArgs e)
{
tabControl1.SelectedIndex = 2;
}
you may want to add split container in your form.
Create UserControl for each buttons.
Code for the button click event
//Button1Click Event
private void button1_Click(object sender, EventArgs e)
{
UserControl1 m_UserControl = new UserControl1();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_UserControl);
}
//Button2Click Event
private void button2_Click(object sender, EventArgs e)
{
UserControl2 m_Usercontrol2 = new UserControl2();
splitContainer1.Panel2.Controls.Clear();
splitContainer1.Panel2.Controls.Add(m_Usercontrol2);
}
you can do this if you want to change what usercontrol display in a panel at run time.
Correct me if i misunderstood your question.
In WinForms, you could use a tab control and just change the selected tabs index when a button is pressed. More specifically, when its click event is fired. Here is a good tutorial on using the TabControl and here is a tutorial on wiring up click events.
EDIT:
This is a better tutorial.
Since you can't hide the tabs of a tabcontrol without using WPF, you may need to use something else, if you don't like the way they look. A good workaround if you only have a couple of buttons and thus views, would be to use panels. When button one is clicked show panel one and hide panel two, etc. Here would be the code:
private void button1_Click(object sender, EventArgs e)
{
pane2.visible = false;
pane1.visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
pane1.visible = false;
pane2.visible = true;
}
Hope this helps you!

How to set focus to an object(textbox) in main form from another form (C#)

I tried everything I know. The trouble must be that my textbox is in a groupbox. I have a Mainform from which I move to another form. When I return to Mainform, I want a particular object to be focused. How is this done?
Here's my code in my Mainform.
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.ShowDialog();
}
now this is how i return back to my Mainform from Form1.
private void button3_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
I have textBox1 in Mainform. How to set focus to textBox1 when quitting Form1 and entering Mainform. I tried textBox1.Focus(); and this.ActiveControl = this.textBox1; under Mainform Load, Show, Activated and Enter events. Still didn't work. I tried creating a public method and call it under the exit button of Form1. Like this.
In Mainform,
public void textBox1Focus()
{
textBox1.Focus();
}
And then in Form1,
private void button3_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
Mainform frm = new Mainform();
frm.textBox1Focus();
}
Which still didn't work. My textBox1 is in a groupbox. Could that be the reason?
Thanks.
I don't understand how the code that you've shown even compiles. You're calling textBox1Focus() from inside a method that's defined in the Form1 class, which as best I can understand, doesn't include a definition for textBox1Focus. That method is only defined in the Mainform class.
And no, the text box being placed in a group box is not preventing it from getting the focus. There's something else wrong with your code. It's hard to tell; I feel like I'm looking at a sunset through Venetian blinds, rather than through a large picture window.
Anyway, I suspect there's a simpler solution. Just set the focus to the textbox control at the end of the button1_Click method. The ShowDialog method is a blocking call, which means execution won't continue until after the user closes the second form. When that happens, execution will continue with the next line of code, which will set the focus to the textbox control.
Try changing your code to the following:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.ShowDialog();
this.textBox1.Select();
}

Categories

Resources