how to remove a button?

  • Thread starter Thread starter Geoff Cox
  • Start date Start date
G

Geoff Cox

Hello,

I have a button which the user clicks to move to the next question and
when the last question is answered I would like the button to be
removed. How do I do this?

Thanks

Geoff
 
Geoff,

You can call the Remove or RemoveAt methods on the Controls collection
that is exposed by the parent control/form. You can also set the Visible
property on the control to false, but this will not remove the instance of
the control, it will just hide the control (you might want to bring it back
if you are navigating back and forth).

Hope this helps.
 
Geoff,

You can call the Remove or RemoveAt methods on the Controls collection
that is exposed by the parent control/form. You can also set the Visible
property on the control to false, but this will not remove the instance of
the control, it will just hide the control (you might want to bring it back
if you are navigating back and forth).

Nicholas,

Thanks! The "make invisible" works fine - wonder if you might help
with this - I want ot be able to add the various TrackBar values to
the data.txt file until there are no more questions to answer ... not
sure how to keep the file open.

I would like to close the file after

this.button1.Visible = false;

but not sure how to refer to the tw.Close()

Geoff.

private void button1_MouseClick(object sender, MouseEventArgs e)
{
TextWriter tw = new StreamWriter("d:\\a-temp1\\data.txt");
tw.WriteLine(trackBar1.Value);
tw.Close();

++qnumber;

if (qnumber == LHSquestions.Length)
{
endMessage();
}
else
{

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];

}
}

private void endMessage()
{
this.label1.Text = "Finished!";
this.label2.Text = "Thank you";
this.button1.Visible = false;
}
 
Geoff,

I wouldn't bother writing the values as they are selected (it would be a
pain to "erase" them from your text file if you navigated back and forth).

Rather, I would wait until you have all the values (store them in
memory), and then write them to the file once when the operation is
complete.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Geoff,

You can call the Remove or RemoveAt methods on the Controls collection
that is exposed by the parent control/form. You can also set the Visible
property on the control to false, but this will not remove the instance of
the control, it will just hide the control (you might want to bring it
back
if you are navigating back and forth).

Nicholas,

Thanks! The "make invisible" works fine - wonder if you might help
with this - I want ot be able to add the various TrackBar values to
the data.txt file until there are no more questions to answer ... not
sure how to keep the file open.

I would like to close the file after

this.button1.Visible = false;

but not sure how to refer to the tw.Close()

Geoff.

private void button1_MouseClick(object sender, MouseEventArgs e)
{
TextWriter tw = new StreamWriter("d:\\a-temp1\\data.txt");
tw.WriteLine(trackBar1.Value);
tw.Close();

++qnumber;

if (qnumber == LHSquestions.Length)
{
endMessage();
}
else
{

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];

}
}

private void endMessage()
{
this.label1.Text = "Finished!";
this.label2.Text = "Thank you";
this.button1.Visible = false;
}






Hope this helps.
 
Geoff,

I wouldn't bother writing the values as they are selected (it would be a
pain to "erase" them from your text file if you navigated back and forth).

Rather, I would wait until you have all the values (store them in
memory), and then write them to the file once when the operation is
complete.

OK - makes sense

Cheers

Geoff
 
Rather, I would wait until you have all the values (store them in
memory), and then write them to the file once when the operation is
complete.

Nicholas,

I'm trying the code below but when I click the button after the 2nd
question has appeared I get an "index out of bounds of the array"
error message re the line

this.results[qnumber] = trackBar1.Value.ToString();

and cannot see why ...

Suggestions please!

Cheers

Geoff

------------------code-----------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace slider3
{
public partial class Form1 : Form
{

private string[] LHSquestions = {"question 1","question 2"};
private string[] RHSquestions = {"question 1", "question 2" };
private int qnumber = 0;
private string[] results = { "fred" };
private int count = 0;
public Form1()
{
InitializeComponent();

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}

private void trackBar1_Scroll(object sender, EventArgs e)
{

}

private void button1_MouseClick(object sender, MouseEventArgs
e)
{

this.results[qnumber] = trackBar1.Value.ToString();

++qnumber;

if (qnumber == LHSquestions.Length)
{
endMessage();
}
else
{

this.label1.Text = LHSquestions[qnumber];
this.label2.Text = RHSquestions[qnumber];

}
}

private void endMessage()
{
this.label1.Text = "Finished!";
this.label2.Text = "Thank you";
this.button1.Visible = false;

TextWriter tw = new StreamWriter("d:\\a-temp1\\data.txt");
for (count = 0; count < LHSquestions.Length; count++)
{
tw.WriteLine(results[qnumber]);
}
tw.Close();

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top