Disable And Change Button Image On Click in C# - c#

I have a button that on click I would like to be disabled and it's background image to be changed to null here is the code I have that happens on button click
private void levelOne1001_Click(object sender, EventArgs e)
{
levelOne1001.Enabled = false;
levelOne1001.BackgroundImage = null;
scoreClass.genRandomNumber(100);
scoreClass.valOfQuestion = 100;
q1001 = true;
openQuestionForm();
}
And here is the code from openQuestionForm();
private void openQuestionForm()
{
QuestionForm qForm = new QuestionForm();
scoreClass.iCount++;
qForm.Show();
this.Hide();
}
And here is where I call this form back up
Level1Form l1Form = new Level1Form();
l1Form.Show();
How the process works is Button on Original form is clicked goes to a Question form, button on Question form is clicked it goes back to Original Form. But when I go back to the original form the button is still enabled and the image is still there. Is there any way to fix this?
EDIT: Forgot to say this was in WinForms

You are instantiating a new Level1Form, so it's returning to its default state, causing the button to return to its default state. There are a few possible approaches:
Add a parameter to Level1Form's constructor that indicates what state the button should be in, something like
Level1Form(bool enableButton) {
initComponent();
if(!enableButton) {
levelOne1001.Enabled = false;
levelOne1001.BackgroundImage = null;
}
}
Or, grab the same form again and reuse it. You will need to keep a reference to it somewhere and tell it to show itself again. Alternately, you can grab it out of Application.OpenForms

You're creating a new Level1Form instance, which has nothing to do with the existing instance that you modified.
You need to re-show the original instance.

You need to remember your initial form instance in a member outside the method and call show on it.
Level1Form l1Form;
private void FirstTimeCreate()
{
l1Form = new Level1Form();
}
private void Reshow()
{
l1Form.Show();
}

First add ImageList1 to your design page. Then click on the arrow on top of the imageList, then click "choose images" and add all the different kind of images that you want your button to change upon clicking on it. Then write the below code:
int im = 3;
private void levelOne1001_Click(object sender, EventArgs e)
{
levelOne1001.BackgroundImage = imageList1.Images[im];
switch (im)
{
case 0:
case 1:
case 2:
im++;
break;
default:
im =0; // assuming you have 3 images; if you are at image 3 and click then let it go to image with index 0 (which is the beginning).
break;
}
// levelOne1001.Enabled = false; // you can add this code above as you see fit
}

Related

How to make correct initialization of ComboBox?

I need show droppeddown combobox after start program.
I need in dropdown style only, not simple style.
This is simple fragment of my program:
private void Form1_Shown(object sender, EventArgs e)
{
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
}
But I found the watch sign as cursor till I click on Form in any place.
I guessed that my Form have not fully active state and wait for something.
When I click Form (or combobox or any control) by LBM, it activated fully and all works fine.
Of course the combobox is dropup then, so I need click combobox twice.
Đ•ell me please what is correct initialization of such style combobox without "Cursor = Cursors.Default;"
You can simply wait until cursor is the default:
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
Application.DoEvents simply process messages from the window queue, so you can process message until you get that cursor is the default. In that moment, you can drop down your control without problem.
If you prefer, create a extension method for the Form:
public static class FormExtends
{
public static void WaitToDefaultCursor(this Form form)
{
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
}
}
And use it:
this.WaitToDefaultCursor();
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
NOTE: I use Cursor.Default but not to change the cursor. The form is processing messages and it's difficult to select a good moment to drop down the control.

C# - How to choose which Control User load first?

I'm using WinForm to do my little CRM.
I got MainWindow form and on this form 3 panels ( 1 Top/ 1Left/ the other part is filled up by 7 Control User )
All the Control User are on top of each other, and when i click on some button( attached to the left panel) the called CU is brought to the front.
public void BtnContact_Click(object sender, EventArgs e)
{
contactControl1.Visible = true;
contactControl1.BringToFront();
panelBar.Height = BtnContact.Height;
panelBar.Top = BtnContact.Top;
employe1.Visible = false;
comptaControl1.Visible = false;
histoControl1.Visible = false;
alerteControl1.Visible = false;
voyageursControl1.Visible = false;
parametresControl1.Visible = false;
}
I don't want all the CU to load at the start of the App, but i want them to be launch when i click on the button on the left. And let say if i opened one and now opening a new one it close the one who was opened.
If i have no choice to open everything ( which i doubt ) how can i choose the one i want to open first or second etc ??
Thank you
I don't want all the CU to load at the start of the App
Use properties tab and make visible=false or use formload event to prevent all cu loading at the start.
private void Form1_Load(object sender, System.EventArgs e)
{
//use code to make anything visible or invisible
}
you can put all controls that should be shown or hidden on the panel.
This is called the other part is filled up by 7 Control User in the question, so just use Panel to store all those controls.
Then you can introduce new field on your form to store current control index that is shown and to write button click that will check the index and activate next control
int currentIndex = -1;
public void BtnContact_Click(object sender, EventArgs e)
{
currentIndex++;
if (currentIndex >= panelFor7Control.Controls.Count)
currentIndex = 0;
foreach (Control c in panelFor7Control.Controls)
c.Visible = false;
Control control2show = panelFor7Control.Controls[currentIndex] ;
control2show.Visible = true;
}
Button BtnContact has to be pressed to activate next control on the panel.
PS I suppose that you are trying to load data for the visible control only. You can use Control.VisibleChanged to check is data loaded and to load if control is visible and data was not loaded.
First you need to make instance of your UC, something like this:
public partial class YourUC : UserControl
{
public static YourUC _instance;
public static YourUC Instance
{
get
{
if (_instance == null)
_instance = new YourUC();
return _instance;
}
}
}
Then you need to call and show UC when button is clicked
private void btn_Click(object sender, EventArgs e)
{
if (!pnlControls.Controls.Contains(YourUC.Instance))
{
pnlControls.Controls.Add(YourUC.Instance);
YourUC.Instance.Dock = DockStyle.Fill;
YourUC.Instance.BringToFront();
}
else
{
YourUC.Instance.BringToFront();
}
}
You it sounds like you want to have a "pages" navigation, I'm not use to work with WinForm, but perhaps this post could help you
What is the most efficient way to create a Win form with multiple pages?
It seems that you have to manage what Control you want to display manually.
Note that passing Controls from Visible to Hidden doesn't unload them

Update Controls in Form 1 from Form 2 that was Created by Form 1

I checked and found this article that is referred to MANY times in this type of question, and it is NOT the answer I want...
I have a form, Form_Main frmMainPage, it creates Form_Status frmStatus.
When it does so, I disable the controls on frmMainPage so the user can't mess with things while things are processing.
When things are done processing they can close the frmStatus window to go back to frmMainPage to continue working. I cannot re-enable the controls from frmStatus. I tried to use the frmMainPage_Enter, but it goes crazy when it first loads, so that isn't really an option.
I have the following in Form_Main.cs
public void EnableForm() {
this.gridData.Enabled = true;
this.txtLocation.Enabled = true;
this.txtSupplier.Enabled = true;
this.txtItem.Enabled = true;
this.FillTable("", "", "");
}
When I use this (per article above):
private void btnClose_Click(object sender, EventArgs e) {
Form_Main f2 = new Form_Main();
f2.Show();
f2.EnableForm();
this.Close();
}
It creates a second Form_Main, which is not what I want. I want to know how to change the controls in the existing form.
Edit: No, it is not this that was also suggested. Most of the answers deal with changing controls on Form 2 from Form 1 when Form 2 is created by Form 1. In my case I need to do the opposite and change controls on Form 1 FROM Form 2, which was created by Form 1. Kind of a circular thing.
I can think of a couple of ways to do this. First (and most common) is to show the second form modally (which means that the first form's code pauses while the second form's code is running):
private void button1_Click(object sender, EventArgs e)
{
var statusForm = new frmStatus();
// ShowDialog will prevent frmMainPage from being accessible.
// This form's code will pause at the next line until the second form is closed
statusForm.ShowDialog();
}
There are occasions where you want to have both forms accessible at the same time. If this is the case, another method would be to add an event handler for the second form's FormClosed event, where you can re-enable the controls on the first form. This will allow both forms to be accessed at the same time:
private void button1_Click(object sender, EventArgs e)
{
var statusForm = new frmStatus();
// Add an event handler for the second form's FormClosed event, and
// put code in that event handler to re-enable controls on this form
statusForm.FormClosed += statusForm_FormClosed;
// Disable our controls on this form and show the second form
DisableForm();
statusForm.Show();
}
private void statusForm_FormClosed(object sender, FormClosedEventArgs e)
{
// When the second form closes, re-enable controls on this form
EnableForm();
}
private void DisableForm()
{
this.gridData.Enabled = false;
this.txtLocation.Enabled = false;
this.txtSupplier.Enabled = false;
this.txtItem.Enabled = false;
}
public void EnableForm()
{
this.gridData.Enabled = true;
this.txtLocation.Enabled = true;
this.txtSupplier.Enabled = true;
this.txtItem.Enabled = true;
this.FillTable("", "", "");
}
you dont need to do this disable enable.
you just need to show your new from with ShowDialog(); like this:
frmStatus.ShowDialog();
instead of just:
frmStatus.Show();

Enabling/disabling button

I am having trouble getting my 'start' button to show when I select either of the radio boxes.
Ideally, when one of the boxes is selected, the 'start' button will enable and allow to be clicked.
Here is my code for the form, as I am relatively new to C# I'm not sure if I'm posting all of the code you need, I'll post more if required.
public partial class mainForm : Form
{
public mainForm() {
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e) {
}
private void mainForm_Load(object sender, EventArgs e) {
title.Font = new Font("Arial", 10, FontStyle.Bold);
}
private void startButton_Click(object sender, EventArgs e) {
if (radioDice.Checked) {
startButton.Enabled = true; //Activates 'start' button
whichDiceGameForm GameForm = new whichDiceGameForm();
GameForm.Show();
}
if (radioCard.Checked) {
startButton.Enabled = true; //Activates 'start' button
whichCardGame GameForm = new whichCardGame();
GameForm.Show();
}
}
}
[Posting for a friend.]
You have placed your enable code in the Button's Click event Handler while you should do it on your checkboxes changed.
Take this code :
if (radioDice.Checked)
{
startButton.Enabled = true;
}
to radioDice checkbox's changed event handler and this one :
if (radioCard.Checked)
{
startButton.Enabled = true; //Activates 'start' button
}
to radioCard checkbox's changed event handler .
Man, seriously?
ANSWER:
You're trying to enable DISABLED button when clicking on that button. You cannot click DISABLED button. What's more - you're duplicating your code.
Button should be always enabled. You only have two choices. Every choice enabled button. So it should be always enabled. No matter the choice. If there is something hidden and button may be disabled, then enable the button in Radio Click event.
Additional information about your code:
Now. About code duplication. Look what you're doing in startButton_Click. You have duplicated code.
You can do something like:
BaseGameForm f = null;
if(radioDice.Checked)
f = new DiceGameForm();
else
if(radioCard.Checked)
f = new CardGameForm();
f.Show();
(BaseGameForm is base form for every game)
But this is not good solution. Better solution is (somewhere in construtor):
radioDice.Tag = new DiceGameForm();
radioCard.Tag = new CardGameForm();
Then in Start button click you look for checked radio:
foreach(Control c in selectGameTypeGroupBox.Controls) //you could do this using LINQ
{
if((c is RadioButton) && ((RadioButton)c).Checked)
{
((Form)c.Tag).Show();
}
}
But this is still not good solution, because you're creating all game forms at startup and this is stupid.
So the better solution would be to keep game form class name in your radio Tag property and then create object of this class using reflection and Activator.
But this is still not the best solution. But I assume that this is one of your first applications so I won't be telling you now about separating gui from logic. If you want to know more - read on the Internet. Or just ask.

Pop up radwindow on second application onclick on first application

Basically I have two have applications, using same database. I want to pop up a window(may be radwindow) on second application by clicking the button on first application.
what is the better way. I was thinking to update some value on click on first app, and put the timer on second application which after every time period checks the value, if updated , popup the window.
Any help will be appreciated.
Thanks
you can save the pointer of the second application in your first application,when clicking the button on your first application,you can check the Visible property of the second application,and change the value of Visible property.Just like:
Form secondForm;//create a global var to save the pointer of the second application
private void Button1_Click(object sender, EventArgs e)
{
if (secondForm == null || secondForm.IsDisposed)
{
secondForm = new Form2();
secondForm.Visible = true;
}
else
{
secondForm.Visible = true;
}
}

Categories