Dispose control - c#

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.

Related

Checkbox and radio doesn't work as expected when checking

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);

How can I search Notes by sequence number

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
}
}
}

Setting Excel worksheet protection with EPPlus

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.

How to insert ContentContol in footnote with specific range word.interop

How to go add content control in specific start and end range of footnote? I can add content control in document.range, but I am unable to add in footnote, please help to do this. If anyone reply quickly, I will be proved of you.
public void FindItalicFootnote(String FindText)
{
foreach (Word.Footnote footNote in Word.Document.Footnotes)
{
Word.Range RngFind = footNote.Range;
RngFind.Find.Forward = true;
if (RngFind.Find.Execute(FindText))
{
while (RngFind.Find.Found)
{
RngFind.Select();
object strtRange = Word.Selection.Range.Start;
object endRange = Word.Selection.Range.End;
string placeHolder = "";
bool findCase = false;
if (Word.Selection.Range.ParentContentControl == null && Word.Selection.Range.ContentControls.Count == 0)
{
RngFind.Select();
while (Word.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Font.Italic == -1)
{
Word.Selection.Previous(Unit: Word.WdUnits.wdWord, Count: 1).Select();
strtRange = Word.Selection.Range.Start;
placeHolder = "{VerifiedBy='Italic'}";
findCase = true;
}
Word.ContentControl CC = RngFind.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText,
footNote.range(strtRange, endRange));
//my query is, how to say footNote.range(start, end), in main part I wrote as Word.Document.range(startRange, endRange)
CC.Title = "Case Reference";
CC.Tag = Guid.NewGuid().ToString();
CC.SetPlaceholderText(Text: placeHolder);
}
RngFind.Find.Execute(FindText);
}
}
}
}
Regards,
Saran

ApplicationData.LocalSettings not storing data?

I'm making an app where a use enters values for two times (starthour, startminute, endhour, endminute). I wrote a function that saves the values and then checks for value and puts the values inside the text boxes. However, it isn't working and I'm not sure why. I'm assuming its a mistake on my part, but I'm not exactly sure. Here's the code:
public async Task savedata()
{
while (true)
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["starthour1"] = starthour1.Text;
localSettings.Values["starthour2"] = starthour2.Text;
localSettings.Values["starthour3"] = starthour3.Text;
localSettings.Values["starthour4"] = starthour4.Text;
localSettings.Values["starthour5"] = starthour5.Text;
localSettings.Values["starthour6"] = starthour6.Text;
localSettings.Values["starthour7"] = starthour7.Text;
localSettings.Values["startminute1"] = startminute1.Text;
localSettings.Values["startminute2"] = startminute2.Text;
localSettings.Values["startminute3"] = startminute3.Text;
localSettings.Values["startminute4"] = startminute4.Text;
localSettings.Values["startminute5"] = startminute5.Text;
localSettings.Values["startminute6"] = startminute6.Text;
localSettings.Values["startminute7"] = startminute7.Text;
localSettings.Values["endhour1"] = endhour1.Text;
localSettings.Values["endhour2"] = endhour2.Text;
localSettings.Values["endhour3"] = endhour3.Text;
localSettings.Values["endhour4"] = endhour4.Text;
localSettings.Values["endhour5"] = endhour5.Text;
localSettings.Values["endhour6"] = endhour6.Text;
localSettings.Values["endhour7"] = endhour7.Text;
localSettings.Values["endminute1"] = endminute1.Text;
localSettings.Values["endminute2"] = endminute2.Text;
localSettings.Values["endminute3"] = endminute3.Text;
localSettings.Values["endminute4"] = endminute4.Text;
localSettings.Values["endminute5"] = endminute5.Text;
localSettings.Values["endminute6"] = endminute6.Text;
localSettings.Values["endminute7"] = endminute7.Text;
//get data
Object starthour1o = localSettings.Values["starthour1"];
if (starthour1o == null)
{
// No data
}
else
{
starthour1.Text = starthour1o.ToString();
}
Object starthour2o = localSettings.Values["starthour2"];
if (starthour2o == null)
{
// No data
}
else
{
starthour2.Text = starthour2o.ToString();
}
Object starthour3o = localSettings.Values["starthour3"];
if (starthour3o == null)
{
// No data
}
else
{
starthour3.Text = starthour3o.ToString();
}
Object starthour4o = localSettings.Values["starthour4"];
if (starthour4o == null)
{
// No data
}
else
{
starthour4.Text = starthour4o.ToString();
}
Object starthour5o = localSettings.Values["starthour5"];
if (starthour5o == null)
{
// No data
}
else
{
starthour5.Text = starthour5o.ToString();
}
Object starthour6o = localSettings.Values["starthour6"];
if (starthour6o == null)
{
// No data
}
else
{
starthour6.Text = starthour6o.ToString();
}
Object starthour7o = localSettings.Values["starthour7"];
if (starthour7o == null)
{
// No data
}
else
{
starthour7.Text = starthour7o.ToString();
}
await Task.Delay(10);
}
}
Two things you need to do, first you need to explicitly save your settings for them to be persisted by calling Save(). Somewhere in your code you need to do localSettings.Save() and it should work.
2nd, if you have saved settings the first thing your code does is overwrite them with the current values of the text boxes, the whole top section where it is localSettings.Values["Foo"] = Foo.Text needs to be moved to the bottom.
As a side comment, do you really need to be updating your code every 10 miliseconds? That is going to eat up a TON of resources in your program. A much more normal approach is load the values at start-up then save them at shutdown.

Categories

Resources