Enabling the SHOP Button from form2 closing event - c#

I have a little problem that I can't enable the shop button when the form2 closes.
So, I was making a shop for my clicker game. When you press the SHOP button your given a form2 (which was supposed to be the shop, WIP) and the shop button (button2) should be disabled because you can spam the shop button when its not disabled. After closing the shop the button was supposed to be enabled again, but I can't get it to work.
Note: the Form 2 Close button for the shop is sCloseBtn.
Form 1 Code:
private void button1_Click(object sender, EventArgs e)
{
i += 1;
if (i == 1)
{
label4.Text = "Cake";
}
else
{
label4.Text = "Cakes";
}
label3.Text = Convert.ToString(i);
}
public void button2_Click(object sender, EventArgs e)
{
WindowsFormsApplications2.Form2 secondForm;
secondForm = new WindowsFormsApplications2.Form2();
if (isInGui == false) {
secondForm.Show();
button2.Enabled = false;
isInGui = true;
}
else if (isInGui == true)
{
button2.Enabled = true;
isInGui = false;
}
}
Form 2 Code:
private void sCloseBtn_Click(object sender, EventArgs e)
{
Close();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
}
All help is appreciated :)

One way to solve that would be to move the event handler "Form2_FormClosing" to Form1,
public partial class Form1 : Form
{
// ...whatever is in your form already...
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
button2.Enabled = true;
isInGui = false;
}
// ...
}
and to assign it to secondForm's event after its creation, ie.
secondForm = new WindowsFormsApplications2.Form2();
secondForm.FormClosing += Form2_FormClosing;

Related

Show button only when focus is on textbox

Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.

How to update a listbox dynamically?

I am making a search utility and using a BackgroundWorker to search. I want that as soon as the first result is found , a new window should open up with a ListBox with the first element displayed. Now, I want that as soon as subsequent results are found, the ListBox should be updated with those results.
The method thought by me was to report the progress as soon as results are found and pass "New" and "Update" as userState to the method.
Based on the userState, I can decide whether to create a new Form or update and existing one.
Here is the code :-
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.UserState.ToString() == "New")
{
Form ResultForm = new Form();
ResultForm.Name = "ResultForm" + i.ToString();
LastFormName = ResultForm.Name;
ListBox ResultListBox = new ListBox();
ResultListBox.DataSource = SearchList;
ResultListBox.Name = "ResultListBox" + i.ToString();
LastListName = ResultListBox.Name ;
ResultForm.Container.Add(ResultListBox);
ResultListBox.Show();
ResultForm.Show();
i++;
}
else
{
;
}
}
I have stored the names of the Last open Form and it's ListBox in the variables LastFormName and 'LastListName'.
I am unable to understand what to put in the else condition, so as to update the ListBox.
What I would do is expose some properties on the popup form so that you can tell if it is open and have access to the list box.
public partial class Popup : Form
{
public bool isOpen;
public ListBox PopupListBox;
public Popup()
{
InitializeComponent();
}
void Popup_FormClosing(object sender, FormClosingEventArgs e)
{
isOpen = false;
}
private void Popup_Load(object sender, EventArgs e)
{
this.FormClosing += Popup_FormClosing;
PopupListBox = popupListBox;
}
}
Then on the calling form I would subscribe to the ProcessedChanged Event and update the listbox with the data you are passing through the ProcessedChangedEventArgs. Here is the code for the calling form
public partial class Form1 : Form
{
Popup popupForm = new Popup();
BackgroundWorker backgroundWorker = new BackgroundWorker();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker.WorkerSupportsCancellation = true;
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.DoWork += backgroundWorkerDoWork;
backgroundWorker.ProgressChanged += backgroundWorkerProgressChanged;
backgroundWorker.RunWorkerCompleted += backgroundWorkerRunWorkerCompleted;
}
void backgroundWorkerRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
//What do you want to do if Cancelled?
}
else if (!(e.Error == null))
{
//What do you want to do if there is an error?
}
else
{
//What do you want to do when it is done?
}
}
void backgroundWorkerProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (!popupForm.isOpen || popupForm == null)
{
popupForm = new Popup();
popupForm.Show();
popupForm.isOpen = true;
}
else
{
popupForm.Activate();
popupForm.WindowState = FormWindowState.Normal;
}
popupForm.PopupListBox.Items.Add(e.ProgressPercentage.ToString() + "%");
}
void backgroundWorkerDoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; (i <= 10); i++)
{
if ((worker.CancellationPending == true))
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress((i * 10));
}
}
}
private void buttonStart_Click(object sender, EventArgs e)
{
if (backgroundWorker.IsBusy != true)
{
backgroundWorker.RunWorkerAsync();
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
if (backgroundWorker.WorkerSupportsCancellation == true)
{
backgroundWorker.CancelAsync();
}
}
}
You shouldn't be doing work in the ProgressChanged event handler
You won't have access to the results as it is only passed an int for progress and user state
This is on the UI thread. The entire point is to do your processing on a background thread
The name DoWork event handler is clear that this is where you should do your processing.
In answer to your question. Since you create the ListBox in your event handler it goes out of scope outside the if statement. You need to create this in a more global scope. Then to add to it ResultListBox.Items.Add("ResultListBox" + i.ToString());

how to reset the focus to last entered textbox in windows application

I have two TextBoxes and a button control in the form. When the button is clicked the name of the last entered TextBox should be displayed in a MessageBox. At the same time I need to reset the focus to last entered TextBox.
string str=string.Empty;
bool foc;
In button click I wrote the following code
if (MessageBox.Show("You want to reset or continue", "control",
MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
if (foc== true)
{
textBox1.Focus();
}
else
{
textBox2.Focus();
}
}
When I clicks on cancel button the focus should be into textbox which is entered at last
private void textBox1_Enter(object sender, EventArgs e)
{
str = textBox1.Name;
foc= textBox1.Focus();
}
private void textBox2_Enter(object sender, EventArgs e)
{
str= textBox2.Name;
foc= false;
}
Other than the above lines of code is there any other possibility to focus into the textbox, but when number of textboxes increases how i need to write the conditions.
If I am having textbox,combobox,listbox,checkbox or any other controls in the form then how to find in which control the user enterd at last and set focus to that control by using any function instead of writing in every control Enter event
You can handle Leave event of the TextBoxes to store the Last TextBox Control.
Try this:
this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
TextBox txtLast = new TextBox();
private void textBox1_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void textBox2_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show(txtLast.Text);
}
private void Cancel_Click(object sender, EventArgs e)
{
txtLast.Focus();
}
public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
void textBox2_GotFocus(object sender, EventArgs e)
{
textBox2WasLastFocused = true;
textBox1WasLastFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1WasLastFocused = true;
textBox2WasLastFocused = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1WasLastFocused)
MessageBox.Show("textbox1 was ladst focused !");
else if(textBox2WasLastFocused)
MessageBox.Show("textbox2 was ladst focused !");
}

Disable Close Button

I have a form that a user uses to select the level when a Game starts. I want to disable the close button such that a user cannot close the form (The user will click some buttons to select the level).
I have been able to stop the user from closing the form if a button is not clicked using
bool _Next = false;
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button_Click);
button2.Click += new EventHandler(button_Click);
button3.Click += new EventHandler(button_Click);
}
void button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn == button1)
{
Level(1);
}
else if (btn == button2)
{
Level(2);
}
else if (btn == button3)
{
Level(3);
}
_Next = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (_Next == true)
{
}
else
{
e.Cancel = true;
}
}
This is quite long. I want to know if there is any way i can just disable or hide the form close button
You can hide the Controls of the form with:
this.ControlBox = false;
The above will remove the maximize, minimize and close button from the right upper corner.
The above is also accessible on the Visual Studio when you select the form itself at the properties tab.
You can override the OnClosing method:
protected override void OnClosing(CancelEventArgs e)
{
if (!_Next)
{
e.Cancel = true;
}
}
You're already disabling the close button. In your FormClosing event you cancel the event:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
This code works perfectly; I just tested it to make sure.

Cell Double Click event is not running. only Cellclick event gets executed

i have both the events cellclick and celldoubleclick for a datagridview in my window forms application.
The problem is that when i double click , only the cellclick event triggers, as it cannot detemine if its single click or double click.
i searched for this and found that timers could be the solution..but how to do that ?
plz help
You probably ought to try to find out why double-click isn't getting fired. Answering your question: you'll indeed need a timer whose Interval you set to the double-click time:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = SystemInformation.DoubleClickTime;
timer1.Tick += delegate { timer1.Enabled = false; };
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if (timer1.Enabled) {
timer1.Enabled = false;
// Do double-click stuff
//...
}
else {
timer1.Enabled = true;
// Do single-click stuff
//...
}
}
}
I established a little solution for this:
add timer in your form
public Form1()
{
timer1.Interval = SystemInformation.DoubleClickTime;
}
bool double_click = false;
//
DataGridViewCellEventArgs sendedEvent = null;
private void DatagridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
double_click = true;
sendedEvent = e;
}
private void DatagridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
sendedEvent = e;
//
timer1.Enabled = true;
//
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!double_click)
{
// DO your simple click stuff and USE sendedEvent if needed
}
else
{
double_click = false;
// DO your Doubleclick stuff and USE sendedEvent if needed
}
timer1.Stop();
timer1.Enabled = false;
}
Hope this will help ^^

Categories