I have application where i am trying to make checkbox checked based on value i get from string. String name is called aktivan and returns values Da or Ne, i checked with messagebox and values are here and valid. If value are Da it need to check checkbox but it doesn't work.
chkAktivan.Checked = aktivan == "Da" ? true : false; // doesn't work
chkAktivan.Checked = true; // working
chkAktivan.Checked = false; // working
Same is for radio, based on string values Muški or Ženski it need to set values but also does't working all time its checking Ženski radio.
if (spol == "Muški")
{
radioMuski.Checked = true;
radioZenski.Checked = false;
}
else
{
radioMuski.Checked = false;
radioZenski.Checked = true;
}
You should trim the values before using them, like this.
if (spol.Trim() == "Muški")
{
radioMuski.Checked = true;
radioZenski.Checked = false;
}
else
{
radioMuski.Checked = false;
radioZenski.Checked = true;
}
And also this.
chkAktivan.Checked = aktivan == "Da" ? true : false;
To this:
chkAktivan.Checked = aktivan.Trim() == "Da" ? true : false;
partial answer
For the first part of your question, regarding this line:
chkAktivan.Checked = aktivan == "Da" ? true : false;
Try changing it to:
chkAktivan.Checked = (aktivan == "Da" ? true : false);
OR MORE SIMPLY:
chkAktivan.Checked = "Da".Equals(aktivan);
Related
Design:
If i don't have any land component price/flight prices to be added, i want the system to dispose the unused controls and move up to fill in the empty spaces, but somehow or rather, it became like this.
Result:
if (bf.landComponent != 0.00m)
{
lblLandComponent.Text = "" + bf.landComponent;
lblLandComponent.Visible = true;
lblLandComponentL.Visible = true;
}
else
{
lblLandComponent.Dispose();
lblLandComponentL.Dispose();
}
if (bf.flightComponent != 0.00m)
{
lblFlightComponent.Text = "" + bf.flightComponent;
lblFlightComponent.Visible = true;
lblFlightComponentL.Visible = true;
}
if (bf.unitSales != 0.00m)
{
lblUnitSales.Text = "" + bf.unitSales;
lblUnitSales.Visible = true;
lblUnitSalesL.Visible = true;
}
if(bf.rentalCar!=0.00m)
{
lblCT.Visible = true;
lblRentalCar_L.Visible = true;
lblRentalCar.Visible = true;
lblRentalCar.Text = "" + bf.rentalCar;
}
if (bf.discount != 0.00m)
{
lblDiscount.Text = "" + bf.discount;
lblDiscountL.Visible = true;
lblDiscount.Visible = true;
}
if(bf.packageInclusion!="")
{
lblPackageInclusion.Text = bf.packageInclusion;
lblPackageInclusion.Visible = true;
lblPackageInclusionL.Visible = true;
}
//BF is a class object stored in a session that i put from the previous page.
Is there something wrong?
The Dispose method is not for the thing you want. Check in the docs to see what it really does.
When you want to hide a control, try setting their Visibility to false, or try removing them from their parent container control.
Conditionally setting their Visibility to true is not enough. In 99% of cases they would be visible anyways, since that's the default value for visibility. You must ensure to either set their initial Visibility to false, or add else clause that will do that.
Here i have used if , else if condition to show an error message and make some label visible and invisible, but i am trying to use ternary operator to do so but i am quite unfamiliar with ternery operator and unable to use it for all condition i have in my if else code.
So any help with my code will be highly appreciated. Thank you.
Below is my code
catch (Exception ex)
{
if (ex.Message == "Parent Menu Title Required")
{
metroLabel4.Visible = true;
metroLabel5.Visible = false;
metroLabel6.Visible = false;
metroLabel4.Text = ex.Message;
}
else if (ex.Message == "Menu Title Required")
{
metroLabel4.Visible = false;
metroLabel5.Visible = true;
metroLabel6.Visible = false;
metroLabel5.Text = ex.Message;
}
else if (ex.Message == "Form Name Required")
{
metroLabel4.Visible = false;
metroLabel5.Visible = false;
metroLabel6.Visible = true;
metroLabel6.Text = ex.Message;
}
else
{
metroLabel4.Visible = true;
metroLabel5.Visible = true;
metroLabel6.Visible = true;
metroLabel4.Text = "Parent Menu Title Required";
metroLabel5.Text = "Menu Title Required";
metroLabel6.Text = "Form Name Required";
}
}
The ternary operator is not a good fit for your problem. It is used to set the value of one variable to one of two values, based on a predicate:
var thingToSet = predicateA ?
ifPredicateAIsTrue :
ifPredicateAIsFalse;
This is the same as:
if (predicateA)
thingToSet = ifPredicateAIsTrue;
else
thingToSet = ifPredicateAIsFalse;
To nest ternary expressions, place a new ternary expression in the value to set:
var otherThingToSet = predicateB ? (
predicateC ?
ifPredicateCIsTrue :
ifPredicateCIsFalse
) : (
predicateD ?
ifPredicateDIsTrue :
ifPredicateDIsFalse
);
This is equivalent to:
if (predicateB)
{
if (predicateC)
otherThingToSet = ifPredicateCIsTrue;
else
otherThingToSet = ifPredicateCIsFalse;
}
else
{
if (predicateD)
otherThingToSet = ifPredicateDIsTrue;
else
otherThingToSet = ifPredicateDIsFalse;
}
As you can see, this is not really a good fit for your problem, as you're trying to set the value of several variables, based on the exception message.
A better fit for your problem would be a switch statement:
switch (ex.Message)
{
case "Parent Menu Title Required":
metroLabel4.Visible = true;
metroLabel5.Visible = false;
metroLabel6.Visible = false;
metroLabel4.Text = ex.Message;
break;
case "Menu Title Required":
metroLabel4.Visible = false;
metroLabel5.Visible = true;
metroLabel6.Visible = false;
metroLabel5.Text = ex.Message;
break;
case "Form Name Required":
metroLabel4.Visible = false;
metroLabel5.Visible = false;
metroLabel6.Visible = true;
metroLabel6.Text = ex.Message;
break;
default:
metroLabel4.Visible = true;
metroLabel5.Visible = true;
metroLabel6.Visible = true;
metroLabel4.Text = "Parent Menu Title Required";
metroLabel5.Text = "Menu Title Required";
metroLabel6.Text = "Form Name Required";
break;
}
Your code is equivalent to:
const string ParMnuTitReq ="Parent Menu Title Required";
const string MnuTitReq ="Menu Title Required";
const string FrmNamReq ="Form Name Required";
string m = ex.Message;
metroLabel4.Visible = m != MnuTitReq && m != FrmNamReq;
metroLabel5.Visible = m != ParMnuTitReq && m != FrmNamReq;
metroLabel6.Visible = m != ParMnuTitReq && m != MnuTitReq;
// This can be done in the form designer
metroLabel4.Text = ParMnuTitReq;
metroLabel5.Text = MnuTitReq;
metroLabel6.Text = FrmNamReq;
You don't need ternary expressions. Instead, you can combine logical expressions. In the case of the Visible property which is of type bool, you can directly assign the result of the logical expression.
You can always assign the same text to the labels, as they won't be visible if the text does not apply. You could even drop the 3 last code lines and instead assign the text in the form designer. This reduces your original 23 lines of code (not counting lines with braces only) to 7.
Nested or chained ternary expressions can be used if you must be able to assign more than 2 different values.
string t = x == 1 ? "case 1" : x == 2 ? "case 2" : x == 3 ? "case 3" : "other case";
Is equivalent to
string t;
if (x == 1) {
t = "case 1";
} else if (x == 2) {
t = "case 2";
} else if (x == 3) {
t = "case 3";
} else {
t = "other case";
}
I use Evernote C# API. I understand how filters working.
ENNoteStoreClient store = ENSessionAdvanced.SharedSession.PrimaryNoteStore;
SyncState currentState = store.GetSyncState();
int currentUpdateCount = currentState.UpdateCount;
if (currentUpdateCount > latestUpdateCount)
{
latestUpdateCount = currentUpdateCount;
// Here synchronization code
}
I have latestUpdateCount and how I can get notes with sequence number >= this number?
You'll want to use GetFilteredSyncChunk using code something like the following:
List<SyncChunk> syncBlocks = new List<SyncChunk>();
int maxEntries = 250;
// These are sample filter settings; you should use what's appropriate for you based on
// what data you want returned in your note objects.
SyncChunkFilter filter = new SyncChunkFilter();
filter.IncludeNotes = true;
filter.IncludeNoteAttributes = true;
filter.IncludeNotebooks = true;
filter.IncludeTags = false;
filter.IncludeSearches = false;
filter.IncludeResources = false;
filter.IncludeLinkedNotebooks = false;
filter.IncludeExpunged = false;
filter.IncludeNoteApplicationDataFullMap = false;
filter.IncludeResourceApplicationDataFullMap = false;
filter.IncludeNoteResourceApplicationDataFullMap = false;
do
{
SyncChunk chunk = store.getFilteredSyncChunk(currentUpdateCount, maxEntries, filter);
if (chunk == null)
{
return null; // This can happen if there is a "503 - Service unavailable" error accessing this person's Evernote info
}
syncBlocks.Add(chunk);
currentUpdateCount = chunk.ChunkHighUSN;
} while (currentUpdateCount < serverSyncState.UpdateCount);
foreach (SyncChunk chunk in syncBlocks)
{
if (chunk != null && chunk.Notes != null)
{
foreach (Note chunkNote in chunk.Notes)
{
// do something with the retrieved chunkNote object
}
}
}
I'm trying to set worksheet permissions for an XLSM file using EPPlus but it seems I can only set the default protection level, individual protections are not being set. For the record, I'm trying to accomplish programmatically method 1 in this article. Here's the code I'm using:
using (var p = new ExcelPackage("output.xlsm"))
{
var ws = p.Workbook.Worksheets["MySheet"];
// Set some cell values here
// Filtering, sorting, protection
ws.Cells[7, 1, 10, 5].AutoFilter = true;
ws.View.FreezePanes(7, 1);
ws.ProtectedRanges.Add("FilteredCells", new ExcelAddress(7, 1, 10, 5));
// Worksheet protection
ws.Protection.AllowAutoFilter = true;
ws.Protection.AllowDeleteColumns = false;
ws.Protection.AllowDeleteRows = false;
ws.Protection.AllowEditObject = false;
ws.Protection.AllowEditScenarios = false;
ws.Protection.AllowFormatCells = false;
ws.Protection.AllowFormatColumns = false;
ws.Protection.AllowFormatRows = false;
ws.Protection.AllowInsertColumns = false;
ws.Protection.AllowInsertHyperlinks = false;
ws.Protection.AllowInsertRows = false;
ws.Protection.AllowPivotTables = false;
ws.Protection.AllowSelectLockedCells = false;
ws.Protection.AllowSelectUnlockedCells = true;
ws.Protection.AllowSort = true;
ws.Protection.IsProtected = true;
ws.Protection.SetPassword("hunter2");
p.SaveAs(new FileInfo("output.xlsm"));
}
This runs without errors, but when I open the file in Excel, or load it back into EPPlus, I find that different protection options have been applied:
AllowAutoFilter = false
AllowDeleteColumns = false
AllowDeleteRows = false
AllowEditObject = true
AllowEditScenarios = true
AllowFormatCells = false
AllowFormatColumns = false
AllowFormatRows = false
AllowInsertColumns = false
AllowInsertHyperlinks = false
AllowInsertRows = false
AllowPivotTables = false
AllowSelectLockedCells = true
AllowSelectUnlockedCells = true
AllowSort = false
IsProtected = true
These obviously aren't the permissions I set before, so how can I make sure they are set correctly? Everything else is saved correctly.
Here is the source:
https://github.com/pruiz/EPPlus/blob/master/EPPlus/ExcelSheetProtection.cs
Setting the IsProtected property is overwriting your choices:
public bool IsProtected
{
get
{
return GetXmlNodeBool(_isProtectedPath, false);
}
set
{
SetXmlNodeBool(_isProtectedPath, value, false);
if (value)
{
AllowEditObject = true;
AllowEditScenarios = true;
}
else
{
DeleteAllNode(_isProtectedPath); //delete the whole sheetprotection node
}
}
}
Move your IsProtected = true call to the start of the code or handle however you want, but you are accidently overriding your previous choice. I would look at the properties at that link to see which ones are going to override your existing selections.
aspx.cs
string aa= dt.Rows[0]["fruit"].ToString();
if (aa== "apple")
{
RadioApple.Checked = true;
RadioLemon.Checked = false;
}
else
{
RadioLemon.Checked = true;
RadioApple.Checked = false;
}
I would like to make it in short way like this:
string aa= dt.Rows[0]["fruit"].ToString();
RadioButton rb= RadioGroupFruit.Items.Find(aa);
rb.Checked = true;
Any ideas?
using ext.net in asp.net
For RadioButtonGroups & CheckBoxGroups try getting the value of the BoxLabel.
// pseudo code
string[] selectedFruit = this.CheckboxGroupFruity.items.where(q => q.Checked).select(q => q.BoxLabel).ToArray();