I have created many groupbox staked one on top of the other and made them to be invisible however ,if i call the first group box it shows but if i call the other groupboxes they will not show
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox2.Show();// is not showing
}
The second related problem is that if i try an if statement it the selected if statement shows nothing at all
private void buttonFinish_Click(object sender, EventArgs e)
{
if (comboBoxType.Text == "Car" && comboBoxName.Text == "BMW"
" && radioButtonBlack.Checked){
if (checkBoxTwoseater.Checked || radioButtonLeather.Checked ||
radioButtonBooster.Checked ){
groupBox1.show}
I cannot insert an image because i am new
Well, you said they are all on top of each other, so what is likely happening is the second call works just fine, but it is actually appearing behind the first one.
What you need to write is:
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish2_Click(object sender, EventArgs e)
{
groupBox1.Hide();
groupBox2.Show();// is showing now!
}
Note that this code is odd, since both methods appear to be named the same. I changed that to make "compilable" code, but you should check yours to make sure that it isn't causing a problem.
Show just sets the Visible property to true, it does not affect the Z-Order (see MSDN)
The second problem will require you stepping through the code with a debugger and checking all your conditions, you haven't given enough information for us to help.
Related
At the beginning, I had 5 buttons in a panel and they worked perfectly.
For example,
private void btnFlipX_Click(object sender, EventArgs e)
{
imgBox.Image.RotateFlip(RotateFlipType.Rotate180FlipX);
imgBox.Refresh();
}
However, when I moved them to groupbox, they did not work anything. I clicked on a button in groupbox and there was anything in there (there is no code in them). For example,
private void btnFlipX_Click_1(object sender, EventArgs e)
{
}
Anyone can give me the reason of this problem?
It looks like VS just added a new event method for your automatically and named it btnFlipX_Click_1 instead of btnFlipX_Click; Are you sure you are assigning the right event (Where the += is)
private void droplesson_SelectedIndexChanged(object sender, EventArgs e)
{
if(e.Equals("LESSON1"));
reload("LESSON1.txt");
if(e.Equals("LESSSON2"));
reload("LESSON2.txt");
if(e.Equals("LESSON3"));
reload("LESSON3.txt");
if(e.Equals("LESSON4"));
reload("LESSON4.txt");
if (e.Equals("LESSON5"));
reload("LESSON5.txt");
}
Above code is not working. I want to change the dropdown menu such that when i select the particular lesson it reload that lesson.enter image description here
You added ';' at the end of every line, including the 'if' lines, soo all of the 'reload' calls got executed...
This is how your code should look:
private void droplesson_SelectedIndexChanged(object sender, EventArgs e)
{
if(e.Equals("LESSON1"))
reload("LESSON1.txt");
if(e.Equals("LESSSON2"))
reload("LESSON2.txt");
if(e.Equals("LESSON3"))
reload("LESSON3.txt");
if(e.Equals("LESSON4"))
reload("LESSON4.txt");
if (e.Equals("LESSON5"))
reload("LESSON5.txt");
}
So i have been browsing around the great mind of Google but have not found a working solution to this; I have a listbox (instanceSelection) and a label (instanceTxt). I want the instanceTxt to be the same as the instanceSelection's text when I select an item in the collection.
Here is the code line that I thought would work earlier:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
instanceTxt.Text = (string)this.instanceSelection.SelectedValue.Text;
}
But in one time it didn't change, and another code block it changed to "0". I also sometimes get a null error when using "ToString".
Thanks,
William
Try this:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
if(instanceSelection.SelectedIndex > -1)
instanceTxt.Text = instanceSelection.Items[instanceSelection.SelectedIndex].ToString();
}
I'm a beginner and have an assignment in which I must program the game of NIM. I begin with 15 "tokens" and at each turn a maximum of three can be removed, or "hidden". So far I am hiding these tokens on click by doing the following.
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
I simply copied and pasted that multiple times and changed the button numbers so that my buttons will close on click. This might be obvious, but is there a more efficient way to do this, instead of having 15 button close methods?
You can use the same click event for every single button, and make use of the sender object, casting it to Button:
private void buttonsToClose_Click(object sender, EventArgs e)
{
((Button)sender).Visible = false;
}
Then just add that handler to every single button you want to close itself on click.
Note, though, that this will throw an InvalidCastException if you or anyone else uses this handler on an object that is not a Button, so if you're actually going to use this code I would add some sort of conditional to check the real type of the sender.
Additionally, you could reuse this for any Control object by casting sender to Control instead, given that Button inherits from Control, and all Control objects have the Visible property. Here's an example, with a conditional to guard against an invalid cast:
private void controlToMakeInvisible_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Control))
{
((Control)sender).Visible = false;
}
}
A final note - it seems from your post like you may have a slight misunderstanding about the way events are created and wired in with objects in Windows Forms. If you go into the Designer, add a click event, and see it pop into your Form code as follows:
private void button1_Click(object sender, EventArgs e)
the name of this method has no bearing on its function. The button1 part of button1_Click doesn't actually have any logical linkage with the Button button1 - it's just the default name assigned by the Designer. The actual assignment of the method button1_Click to the Button.Click event is auto-generated into your Form's Designer.cs method.
The point of this is that if you copy and paste button1_Click and change every incidence of button1 with button2, like so:
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
it's not going to fire when button2 gets clicked. In actual fact, it's never going to fire at all, because the method hasn't actually been connected to any controls/events.
just call your event in a foreach loop.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}
void button_Click(object sender, EventArgs e)
{
((Control) sender).Visible = false;
}
if you change:
Controls.OfType<Button>()
to
Controls.OfType<Control>()
it will set visible to false for any Control. so you can control what item you want the event to be raised for easily.
OfType summary: Filters the elements of an IEnumerable based on a specified type.
I am hoping someone here can help me, i have a Tabless Control on my windows forms application and basically because the tabs are purposely hidden i have added 2 buttons to each tab "Next" and "Back".
This is the code snippet i have for my "Next" button:
private void nextbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
this.toolStripStatusLabel8.Text = System.DateTime.Now.ToString();
}
Which works fine, however when i use the exact same theory on the "Back" button it does not work:
private void backbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabmain;
this.toolStripStatusLabel1.Text = System.DateTime.Now.ToString();
}
So my question is how does one go to a previous tabpage from a button? I have looked through here and tried all of the links that came up but nothing has worked any ideas?
You should use the SelectedIndex property instead of using concrete TabPage instances. This way it will still work when you decide to change the order of the pab pages or add new pages:
private void previousButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex > 0)
{
tabControl1.SelectedIndex--;
}
}
private void nextButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1)
{
tabControl1.SelectedIndex++;
}
}
Since there is no "Tabless" tab control in .NET Framework I can only assume that it works similar to the standard TabControl. If the solution doesn't work you should give us some information about the actual class you use.
BTW: There is no need to repeat the buttons on each page. Why don't you just put the buttons outside the TabControl?
Also: I see that you use a ToolStripStatusLabel to show the current time. Instead of updating it each time the user clicks somewhere add a Timer to your form. Set its Interval to 1000 and handle its Tick event. Update the label there:
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString();
}
This way it updates constantly and again there is no need to repeat anything. You need to call timer1.Start() in the form's constructor.