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.
Related
I have a panel with three groupboxes. Each groupbox has its control(combobox, labels, textboxes etc.). Groupbox1 is default when the panel is loaded. A combobox with two items determines when groupbox2 becomes visible while groupbox1 disappear so that the user can input certain information in it.
from properties, groupbox1: visible = "true" groupbox2: visible = "false" groupbox3: visible = "false"
My intention is that,
1) Item one does nothing when selected since default groupbox1 and its controls are already visible.
2) Item two makes groupbox1 and its controls hidden and groupbox2 and its controls visible. A combobox with similiar function is in groupbox2 exist to enable the switch back to the groupbox1 is item two is selected.
All this is supposed to occurs during runtime.
There is a third groupbox3 which is triggered by a button on the panel. I intend to make the groupbox3 visible whilst making everything on the panel look dim until a close button is clicked on groupbox3. i want to be able to make changes in the labels in groupbox1 and groupbox2 using a combobox control in groupbox3.
My Problem is
1)how do i make groupbox1 or groupbox2 on the panel appear faded and not totally hidden, while groupbox3 pops up like a messagebox,and also when i click a close button on groupbox3, groupbox3 closes while groupbox1 or groupbox2 becomes visible again.
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
gb1.Visible = cb1.SelectedIndex == 0;
switch (cb1.SelectedIndex)
{
case 0:
gb1.Visible = true;
gb2.Visible = false;
break;
case 1:
gb2.Visible = true;
gb1.Visible = false;
break;
}
}
private void cb2_SelectedIndexChanged(object sender, EventArgs e)
{
gb2.Visible = cb2.SelectedIndex == 0;
switch (cb1.SelectedIndex)
{
case 0:
gb2.Visible = true;
gb1.Visible = false;
break;
case 1:
gb1.Visible = true;
gb2.Visible = false;
break;
}
}
private void bt1_Click(object sender, EventArgs e)
{
gb3.Show();
gb1.Hide();
gb2.Hide();
}
private void bt2_on_gb3_Click(object sender, EventArgs e)
{
gb3.Hide();
gb1.Show();
gb2.Show();
}
The question is still a little unclear, but here's a shot:
Fading
Solution 1
Consider using the Enabled property. This makes the controls in the group box appear faded. That said, its primary function is to prevent the user from interacting with them. You can still change values programatically though. See the second example if don't want to disable the controls.
Example 1
private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
gb1.Enabled = cb1.SelectedIndex == 0;
gb1.Enabled = cb1.SelectedIndex == 1;
// Unless you plan to add more cases to the switch,
// the switch is redundant with the lines above.
//switch (cb1.SelectedIndex)
//{
// case 0:
// gb1.Enabled = true;
// gb2.Enabled = false;
// break;
// case 1:
// gb2.Enabled = true;
// gb1.Enabled = false;
// break;
//}
}
Solution 2
I wasn't sure, but it almost seems like you were looking to simply perform a visual only dimming of the controls (and not block the user from interacting with them), you can experiment with the ForeColor property on each individual control. You could set and restore the ForeColor individually per control or you could do this with by looping through the Controls Collection of the GroupBox. Note that the loop is probably only helpful if all the controls in the group have the same initial forecolor (which you also may want to store so you can restore it).
That said, this is non-standard WinForms behavior and may confuse users.
Example 2
public static readonly Color kFadedColor = Color.DarkGray;
private void SetGroupBoxContentsForeColor(GroupBox aGroupbox, Color aColor)
{
aGroupbox.ForeColor = aColor;
foreach (Control lControl in aGroupbox.Controls)
{
lControl.ForeColor = aColor;
}
}
private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
// Again, if you need to expand the ComboBox entries,
// this will need additional logic
if (cb1.SelectedIndex == 0)
{
SetGroupBoxContentsForeColor(gb1, mPrevioslySavedColorToRestore);
SetGroupBoxContentsForeColor(gb2, kFadedColor);
}
else
{
SetGroupBoxContentsForeColor(gb2, mPrevioslySavedColorToRestore);
SetGroupBoxContentsForeColor(gb1, kFadedColor);
}
}
Solution 3
Also non-standard, but perhaps you want to highlight the groupbox instead.
Example 3
public static readonly Color kHighlightedColor = Color.Orange;
private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
// Again, if you need to expand the ComboBox entries,
// this will need additional logic
if (cb1.SelectedIndex == 0)
{
gb1.ForeColor = kHighlightedColor;
gb2.ForeColor = mPrevioslySavedColorToRestore;
}
else
{
gb2.ForeColor = kHighlightedColor;
gb1.ForeColor = mPrevioslySavedColorToRestore;
}
}
Notes:
The original cb2_SelectedIndexChanged() method is using switch (cb1.SelectedIndex) instead of switch (cb2.SelectedIndex)
ComboBox3 and GroupBox3
Solution
Assuming you want disable the controls to fade them (Fading Solution 1 above), you can do the following. However, you may need to add additional
logic to check the state of ComboBoxes cb1 and cb2 if that is important.
Also, if you need gb3 hidden to start with, either set the Visible property in the WinForms designer, or set gb3.Visible = false; in the Forms constructor following the InitializeComponent() method call.
Example
private void BtnShowGB3_Click(object sender, EventArgs e)
{
// Disable/fade gb1 and gb2
gb1.Enabled = false;
gb2.Enabled = false;
// Make gb3 visible
gb3.Visible = true;
}
private void BtnHideGB3_Click(object sender, EventArgs e)
{
// Enable/unfade gb1 and gb2
gb1.Enabled = true;
gb2.Enabled = true;
// Hide gb3
gb3.Visible = false;
}
I am currently working with an API for a program called Rhinoceros. The program does not allow tabbing through Forms so I am trying to program it in. It works well how I have it, however, when I try to tab from a combobox to a textbox or vice versa the cursor doesn't move. I have tried the select() and focus() function, neither seem to work and I am currently trying SelectNextControl but I cannot seem to get it to work either. If you have any ideas please let me know, anything is helpful.
private void cbNPProjectFolder_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Tab)
{
txtbxNPProjectNum.SelectNextControl(sender as Control, true, false, true, true);
e.Handled = true;
e.SuppressKeyPress = true;
}
}
I ended up figuring it out, If anyone else has this problem:
I selected all of the containers I wanted to tab through, went to events on the bottom right, in the KeyDown box I named it Generic_KeyDown.
private void Generic_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Tab)
{
e.Handled = true;
this.SelectNextControl((Control)sender, true, true, true, true);
e.SuppressKeyPress = true;
}
}
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
I'd like to create a roulette game, and I've made a PictureBox with an image of a roulette table.
I've tried to set Visibility = False in the property and in code:
pictureBox1.Visible = !pictureBox1.Visible;
But when I click on the hidden PictureBox, nothing happens.
Do you have any idea how to make it work?
I want to make it visible from being invisible, by clicking on it.
Once you make the visibility of an Item false, it can no longer be clicked. You can handle the click event on the form or container level, check that the mouse coordinates are contained in the bounds of the PictureBox and use that to make it visible again.
i.e.
//Common eventhandler assigned to all of your PictureBox.Click Events
private void pictureBox_Click(object sender, EventArgs e)
{
((PictureBox)sender).Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
foreach (var item in this.Controls) // or whatever your container control is
{
if(item is PictureBox)
{
PictureBox pb = (PictureBox)item;
if (pb.Bounds.Contains(PointToClient( MousePosition)))
{
pb.Visible = true;
}
}
}
}
Problem with your logic is that once the PictureBox is made invisible, you can no longer click it anymore, since it's no longer on the form, and while you can for sure click the empty space it left, you're still not clicking it.
A possibility would be to, instead of making visible/invisible, to put/remove its background picture, so it appears to have disappeared but in fact it's still there, still able to receive clicks and respond to events.
If you want to make the pictureBox1 not visible, you would write pictureBox1.Visible = false;
Edit: What you are doing should work. I created a winform and put a picture box in it. I double clicked the picture box to make Click event handler and typed pictureBox1.Visible = !pictureBox1.Visible;. When I debug the program and click on the picture box, it turns invisible. I can't think of why it isn't working for you.
Edit: I understand what you want now. To make it visible on the click of it when it is invisible. Unfortunately I don't know how to do that.
A maybe-silly but simple way might be to put one picture box on top of the other. pictureBox1 would be your picture, pictureBox2 would have no picture. Then you could do something like this:
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = false;
pictureBox2.Visible = true;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
pictureBox2.Visible = false;
}
You could put each of your PictureBox controls in a Panel control. Then handle the Click event of both the Panel and the PictureBox.
private void panel1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}
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();