I am writing a C# web application where I have a menu strip with an option to keep windows always on top. The name of it is alwaysOnTopToolStripMenuItem and during page load I keep it checked because I start with the form being on top:
private void frmMain_Load(object sender, EventArgs e)
{
alwaysOnTopToolStripMenuItem.Checked = true;
}
I then have the following code to allow the user to select and change it based on the condition of the menu item:
private void alwaysOnTopToolStripMenuItem_Click(object sender, EventArgs e)
{
if (alwaysOnTopToolStripMenuItem.Checked == true)
{
MessageBox.Show("TRUE - SETTING TO FALSE");
alwaysOnTopToolStripMenuItem.Checked = false;
this.TopMost = false;
}
if (alwaysOnTopToolStripMenuItem.Checked == false)
{
MessageBox.Show("FALSE - SETTING TO TRUE");
alwaysOnTopToolStripMenuItem.Checked = true;
this.TopMost = true;
}
}
When it's commented out, If I go through the menu option, clicking it doesn't uncheck it and then clicking it again doesn't check it. It always remains checked.
The MessageBox displays both prompt no matter what I choose.
How can I fix the following:
Change check to uncheck and uncheck to check based on the current state?
Also, make the form TopMost based on the selection?
Change your second if statement to an else if, because now it will change it to Checked = true, then in the second check (which equals to true), it'll switch it back to Checked = false
Related
I have a comboBox (cmbPortName) and a command button (btnConnect).
You'd use the drop-down in the comboBox to select a port you want to connect to and then click btnConnect.
I just want to disable the command button till a valid selection is made in ComboBox. I figured the best way to solve this by doing something like
btnConnect.Enabled = True;
until a selection is made in the Combobox.
Is there a better way of doing it? I am quite new to the programming and still learning stuffs.
You need to add a SelectedIndexChanged event handler for the combo box. In the Visual Studio design view for your form, double-click the combo box, or double-click the empty space to the right of the event name in the "Properties" window:
That will generate and bring you to this block of code in your form's .cs file:
private void cmbPortName_SelectedIndexChanged(object sender, EventArgs e)
{
}
And then add whatever code you want to conditionally enable your button:
private void cmbPortName_SelectedIndexChanged(object sender, EventArgs e)
{
// This will enable the button so long as the selected value
// is not null or an empty string.
if (cmbPortName.SelectedItem != null && !string.IsNullOrEmpty(cmbPortName.SelectedItem.ToString()))
btnConnect.Enabled = true;
else
btnConnect.Enabled = false;
}
Disable the button at first.
if(cmbPortName.SelectedIndex > 0)
{
btnConnect.Enabled = True;
}
There is an event for the combobox selected item change, you can write btnConnect.Enabled = True there.
I want to make a quiz, which goes through the questions, keeping in mind that while question 1 is being used, the others are disabled. Once the Next button is clicked it should change directly to Q2, disabling Q1 and so on.
How do I make it disable the previous tab and keep the current one enabled after the Next button is clicked?
As stated previously tabs can be selected by index.
So as before, let's disable all other tabs:
foreach(TabPage tab in tabControl.TabPages)
{
tab.Enabled = false;
}
(tabControl.TabPages[0] as TabPage).Enabled = true;
Now the way to prevent navigating to any other tab is simple:
private void tabControl_Selecting(object sender, TabControlCancelEventArgs e)
{
if (!e.TabPage.Enabled)
{
e.Cancel = true;
}
}
The only downside is that they will appear selectable, meaning they are not grayed out. You would have to do this yourself if you want the look to appear unavailable as well.
Another solution (the simplest I think) :
Using a global variable (here currentSelectedTab)
Using the event Selecting
// currentSelectedTab is the Only Tab I want enabled.
TabPage currentSelectedTab = tabWizardControl.TabPages[0];
private void tabWizardControl_Selecting(object sender, TabControlCancelEventArgs e)
{
int selectedTab = tabWizardControl.SelectedIndex;
//Disable the tab selection
if (currentSelectedTab != selectedTab)
{
//If selected tab is different than the current one, re-select the current tab.
//This disables the navigation using the tab selection.
tabWizardControl.SelectTab(currentSelectedTab);
}
}
A Tab can be accessed by its index, like so:
tabControl.TabPages[0]
So, say you're starting on tab 1 (index = 0), you want to disable all the other tabs.
// This can be done manually in the designer as well.
foreach(TabPage tab in tabControl.TabPages)
{
tab.Enabled = false;
}
(tabControl.TabPages[0] as TabPage).Enabled = true;
Now, when you press the Next button, you want to disable the current tab, enable the next one, AND GO to the next one. But remember to check if the tab exists!
if(tabControl.TabCount - 1 == tabControl.SelectedIndex)
return; // No more tabs to show!
tabControl.SelectedTab.Enabled = false;
var nextTab = tabControl.TabPages[tabControl.SelectedIndex+1] as TabPage;
nextTab.Enabled = true;
tabControl.SelectedTab = nextTab;
DISCLAIMER: This is not tested, but it should be something along these lines.
You stated that you got an error about object not containing a definition for Enabled - my code typecasts each tab page as a TabPage. However I have not tested it.
I followed this way:
i) A global with currentIndex value.
ii) Add SelectedIndexChanged Event Handler to tabControl.
iii) In the SelectedIndexChanged handler set the index back to currentIndex.
iv) Change currentIndex in your NextButton Click Event
This may work:
currentIndex = 0; //global initial setting
tabControl1.SelectedIndexChanged += new EventHandler(tabControl1_SelectedIndexChanged);
void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
tabControl1.SelectedIndex = currentIndex;
return;
}
private void nextButton_Click(object sender, EventArgs e)
{
currentIndex += 1;
if (currentIndex >= tabControl1.TabPages.Count)
{
currentIndex = 0;
}
foreach (TabPage pg in tabControl1.TabPages)
{
pg.Enabled = false;
}
tabControl1.TabPages[currentIndex].Enabled = true;
tabControl1.SelectedIndex = currentIndex;
}
I Googled around but seems my problem happens when two gropboxes are overlapping, in my case they are not overlapping!
Problem is that the Visible property of groupbox doesn't work. what am I trying to do is that groupbox1 is visible when program starts and groupbox2 is not, by clicking on a button it should goes invisible and groupbox2 should appear, clicking the same button this action should be done vice versa.
here is my code:
private void button2_Click(object sender, EventArgs e)
{
if (groupBox2.Visible == false)
{
groupBox1.Visible = false;
groupBox2.Visible = true;
}
if (groupBox1.Visible == false)
{
groupBox1.Visible = true;
groupBox2.Visible = false;
}
}
Your problem is that after the first if-statement, it immediately checks if groupBox1.Visible is false, which it always will be. It then proceeds to flip it back.
Change the if to an else, or at least and else if and your code will work.
I have a number of checkboxes that I want to handle them as toggle buttons. Each one is totally independent from the other and can be either checked or unchecked. When the user clicks on the checkbox/button it stays checked and if the user clicks again, then the checkbox/button returns to the "unchecked" state. Can the checkbox/button check/uncheck itself?
I have been trying for a long time, searching also in the internet and cannot find a solution.
Check the status of the check box, every time the button is clicked, change it.
private void toggButton_CheckedChanged(object sender, EventArgs e)
{
// everytime you click the button checkbox states will change.
if(checkBox.Checked)
{
checkBox.Checked = false;
}
else
{
checkBox.Checked = true;
}
}
The problem is that the checkbox/button is "displayed" as always
pressed i.e. the "blue" color that it has when checked is not changed
giving the impression that it is not unchecked. – user1304490
May be setting CheckBox.ThreeState property false helps.
If the ThreeState property is set to false, the CheckState property value can only be set to the Indeterminate value of System.Windows.Forms.CheckState in code and not by user interaction.
A very short command:
checkBox.Checked = !checkBox.Checked;
You can assign a common event to all Checkboxes. Write this code in your .cs file and select all Checkboxes and assign this event to checkedChange event.
private void chkboxes_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
if(!chk.Checked)
chk.Checked = true;
else
chk.Checked = false;
}
Ref: CheckBox.CheckState property.
it is read and write property so you set it in GUI or through code.
Checked The CheckBox displays a check mark. The control appears sunken.
Unchecked The CheckBox is empty. The control appears raised.
Indeterminate The CheckBox displays a check mark and is shaded. The control appears flat.
write this on checked change event and check it is toggle or not.. it will work as like a switch..
label1.Text = "ThreeState: " + checkBox1.ThreeState.ToString() + "\n" + "Checked: " +
checkBox1.Checked.ToString() + "\n" + "CheckState: " + checkBox1.CheckState.ToString();
for your question's answer check CheckBox.AutoCheck Property
When you want to change check box state for too many check boxes on one form, you need fast reliable code, without complexity.
The best way for doing it is using:
CheckBox.CheckState
you can change Check Boxes state to (unchecked) for example using this code:
checkbox1.CheckState = CheckState.Unchecked;
checkbox2.CheckState = CheckState.Unchecked;
checkbox3.CheckState = CheckState.Unchecked;
For further reading, see this from MSDN.
The question is very unclear but if someone is still struggling with checkboxes:
Toggle between two of them(both checkboxes cannot be ticked at the same time and min. one need to be ticked):
private void chkBoxON_CheckedChanged(object sender, EventArgs e)
{
if (chkBoxON.Checked)
{
chkBoxOFF.Checked = false;
// here some code to do after this checkbox is ticked
}
else
{
chkBoxOFF.Checked = true;
}
}
private void chkBoxOFF_CheckedChanged(object sender, EventArgs e)
{
if (chkBoxOFF.Checked)
{
chkBoxON.Checked = false;
// here some code to do after this checkbox is ticked
}
else
{
chkBoxON.Checked = true;
}
}
If you need only one checked or both unchecked then:
if (chkBoxON.Checked)
{
chkBoxOFF.Checked = false;
}
}
private void chkBoxOFF_CheckedChanged(object sender, EventArgs e)
{
if (chkBoxOFF.Checked)
{
chkBoxON.Checked = false;
}
If you need to change the state of it using the button then:
private void button1_Click(object sender, EventArgs e)
{
chkBoxON.Checked = false;
//chkBoxON.Checked = true; //if need to be checked
}
If you need to disable the possibility to click checkBox then:
chkBoxOFF.Enabled = false;
You just need to use the NOT operator to create the simplest toggle.
checkBox.Checked = !checkBox.Checked;
I am building a WinForms Application in C# .NET
The WinForms Application has a ComboBox where the DropDownStyle is set to DropDownList. When the App is launched, I read an XML file to populate the values of the ComboBox. And, at this time, nothing is selected in the ComboBox by default. As a result, buttons Change and Delete are disabled.
Now, when the user selects a value, I want the buttons Change and Delete to be enabled. So far I have accomplished (although, I am not sure that I have done it in the right way).
I have written the code in the SelectionChangeCommitted Event.
private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
if (cbList.SelectedItem != null)
{
this.btnModify.Enabled = true;
this.btnRemove.Enabled = true;
}
else
{
this.btnModify.Enabled = false;
this.btnRemove.Enabled = false;
}
}
Now, when I chose a value...the buttons get enabled (as expected). The user then clicks on Delete button and we remove the selected value. Now, there is nothing Selected in the cbList but the buttons are still enabled?
What is the function/event where I check if a value is selected or not and then enable/disable the buttons.
At the moment, dont have Visual Studio, so I dont remember which events we have. But you can make this,
private void CheckButtons()
{
if (cbList.SelectedItem != null)
{
this.btnModify.Enabled = true;
this.btnRemove.Enabled = true;
}
else
{
this.btnModify.Enabled = false;
this.btnRemove.Enabled = false;
}
}
and use your func in event
private void cbList_SelectionChangeCommitted(object sender, EventArgs e)
{
CheckButtons();
}
as you said, after deleting, buttons are still visible, so you can put CheckButtons() function after your delete function like
DeleteX();
CheckButtons();