I'm trying to create an Update button to let me insert data into a gridview and after creating the class, the button does nothing and I don't understand why that is the case.
private void Basic()
{
// no updates allowed
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.ReadOnly = true;
//UpdateButton is available
UpdateButton.Enabled = true;
//Save and Cancel Unavailable
SaveButton.Visible = false;
CancelButton.Visible = false;
sTableAdapter.Fill(dataSet1.S);
}
This is the UpdateButton Event
private void UpdateButton_Click(Object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = true;
dataGridView1.AllowUserToDeleteRows = true;
dataGridView1.ReadOnly = false;
UpdateButton.Enabled = false;
SaveButton.Visible = true;
CancelButton.Visible = true;
}
And this should be the update class:
this.UpdateButton.Location = new System.Drawing.Point(12, 63);
this.UpdateButton.Name = "UpdateButton";
this.UpdateButton.Size = new System.Drawing.Size(103, 23);
this.UpdateButton.TabIndex = 0;
this.UpdateButton.Text = "Update";
this.UpdateButton.UseVisualStyleBackColor = true;
The connection is made with a accdb.
The Update button isn't working on click, what did I do wrong?
Related
I have 5 buttons and 5 labels next to each button. When i run the app i expect the first button to be enabled and the the rest disabled and greyed out with the labels. after i click the first button it should disable with the label and enable the second button, and so forth with all the other buttons.
this way is to long, is there a better way of doing this?
private void Form1_Load(object sender, EventArgs e)
{
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
private void btn1_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = true;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = true;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
private void btn2_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = true;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = true;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
}
private void btn3_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = true;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = true;
lblStep5.Enabled = false;
}
private void btn4_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = true;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = true;
}
private void btn5_Click(object sender, EventArgs e)
{
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
Let all these buttons and labels are inside a container(if it doesn't mean that you can use this.Controls as well if the form contains these buttons and labels only). Let it be pnlContainer, Now you can try something like this:
public void ButtonController(Button buttonToEnable, Label labelToenable)
{
foreach (Control ctrl in panel1.Controls)
{
if (ctrl == buttonToEnable || ctrl == labelToenable)
{
ctrl.Enabled = true;
}
else
{
ctrl.Enabled = false;
}
}
}
So in Form1_Load you want to enable btn1 and lblStep1 so the call should be :
ButtonController(btn1,lblStep1);
For btn1_Click the method call will be like ButtonController(btn2,lblStep2);. in short, you can pass the button and label that you want to enable to this method, which will disable rest of controls in the container.
private void txtenable (Boolean txtenable)
{
if(txtenable== false)
{
txtname.Enabled = false;
txtTel.Enabled = false;
txtmobile.Enabled = false;
txtAdress.Enabled = false;
}
else
{
txtname.Enabled = true;
txtTel.Enabled = true;
txtmobile.Enabled = true;
txtAdress.Enabled = true;
}
}
I want use this class but i can not call textboxes. How can call textbox in class?
First you need to be able to accept the TextBox objects within the class, then you can manipulate them how you see fit. I haven't actually tried this, but this is how I would go about setting it up.
public class YourClass
{
TextBox txtName;
TextBox txtTel;
TextBox txtMobile;
TextBox txtAddress;
private void txtenable (Boolean txtenable, TextBox txtName, TextBox txtTel, TextBox txtMobile, TextBox txtAddress)
{
if(txtenable== false)
{
txtName.Enabled = false;
txtTel.Enabled = false;
txtMobile.Enabled = false;
txtAddress.Enabled = false;
}
else
{
txtName.Enabled = true;
txtTel.Enabled = true;
txtMobile.Enabled = true;
txtAddress.Enabled = true;
}
}
In order for you to access the textboxes from within your class you will need to pass them such as:
public class OtherClassContainingTextBoxes
{
private void SomeEvent(object sender, EventArgs e){
txtenable(true, txtName, txtTel, txtMobile, txtAddress);
}
However, based on the example provided, I am unsure why you wouldn't do this in a method within the class you have your textboxes.
You could do something on pageload:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["enable"] == false){
txtenable(false);
}else{
txtenable(true);
}
}
private void txtenable (Boolean txtenable)
{
if(txtenable== false)
{
txtName.Enabled = false;
txtTel.Enabled = false;
txtMobile.Enabled = false;
txtAddress.Enabled = false;
}
else
{
txtName.Enabled = true;
txtTel.Enabled = true;
txtMobile.Enabled = true;
txtAddress.Enabled = true;
}
}
I am trying to disable all buttons I am using after a time period of inactivity using the timer from toolbox. I can currently get it to work by just disabling the relevant buttons in
private void timer1_Tick(object sender, EventArgs e)
but this disables in ten seconds regardless of activity or inactivity.
Is it possible for the timer to work only after 10 seconds of no activity?
Here is the code I am using.
private void timer1_Tick(object sender, EventArgs e) {
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button10.Enabled = false;
button11.Enabled = false;
button12.Enabled = false;
button13.Enabled = true;
button14.Enabled = true;
button14.Visible = false;
button15.Enabled = true;
button15.Visible = false;
button17.Enabled = false;
button17.Visible = false;
button18.Visible = false;
button19.Visible = false;
button20.Enabled = false;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
checkBox1.Visible = false;
checkBox2.Visible = false;
label15.Visible = false;
label16.Visible = false;
label21.Visible = false;
}
Any help would be greatly appreciated.
Just add the following event to each button that might be clicked and be counted as 'active':
public void ResetTimer(object sender, EventArgs e)
{
timer.Stop();
timer.Start();
}
...
button1.Click += ResetTimer;
The Timer in Winforms doesn't have a Reset method (nor does any of the other timer classes), so you'll have to stop it first and then start it again.
Solution 1:
Reset Time every time user click any button.
Solution 2: ( Not Recommended )
Have a global variable LastActiveTime, Every-time somebody clicks any
button :
LastActiveTime = DateTime.Now()
Parallel running Timer/Thread will check every second if inactive time is more than 10
second and will do accordingly.
I have my code written like so:
private void radioSelectButton_Click(object sender, EventArgs e)
{
if (pteriRadio.Checked) // Selecting avatar to be displayed from here on out.
//This avatar will also be displayed on the game board.
{
pteriBox1.Visible = true;
xweetokBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Pteri Inventory");
player1avatar = "pteri";
}
else if (xweetokRadio.Checked)
{
xweetokBox1.Visible = true;
pteriBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Xweetok Inventory");
player1avatar = "xweetok";
}
else if (ixiRadio.Checked)
{
ixiBox1.Visible = true;
pteriBox1.Visible = false;
xweetokBox1.Visible = false;
label1.Text = ("Ixi Inventory");
player1avatar = "ixi";
}
characterSelectBox.Visible = false;
radioSelectButton.Visible = false;
characterSelectBox2.Visible = true;
radioSelectButton2.Visible = true;
}
It seems as though the visibility changes should display when I have it like this, with the changes within the button click but outside of the if statements (It doesn't matter what the user selects, once selected the option to select needs to disappear for that user.)Yet the visibility changes don't execute. What am I missing here?
If I nest the if-statements as suggested by a previous person, here's what I have:
private void radioSelectButton_Click(object sender, EventArgs e)
{
if (pteriRadio.Checked) // Selecting avatar to be displayed from here on out.
//This avatar will also be displayed on the game board.
{
pteriBox1.Visible = true;
xweetokBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Pteri Inventory");
player1avatar = "pteri";
if (xweetokRadio.Checked)
{
xweetokBox1.Visible = true;
pteriBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Xweetok Inventory");
player1avatar = "xweetok";
if (ixiRadio.Checked)
{
ixiBox1.Visible = true;
pteriBox1.Visible = false;
xweetokBox1.Visible = false;
label1.Text = ("Ixi Inventory");
player1avatar = "ixi";
}
characterSelectBox.Visible = false;
radioSelectButton.Visible = false;
characterSelectBox2.Visible = true;
radioSelectButton2.Visible = true;
}
}
}
Now, not only do the visible items not swap over, two of the character choices don't display.
It looks like the radio buttons here can be checked/unchecked together. I think you should use separate if statements for each condition instead of else if.
You need to Invalidate your form to make it redraw after you change the properties. Assuming the radioSelectButton_Click method is in the same form, calling this.Invalidate() at the end of the method should force the redraw.
it's my first time in C# form and I dont know if what I am doing is correct.
below is my form workspace in C# and you can see that there's a lot of things there and it's messy...
i use the this.BackgroundImage = image; to change the background image of the form and i just turn the visible property of each control on whenever they are needed so that it look nice when i run the app (im still not finish with it though, it's hard to work in a form that's messy)
is there anyway i can work in an organized way like i could work with a lot of forms, instead of one, and just interconnect them like in powerpoint where you could have many slides and just use a hyperlink to point to other slides... please help...
and here's my code (sorry for the noob coding style)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Microsoft.VisualBasic;
namespace PProj1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backfromaboutandhow.Visible = false;
nextbutton.Visible = false;
backfromreserve.Visible = false;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
finish.Visible = false;
backtoreserve.Visible = false;
N1.Visible = false;
N2.Visible = false;
N3.Visible = false;
N4.Visible = false;
N5.Visible = false;
N6.Visible = false;
N7.Visible = false;
N8.Visible = false;
S1.Visible = false;
S2.Visible = false;
S3.Visible = false;
S4.Visible = false;
S5.Visible = false;
S6.Visible = false;
S7.Visible = false;
S8.Visible = false;
E1.Visible = false;
E2.Visible = false;
E3.Visible = false;
E4.Visible = false;
W1.Visible = false;
W2.Visible = false;
W3.Visible = false;
NW1.Visible = false;
NW2.Visible = false;
NE1.Visible = false;
NE2.Visible = false;
SW.Visible = false;
SE.Visible = false;
}
OleDbConnection con;
OleDbCommand cmd;
OleDbDataAdapter adapter;
DataSet ds;
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
aboutoldtrafford.Location = new Point(16, 9);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
aboutoldtrafford.Location = new Point(9, 9);
}
private void pictureBox2_MouseHover(object sender, EventArgs e)
{
howtogetthere.Location = new Point(16, 62);
}
private void pictureBox2_MouseLeave(object sender, EventArgs e)
{
howtogetthere.Location = new Point(9, 62);
}
private void pictureBox3_MouseHover(object sender, EventArgs e)
{
reserveaticket.Location = new Point(16, 113);
}
private void pictureBox3_MouseLeave(object sender, EventArgs e)
{
reserveaticket.Location = new Point(9, 113);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.about;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = true;
}
private void pictureBox4_MouseHover(object sender, EventArgs e)
{
backfromaboutandhow.Location = new Point(566, 511);
}
private void pictureBox4_MouseLeave(object sender, EventArgs e)
{
backfromaboutandhow.Location = new Point(559, 511);
}
private void pictureBox4_MouseClick(object sender, MouseEventArgs e)
{
backfromaboutandhow.Visible = false;
Image image = Properties.Resources.oldtraffordwelcome1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = true;
howtogetthere.Visible = true;
reserveaticket.Visible = true;
}
private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.howto;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = true;
}
private void pictureBox3_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.reg1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = false;
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
nextbutton.Visible = true;
backfromreserve.Visible = true;
}
private void pictureBox5_MouseHover(object sender, EventArgs e)
{
nextbutton.Location = new Point(545, 463);
}
private void pictureBox5_MouseLeave(object sender, EventArgs e)
{
nextbutton.Location = new Point(539, 463);
}
private void pictureBox6_MouseHover(object sender, EventArgs e)
{
backfromreserve.Location = new Point(30, 463);
}
private void pictureBox6_MouseLeave(object sender, EventArgs e)
{
backfromreserve.Location = new Point(36, 463);
}
private void pictureBox6_MouseClick(object sender, MouseEventArgs e)
{
nextbutton.Visible = false;
backfromreserve.Visible = false;
Image image = Properties.Resources.oldtraffordwelcome1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = true;
howtogetthere.Visible = true;
reserveaticket.Visible = true;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
finish.Visible = false;
backtoreserve.Visible = false;
}
private void pictureBox5_MouseClick(object sender, MouseEventArgs e)
{
if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
{
Image image = Properties.Resources.reg21;
this.BackgroundImage = image;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
label5.Visible = false;
nextbutton.Visible = false;
backfromreserve.Visible = false;
finish.Visible = true;
backtoreserve.Visible = true;
N1.Visible = true;
N2.Visible = true;
N3.Visible = true;
N4.Visible = true;
N5.Visible = true;
N6.Visible = true;
N7.Visible = true;
N8.Visible = true;
S1.Visible = true;
S2.Visible = true;
S3.Visible = true;
S4.Visible = true;
S5.Visible = true;
S6.Visible = true;
S7.Visible = true;
S8.Visible = true;
E1.Visible = true;
E2.Visible = true;
E3.Visible = true;
E4.Visible = true;
W1.Visible = true;
W2.Visible = true;
W3.Visible = true;
NW1.Visible = true;
NW2.Visible = true;
NE1.Visible = true;
NE2.Visible = true;
SW.Visible = true;
SE.Visible = true;
}
else
{
label5.Visible = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
con = new OleDbConnection(#" provider=Microsoft.ace.Oledb.12.0; data source=C:\OldTrafford.accdb; Persist Security Info=False");
loaddata();
}
void loaddata()
{
adapter = new OleDbDataAdapter("select * from oldtraff", con);
ds = new DataSet(); //student-> table name in stud.accdb file
adapter.Fill(ds, "oldtraff");
ds.Tables[0].Constraints.Add("pk_ID", ds.Tables[0].Columns[0], true);//creating primary key for Tables[0] in dataset
//dataGridView1.DataSource = ds.Tables[0];
}
private void finish_MouseHover(object sender, EventArgs e)
{
finish.Location = new Point(545, 553);
}
private void finish_MouseLeave(object sender, EventArgs e)
{
finish.Location = new Point(539, 553);
}
private void backtoreserve_MouseHover(object sender, EventArgs e)
{
backtoreserve.Location = new Point(30, 553);
}
private void backtoreserve_MouseLeave(object sender, EventArgs e)
{
backtoreserve.Location = new Point(36, 553);
}
private void backtoreserve_MouseClick(object sender, MouseEventArgs e)
{
Image image = Properties.Resources.reg1;
this.BackgroundImage = image;
aboutoldtrafford.Visible = false;
howtogetthere.Visible = false;
reserveaticket.Visible = false;
backfromaboutandhow.Visible = false;
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
label1.Visible = true;
label2.Visible = true;
label3.Visible = true;
label4.Visible = true;
nextbutton.Visible = true;
backfromreserve.Visible = true;
finish.Visible = false;
backtoreserve.Visible = false;
N1.Visible = false;
N2.Visible = false;
N3.Visible = false;
N4.Visible = false;
N5.Visible = false;
N6.Visible = false;
N7.Visible = false;
N8.Visible = false;
S1.Visible = false;
S2.Visible = false;
S3.Visible = false;
S4.Visible = false;
S5.Visible = false;
S6.Visible = false;
S7.Visible = false;
S8.Visible = false;
E1.Visible = false;
E2.Visible = false;
E3.Visible = false;
E4.Visible = false;
W1.Visible = false;
W2.Visible = false;
W3.Visible = false;
NW1.Visible = false;
NW2.Visible = false;
NE1.Visible = false;
NE2.Visible = false;
SW.Visible = false;
SE.Visible = false;
}
}
}
You should probably consider using "User Controls" to separate your screens. Each of these controls should manage it's own interactions with the user. If you need to let you main form know what's going on with one of these controls, you can use an event (just like any other type of control).
For the example you've shown, you'd have 5 controls (one for each screen), and maybe a few events on each control which indicate the user has pressed the "Next" button (or whatever).
The main control should change which control is displayed at any given time... and it'll probably be easiest to do this through code instead of the GUI designer.
EDIT:
This link is super old, but it'll give you an idea of what I mean - and save me from making a bunch of screen shots and pasting them in here:
http://msdn.microsoft.com/en-us/library/aa302342.aspx
http://knol.google.com/k/creating-custom-controls-with-c-net#
I think you could make wizard like your application. Also as others mentioned in their answers use user controls to "group" controls to manage them easily.
You could make a usercontrol for each and simply hide/show that usercontrol.