The Default is No Checkbox
When I run the program and Click the Yes Checkbox the program overflowed
private void checkEdit1_Click(object sender, EventArgs e)
{
checkEdit2.Checked = false;
textEdit1.Enabled = true;
answered = true;
optional = textEdit1.Text;
if (!checkEdit1.Checked)
{
checkEdit1.Checked = true;
checkEdit2.Checked = false;
textEdit1.Enabled = true;
optional = textEdit1.Text;
}
}
private void checkEdit2_Click(object sender, EventArgs e)
{
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
if (!checkEdit2.Checked)
{
checkEdit2.Checked = true;
checkEdit1.Checked = false;
textEdit1.Enabled = false;
answered = false;
}
}
What you think is the error ?
Instead of the Click event you should use the CheckedChanged event in this way:
checkEdit1.CheckedChenged += new EventHandler(checkEdit1_CheckedChanged);
checkEdit2.CheckedChenged += new EventHandler(checkEdit2_CheckedChanged);
private void checkEdit1_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
private void checkEdit2_CheckedChanged(object sender, EventArgs e)
{
if(checkEdit1.Checked == checkEdit2.Checked)
checkEdit2.Checked = !checkEdit.Checked;
}
But the best way in this case is to use a group of radio buttons.
Assuming that those methods are wired up to checkEdit1 and checkEdit2 I would advise that you don't make a change to checkEdit1 in checkEdit1_Click as it has already changed - only modify the state of the alternate.
However, when you modify the state of the other, unless you're careful, you're going to get called back. Eventually the computer gives up -- the overflow!
As mentioned in a comment by #Cyborgx37, radio buttons are the better UX choice here!
A possible solution, bind a single method to the OnClick to BOTH checkboxes:
private bool internallyUpdating = false;
private void CheckboxClick(object sender, EventArgs e)
{
if ( !internallyUpdating )
{
// Prevent subsequent changes
internallyUpdating = true;
// Exchange 'checked' state
if ( sender == checkEdit1 )
{
checkEdit2.Checked = !checkEdit2.Checked;
}
else // if (sender == checkEdit2)
{
checkEdit1.Checked = !checkEdit1.Checked;
}
// other logic here..
// restore 'on change' functionality.
internallyUpdating = false;
}
Related
I have the following code which checks each radio button (Temp30, Temp40 and Temp60) and does the necessary things such as turning the wash temperature light on etc...
I want to create an event which handles all 3 radio buttons. I thought it could possibly have something to do with the groupbox they are in? (it is called TempGroupBox)
Any help would be much appreciated!
private void Temp30_CheckedChanged(object sender, EventArgs e)
{
if (Temp30.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._30degrees;
SpeedGroupBox.Enabled = true;
}
}
private void Temp40_CheckedChanged(object sender, EventArgs e)
{
if (Temp40.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._40degrees;
SpeedGroupBox.Enabled = true;
}
}
private void Temp60_CheckedChanged(object sender, EventArgs e)
{
if (Temp60.Checked)
{
MainDisplayLabel.Text = (" SELECT SPIN SPEED");
WashTempLight.Visible = true;
WashTempLight.Image = Properties.Resources._60degrees;
SpeedGroupBox.Enabled = true;
}
}
You can bind all radioButton's event to the same handler and use sender parameter to get the control that the action is for.
private void Temps_CheckedChanged(object sender, EventArgs e)
{
string checkedName = ((RadioButton)sender).Name;
if(checkedName == "Temp40")
{
...
}
else if(checkedName == "Temp60")
{
...
}
}
You can add event handler for all RadioBUttons's like that after InitializeComponent():
var radioButtons =this.Controls.OfType<RadioButton>();
foreach (RadioButton item in radioButtons)
{
item.CheckedChanged += Temps_CheckedChanged;
}
I have got problem with check-box to enable corresponding control next to it. My Requirement is at the page load we want to disable the all textboxes and dropdownlists by using checkbox
if the check-box is checked the control next to that check-box will be enabled for that i have done like this....
at page load
i have written like this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
......
.....
}
and then in checkbox check changed event i have written like this
protected void ChckOrdType_CheckChanged(object sender, EventArgs e)
{
if (ChckOrdType.Checked)
ddlOrdType.Enabled = true;
else
ddlOrdType.Enabled = false;
}
protected void chkPlntPric_CheckChanged(object sender, EventArgs e)
{
if (ChkPlntPric.Checked)
ddlPlntPric.Enabled = true;
else
ddlPlntPric.Enabled = false;
}
protected void chkExcluBro_CheckChanged(object sender, EventArgs e)
{
if (ChkExcluBro.Checked)
ddlExcluBroker.Enabled = true;
else
ddlExcluBroker.Enabled = false;
}
but results is like this ...
I am getting checkbox not checked and control next to it is enabled...But this not what i want
My results is if the check-box is not checked the control next to it is disabled
would any one pls help on this....
Thanks In advance......
This is because you have just wrote ONLY to uncheck the checkboxes in the pageload and not to disable the controls behind the checkbox; If thats needed, then your snippet in the pageload should be:
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
......
.....
ddlOrdType.Enabled = false;
ddlPlntPric.Enabled = false;
ddlExcluBroker.Enabled = false;
.........
}
or
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
......
.....
ChckOrdType_CheckChanged(sender,e);
chkPlntPric_CheckChanged(sender,e);
chkExcluBro_CheckChanged(sender,e);
...
}
Disable text boxes in page load like below.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ChckOrdType.Checked = false;
ChkPlntPric.Checked = false;
ChkExcluBro.Checked = false;
DisableFirstTime();
......
.....
}
private void DisableFirstTime()
{
ddlOrdType.Enabled = false;
ddlPlntPric.Enabled = false;
ddlExcluBroker.Enabled = false;
}
I have a ListView with various items and a ItemCheck handler as below:
private void ListView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == System.Windows.Forms.CheckState.Checked)
{
if (e.Index == 0)
{
ListView1.Items[1].Checked = false;
ListView1.Items[2].Checked = false;
ListView1.Items[3].Checked = false;
ListView1.Items[4].Checked = false;
ListView1.Items[5].Checked = false;
ListView1.Items[6].Checked = false;
ListView1.Items[7].Checked = false;
}
else
{
ListView1.Items[0].Checked = false;
}
}
}
Essentially the first item is "none", so when it is checked all the other items become unchecked (and vice-versa). Occasionally the program checks items in the code and I think this is causing problems. I know TreeViewEventArgs has a field called Action which is equal to TreeViewAction.Unkownif the call is coming from the program and not from the user.
Is there a way to check if a ListViewItem is being checked by a user as opposed to being checked by code?
There's no way to tell from the event arguments so you would have code for it yourself, something like
private bool raisedFromCode;
private void button2_Click(object sender, EventArgs e)
{
raisedFromCode = true;
listView1.Items[1].Checked = !listView1.Items[1].Checked;
raisedFromCode = false;
}
private void listView1_ItemCheck(object sender, ItemCheckEventArgs args)
{
if (!raisedFromCode)
MessageBox.Show("User checked");
}
Alternatively if you just don't want your logic to fire when you change the check state through code you could unsubscribe from the event handler
listView1.ItemCheck -= new ItemCheckEventHandler(this.listView1_ItemCheck);
listView1.Items[1].Checked = false;
listView1.ItemCheck += new ItemCheckEventHandler(this.listView1_ItemCheck);
Im not very familiar with refactoring but I feel like I need to do some changes in this code...
I have 4 buttons in my app. Each one calls an method that receives data from an online xml and use this data to populate a listbox.
I kept myself repeating code for all those methods, although I see they differ sometimes.
Here is a little explanation of my logic:
The buttons btnA_Click and btnB_Click get the same data, but from differente years.
The same goes for the other 2 buttons. They get the same data(not the same as btnA
and btnB) but from different years too.
Buttons:
private void btnA_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the web";
table.OpenReadAsync(new Uri(webService));
btnA.IsEnabled = false;
btnB.IsEnabled = false;
progressBar1.Visibility = System.Windows.Visibility.Visible;
}
private void btnB_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the net";
table.OpenReadAsync(new Uri(webService));
btnA.IsEnabled = false;
btnB.IsEnabled = false;
progressBar1.Visibility = System.Windows.Visibility.Visible;
}
private void btnSTA_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the web;
stats.OpenReadAsync(new Uri(webService));
btnSTA.IsEnabled = false;
btnSTB.IsEnabled = false;
progressBar3.Visibility = System.Windows.Visibility.Visible;
}
private void btnSTB_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the web;
stats.OpenReadAsync(new Uri(webService));
btnSTA.IsEnabled = false;
btnSTB.IsEnabled = false;
progressBar3.Visibility = System.Windows.Visibility.Visible;
}
Methods:
void table_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
XElement xml = XElement.Load(e.Result);
var table = LINQ Statement...
listBox1.ItemsSource = table;
btnClassificacaoSerieA.IsEnabled = true;
btnClassificacaoSerieB.IsEnabled = true;
progressBar1.Visibility = System.Windows.Visibility.Collapsed;
}
}
void stats_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
XElement xml = XElement.Load(e.Result);
var stats = LINQ Statement...
listBox3.ItemsSource = stats;
btnSTA.IsEnabled = true;
btnSTB.IsEnabled = true;
progressBar3.Visibility = System.Windows.Visibility.Collapsed;
}
}
Im a little lost here. What could I do to make this code more Object Oriented?
Thx!
Your click events seem to mostly be the same... Make a sub that does that stuff and call the sub from the click event. See if you can put any differences in parameters.
Also your code for hiding / showing the progress bar and enable / disable of buttons is pretty much the same. stuff that in to a sub with a param that lets you say if you want to hide or show.
I have a pretty interesting dilemma that is giving me a hurricane of a headache. I've seen a similar question asked here, but the user posted no code, so it was unresolved. This is a asp.net, sql server, and c# app.
In my application, I use JavaScript to display dummy data in a TextBox to entertain the user while a long process is running. (please note, this is a project, not a professional app).
The problem is, once the app is done executing, the app (I believe) refreshes the page and clears the TextBox. I want to prevent this from happening and continue to display the text after the program is completed.
My question is, where in the following code is the page being refreshed? How can I re-code it to prevent the text from being cleared?
I know there is no issue with the JavaScript or the .aspx page. I have not set or programmed any property to clear the text. If anyone could shed light on the issue, I would greatly appreciate it. If you need more code from me please let me know. I will be very active on this page until it is resolved. Thanks again!
public partial class SendOrders : System.Web.UI.Page
{
protected enum EDIType
{
Notes,
Details
}
protected static string NextBatchNum = "1";
protected static string FileNamePrefix = "";
protected static string OverBatchLimitStr = "Batch file limit has been reached. No more batches can be processed today.";
protected void Page_Load(object sender, EventArgs e)
{
Initialize();
}
protected void Page_PreRender(object sender, EventArgs e)
{ }
protected void btnExit_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.GetCurrentProcess().Kill();
}
protected void Button_Click(object sender, EventArgs e)
{
PutFTPButton.Enabled = false;
Thread.Sleep(3000);
Button btn = (Button)sender;
KaplanFTP.BatchFiles bf = new KaplanFTP.BatchFiles();
KaplanFTP.Transmit transmit = new KaplanFTP.Transmit();
if (btn.ID == PutFTPButton.ID)
{
DirectoryInfo dir = new DirectoryInfo(#"C:\Kaplan");
FileInfo[] BatchFiles = bf.GetBatchFiles(dir);
bool result = transmit.UploadBatchFilesToFTP(BatchFiles);
if (!result)
{
ErrorLabel.Text += KaplanFTP.errorMsg;
return;
}
bf.InsertBatchDataIntoDatabase("CTL");
bf.InsertBatchDataIntoDatabase("HDR");
bf.InsertBatchDataIntoDatabase("DET");
bf.InsertBatchDataIntoDatabase("NTS");
List<FileInfo> allfiles = BatchFiles.ToList<FileInfo>();
allfiles.AddRange(dir.GetFiles("*.txt"));
bf.MoveFiles(allfiles);
foreach (string order in bf.OrdersSent)
{
OrdersSentDiv.Controls.Add(new LiteralControl(order + "<br />"));
}
btnExit.Visible = true;
OrdersSentDiv.Visible = true;
OrdersInfoDiv.Visible = false;
SuccessLabel.Visible = true;
NoBatchesToProcessLbl.Visible = true;
BatchesToProcessLbl.Visible = false;
PutFTPButton.Enabled = false;
BatchesCreatedLbl.Text = int.Parse(NextBatchNum).ToString();
Thread.Sleep(20000);
if (KaplanFTP.errorMsg.Length != 0)
{
ErrorLabel.Visible = false;
SuccessLabel.Visible = true;
ErrorLabel.Text = KaplanFTP.errorMsg;
}
}
}
private void Initialize()
{
KaplanFTP.BatchFiles bf = new KaplanFTP.BatchFiles();
if (!IsPostBack)
{
FileNamePrefix = bf.FileNamePrefix;
NextBatchNum = bf.NextBatchNum;
BatchesCreatedLbl.Text = (int.Parse(NextBatchNum) - 1).ToString();
if (bf.CheckLocalForNewBatch() == true)
{
NoBatchesToProcessLbl.Visible = false;
BatchesToProcessLbl.Visible = true;
if (int.Parse(NextBatchNum) >= 50)
{
ErrorLabel.Text += ErrorLabel.Text + OverBatchLimitStr;
ErrorLabel.Visible = true;
PutFTPButton.Enabled = false;
}
else
{
bf.ReadyFilesForTransmission();
ErrorLabel.Visible = false;
PutFTPButton.Enabled = true;
List<string[]> detStream = bf.GetBatchStream("DET");
List<string[]> hdrStream = bf.GetBatchStream("HDR");
OrdersInfoDiv.Visible = true;
DataTable dt = new DataTable();
dt.Columns.Add("ORDER NUMBER");
dt.Columns.Add("LINE NUMBER");
dt.Columns.Add("ITEM NUMBER/ISBN");
dt.Columns.Add("DESCRIPTION");
dt.Columns.Add("QUANTITY");
dt.Columns.Add("SHIPPING");
Dictionary<string, string> orderShip = new Dictionary<string, string>();
foreach (string[] hdrItems in hdrStream)
{
orderShip.Add(hdrItems[0], hdrItems[2]);
}
foreach (string[] detItems in detStream)
{
List<string> detLineList = new List<string>(detItems);
detLineList.Add(orderShip[detItems[0]]);
detLineList.RemoveAt(13);
detLineList.RemoveAt(12);
detLineList.RemoveAt(11);
detLineList.RemoveAt(10);
detLineList.RemoveAt(9);
detLineList.RemoveAt(8);
detLineList.RemoveAt(7);
detLineList.RemoveAt(4);
detLineList.RemoveAt(2);
detLineList[1] = detLineList[1].TrimStart('0');
detLineList[4] = detLineList[4].TrimStart('0');
dt.Rows.Add(detLineList.ToArray());
}
BatchDetails.DataSource = dt;
BatchDetails.DataBind();
}
}
else
{
NoBatchesToProcessLbl.Visible = true;
BatchesToProcessLbl.Visible = false;
PutFTPButton.Enabled = false;
}
}
}
}
Yes. You will have to determine the state of your calculation and populate the control inside Page_Load in the case where IsPostBack is true:
protected void Page_Load(object sender, EventArgs e) {
if (IsPostBack) {
// re-populate javascript-fill UI
} else {
}
Initialize();
}
You can probably also move Initialize() into the else clause as well.
Since you are handling the button click server side (I'm assuming the problem is happening once the user clicks the button?) there has to be a postback to handle it.
You could try putting your button and label into an updatepanel control - it uses AJAX to refresh its contents.
See this page for more information on updatepanels.