I have a form with 2 tabs on it. I can chose the tab viewed after initialization and I need some initial code every time after the tab2 is initialized:
public partial class SetupComponent : Form
{
public SetupComponent(bool tab2)
{
InitializeComponent();
if (tab2)
{
this.tabControl1.SelectedTab = tabPage2;
}
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox2.SelectionStart = textBox2.Text.Length;
textBox2.Focus();
}
}
if I call this class with tab2=false and then click onto tab2, tabControl1_SelectedIndexChanged is called.
But if I select the tab2=true during SetupComponent, I find no possibility to do that code. All the TabControl1_Events I found are too early and I don`t find a matching TabPage2_Event.
How can I manage it?
I managed this problem using the Paint_Event:
bool activated = false;
private void tabPage2_Paint(object sender, PaintEventArgs e)
{
if (!activated)
{
tabControl1_SelectedIndexChanged(null, null);
activated = true;
}
}
I use the variable because the Paint_Event is called many times.
Related
I am trying to create a button to make a windows floating on the desktop using "topmost" but I can't assign a Boolean to the button, because it is a
method group
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
if(this.TopMost)
{
this.TopMost = true;
Button1_Click = true;
}
else
{
this.TopMost = false;
Button1_Click = false;
}
}
}
The answer is actually pretty short:
private void Button1_Click(object sender, EventArgs e)
{
TopMost = !TopMost;
}
You don't need to include this since you are already in the scope of your form and your if else logic can be shortened to e = !e.
Button1_Click
Is a method, you can't assign a value to it.
I'd like the activated event to only run once. I've tried using an If condition but the Reload variable doesn't set to false and thus it keeps looping endlessly. Is there a way around this?
Form1.cs code:
private void Form1_Activated(object sender, EventArgs e)
{
if (Class1.Reload == true) {
Class1.Reload = false;
}
}
Class1.cs code:
public class Class1 {
public static void Refresh() { Reload = true; }
public static bool Reload { get; set; }
Just unsubscribe from the event the first time it is triggered.
private void Form1_Activated(object sender, EventArgs e)
{
this.Activated -= Form1_Activated;
// Do other stuff here.
}
While CathalMF's solution is valid, I'll post the solution I implemented, whose aim was to refresh a DatagridView when I come back to the main form.
private void Form1_Activated(object sender, EventArgs e) {
if (Class1.Reload == true) {
Activated -= Form1_Activated;
Class1.Reload = false;
//Here I implement the code to refresh a DatagridView
Activated += Form1_Activated;
}
}
Class1.cs stays the same.
So, huh, I'm kinda new to programming in general and I have a questions that maybe you could help me out. I don't know if I did this right but hope you can give me a light here.
public partial class Form1 : Form
{
private int tick;
private int conta_jogadas;
private string[,] grelha = new string[3,3];
public Form1()
{
InitializeComponent();
}
private void pos00_button_Click(object sender, EventArgs e)
{
if (tick == 0)
{
pos00_button.Text = "X";
tick = 1;
}
else if (tick == 1)
{
pos00_button.Text = "O";
tick = 0;
}
grelha[0, 0] = pos00_button.Text;
conta_jogadas++;
}
And to verify it I have:
private void Form1_Load(object sender, EventArgs e)
{
if (conta_jogadas == 1)
{
MessageBox.Show("Teste");
}
}
For the moment I just want my button to add 1 to my variable. If i click it 3 times, my conta_jogadas will be = 3. I think I was clear here, don't know what else I can add to help. Well, thanks in advance guys.
This function
private void Form1_Load(object sender, EventArgs e)
{
if (conta_jogadas == 1)
{
MessageBox.Show("Teste");
}
}
Will execute exactly once, when your form loads. At that time the variable conta_jogadas will have the value 0 (The default for an int) so you will not ever see your message box.
You could change this to a normal function that you call whenever you update conta_jogadas if you want to see the message box when the value becomes 1
For the moment I just want my button to add 1 to my variable. If i click it 3 times, my conta_jogadas will be = 3.
Try this:
public partial class Form1 : Form
{
private int tick;
private int conta_jogadas;
private string[,] grelha = new string[3,3];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (conta_jogadas == 1)
{
MessageBox.Show("Teste");
}
}
private void pos00_button_Click(object sender, EventArgs e)
{
conta_jogadas++;
grelha[0, 0] = pos00_button.Text;
}
}
Every time button: pos00 gets clicked, variable conta_jogadas gets 1 added to the current number.
Note: couldn't compile since, I don't have Visual Studio installed here...
I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.
I have "formA" and 2 buttons on it (button1 and button2). What I want to do is:
When I click on button1 to call "formB" display text written in label1.
When I click button2 to call the same form ("formB") hide label1 and display label2.
The problem is that I don't know how to check what button is clicked on "formA".
Edit: Thanks very much folks for the quick answer. Problem is solved!
This is where events come in handy:
public class FormA
{
private void button1_Click(object sender, EventArgs e)
{
formB.Button1WasClicked();
}
private void button2_Click(object sender, EventArgs e)
{
formB.Button2WasClicked();
}
}
public class FormB
{
public void Button1WasClicked()
{
label2.Visible = false;
label1.Visible = true;
label1.Text = "Button 1 was clicked!";
}
public void Button2WasClicked()
{
label1.Visible = false;
label2.Visible = true;
label2.Text = "Button 2 was clicked!";
}
}
button1 and button2 have their own separate Click event handlers. This way we can differentiate the two when they are clicked.
If you have the same event handler for both buttons (as mentioned in one of the comments), you can identify them with the sender parameter using:
Object.ReferenceEquals(sender, button1);
or
Object.ReferenceEquals(sender, button2);
Then your code would look like this:
private void button_Click(object sender, EventArgs e)
{
if (Object.ReferenceEquals(sender, button1))
{
formB.Button1WasClicked();
}
else
{
formB.Button2WasClicked();
}
}
FormB can't find out, the buttons are a private implementation details of FormA. They might not even be a button, surely you are going to add a menu or a toolbar to FormA some day.
The workaround becomes much simpler if you stop thinking of "calling a form". You never call a form, you create an instance of it. And then you make it visible by calling its Show() method. Lots of things you can do in between those two steps.
Add a public method to FormB. For lack of a better name:
public void MakeLabel2Visible() {
this.label1.Visible = false;
this.label2.Visible = true;
}
Now it becomes simple. Implement button2's Click event handler like this:
private void button2_Click(object sender, EventArgs e) {
var frm = new FormB();
frm.MakeLabel2Visible();
frm.Show();
}
Adding another constructor to a form that lets you initialize it differently is another very common approach. These are just classes, standard programming techniques are appropriate.
Because you are using winforms you can do all this very easily due to the fact that you have a stateful environment.
Assuming a very basic set up with:
event handlers in the code behind of form a
a reference to an instance of form b in form a (or the button click creating such an instance)
a method to use in form b to pass it data
Your code will be something like this:
public partial class FormA : Form
{
private FormB formB;
public FormA()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (formB == null || formB.IsDisposed)
{
formB = new FormB();
}
formB.UpdateLabel("Button A");
formB.Show();
}
private void button2_Click(object sender, EventArgs e)
{
if (formB == null || formB.IsDisposed)
{
formB = new FormB();
}
formB.UpdateLabel("Button B");
formB.Show();
}
}
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
}
public void UpdateLabel(string message)
{
label1.Text = message;
}
}
Of course, there are lots of improvements to this - using events and alerts more intelligently and refactoring to remove duplication, but this is a basic example of the sort of things you can do.