UWP - Display Message Dialog 1 time - c#

I have the code below:
if (jumlahiddb < jumlahbuku)
{
DownloadBukuKomik(url);
string KomikUpdate = #"INSERT INTO books (id,title,folder_id,identifier) SELECT " + intID + ",'" + namaFile + ".pdf',67,'" + namaFile +
".pdf' WHERE not exists (select id AND title AND folder_id AND identifier FROM books WHERE id=" + intID + " and title='" + namaFile +
".pdf' AND folder_id=67 and identifier='" + namaFile + ".pdf')";
Debug.WriteLine(KomikUpdate.ToString());
var komikQuery = objConnUpdate.Prepare(KomikUpdate);
komikQuery.Step();
}
else
{
bool shown = false;
if (!shown)
{
MessageDialog messageDialog1 = new MessageDialog("Jumlah komik bertambah sebanyak " + jumlahbuku + " komik pada menu Komik Pendidikan", "Update Berhasil");
messageDialog1.Commands.Add(new UICommand("OK", (command) =>
{
DownloadBukuVideo.IsOpen = false;
Downloading.IsOpen = false;
ukomikBtn.Visibility = Visibility.Visible;
downloadKomikBtn.Visibility = Visibility.Collapsed;
ukomikText.Visibility = Visibility.Collapsed;
ukomikText.Text = "";
shown = true;
}));
await messageDialog1.ShowAsync();
}
I have a problem, that is when I click the OK button, it will display message dialog again. I want message dialog shown only 1 time. How to solve it?

The problem is that you are declaring the shown variable in a local scope, so it is being initialised and set every time your show message box code is run.
To avoid this, declare it at a higher level - for instance, at class level. For example, based on the code you shared in the comments:
class myClass {
private bool _shown;
public async void KomikMsgDialog()
{
if (!_shown) // If we haven't shown the dialog yet
{
MessageDialog messageDialog1 = new MessageDialog("Jumlah komik bertambah sebanyak " + jumlahbuku + " komik pada menu Komik Pendidikan", "Update Berhasil");
messageDialog1.Commands.Add(new UICommand("OK", (command) =>
{
DownloadBukuVideo.IsOpen = false;
Downloading.IsOpen = false;
ukomikBtn.Visibility = Visibility.Visible;
downloadKomikBtn.Visibility = Visibility.Collapsed;
ukomikText.Visibility = Visibility.Collapsed;
ukomikText.Text = "";
}));
await messageDialog1.ShowAsync();
_shown = true; // Flag the dialog as having been shown
}
}
}
This way, the first time you call the method it will check whether the the dialog has been shown, which it won't have been, so it will show the dialog and flag it as having been shown. The next time it will check the flag and not show the dialog.

Related

Is there a way to change the color of a specific word/string at run time of a WPF ListBox Item?

So all i want is if a specific word will be added in a listbox row/item, at run time, cause i am using a timer to add items at run time from my Database, i want that specific word/string to have a diferent color.
ie: All new item added that contains the string/word "Aproved", should be colored as green as soon as a new item its added to the WPF ListBox at run time.
private void dispatcherTimerMensagem_Tick(object sender, EventArgs e)
{
if (!(principalDB.testarConexãoDB()))
{
dispatcherTimerVendasFechadas.Stop();
dispatcherTimerMensagem.Stop();
LstMensagem.ItemsSource = null;
LbPbVendasFechadas.ItemsSource = null;
}
else
{
mensagem = principalDB.selectMessagemUsuario(null);
if (mensagem != string.Empty)
{
this.Activate();
LstMensagem.Opacity = 1;
LstMensagem.Items.Add(principalDB.mensagemRemetente + " (" + principalDB.mensagemData + ")" + ": " + mensagem);
voice.Voice = voice.GetVoices().Item(0);
myWorkerMensagem.WorkerReportsProgress = true;
myWorkerMensagem.WorkerSupportsCancellation = true;
myWorkerMensagem.RunWorkerAsync();
if (VisualTreeHelper.GetChildrenCount(LstMensagem) > 0)
{
Border border = (Border)VisualTreeHelper.GetChild(LstMensagem, 0);
ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
scrollViewer.ScrollToBottom();
}
}
else
{
LstMensagem.Opacity = 0.5;
}
}
}
So the LstMensagem will recieve a new item at run time, from the variables declared, in this line of code:
LstMensagem.Items.Add(principalDB.mensagemRemetente + " (" + principalDB.mensagemData + ")" + ": " + mensagem);
If a specific word/string comes up, as ie "aproved" i want that string with a different text color,as example, brushed in green.
Use a TextBlock instead string. For entire item:
var text = principalDB.mensagemRemetente + " (" + principalDB.mensagemData + ")" + ": " + mensagem;
var tb = new TextBlock();
tb.Text = text;
if(text.Contains("aproved"))
tb.Foreground = Brushes.Green;
LstMensagem.Items.Add(tb);
For only part of the item, use the Inlines property to add different formatted texts:
var tb = new TextBlock();
tb.Inlines.Add(new Run { Foreground = Brushes.Green, Text =
principalDB.mensagemRemetente});
tb.Inlines.Add(" (" + principalDB.mensagemData + ")" + ": " + mensagem);
LstMensagem.Items.Add(tb);

C# WCF Multiple clients receive all the same data

my problem is that i have a WCF Service and multip
le clients can connect/subscribe to that service and get all Random Data
trough Callback with the service...a Timer starts when a clients subscribed to the service, the thing is that every client then gets all not the same data but thats what i want to achieve...what am i doing wrong?
This is what happens when one client subscribed to the service:
public SubscribeResult Subscribe()
{
SubscribeResult retVal = new SubscribeResult();
try
{
retVal.ClientID = Guid.NewGuid().ToString();
clientID = retVal.ClientID;
ServiceCallback = OperationContext.Current.GetCallbackChannel<ITraceCallback>();
ti = new Timer();
ti.Elapsed += Ti_Elapsed;
ti.Interval = 1000;
ti.Enabled = true;
ti.Start();
retVal.Success = true;
SubscribeClientList.Add(new SubscribeItem {CLIENTID = clientID, SENT = true });
connectedClients += 1;
}
catch (Exception ex)
{
retVal.Success = false;
Console.WriteLine("Something went horribly wrong! \n" + ex.Message);
}
return retVal;
}
And this is what happens when the timer hits zero :
private void Ti_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
if (isSending == false)
{
isSending = true;
// generate message
CreateRandomString crs = new CreateRandomString();
string msg = crs.CreateString(rnd.Next(15, 25));
// increase entry
entry += 1;
// create timestamp of creation
DateTime creation = DateTime.Now;
// generate level
int muchwow = rnd.Next(1, 4);
string lvl = "undefined";
switch (muchwow)
{
case 1:
lvl = "Error";
break;
case 2:
lvl = "Warning";
break;
case 3:
lvl = "Information";
break;
}
bool sucess = ServiceCallback.Message(entry, creation, lvl, msg);
if (sucess == true)
{
CreateRandomString messagePreview = new CreateRandomString();
string prev = messagePreview.TruncString(msg, 20);
Console.WriteLine("[" + DateTime.Now + "] : [" + clientID + "] : " + "'" + prev + "' : " + "(" + lvl + ")");
sucess = false;
}
isSending = false;
}

Windows store app crashes when no internet

I am creating my first windows store app and i have several web service calls during startup and also periodically through the app. However I have noticed that my app will ot start/crashes or just closes down when I don't have internet access because of the web serivice calls. I want my app to start up in normal way with some initial data and seem normal even when there is no internet access. The data I get from webservice are mostly weather data that I show in various textboxes and graphs.
The code below shows the webservice calls in my extended splash screen.
public sealed partial class ExtendedSplashScreen : Page
{
//parameterItem max1DayAgo = new parameterItem();
//parameterItem min1DayAgo = new parameterItem();
public ExtendedSplashScreen()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
string[] periodSelector = { "1DayAgo", "1WeekAgo", "1MonthAgo" };
string[] modeSelector = { "max", "min" };
string[] parameterSelector = { "umtTemp1", "umtWindSpeed", "umtAdjBaromPress", "umtRainRate" };
//Create a webservice object
ServiceReference.WebServiceSoapClient webServiceObj = new ServiceReference.WebServiceSoapClient();
//First we create an object that holds max data for yesterday
var getMax1DayAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[0], modeSelector[0]);
//create an object that holds min data for yesterday
var getMin1DayAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[0], modeSelector[1]);
//Save arrayOfValue and arrayOfUnit to a parameterItem object. these objects are created during startup
// and the can be accessed and updated by all methods in this page later we will see that maxMinButton_Click method
//for the maxMinButton will use these data
//create an object that holds max data for last week
var getMax1WekAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[1], modeSelector[0]);
//create an object that holds min data for last week
var getMin1WekAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[1], modeSelector[1]);
//create an object that holds max data for last month
var getMax1MonthAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[2], modeSelector[0]);
//create an object that holds min data for last month
var getMin1MonthAgoObj = await webServiceObj.GetSelectedMaxMinDataAsync(parameterSelector, periodSelector[2], modeSelector[1]);
(App.Current as App).max1DayAgo.arrayOfValue = getMax1DayAgoObj.arrayOfValue;
(App.Current as App).max1DayAgo.arrayOfUnit = getMax1DayAgoObj.arrayOfUnit;
(App.Current as App).min1DayAgo.arrayOfValue = getMin1DayAgoObj.arrayOfValue;
(App.Current as App).min1DayAgo.arrayOfUnit = getMin1DayAgoObj.arrayOfUnit;
(App.Current as App).max1WeekAgo.arrayOfValue = getMax1WekAgoObj.arrayOfValue;
(App.Current as App).max1WeekAgo.arrayOfUnit = getMax1WekAgoObj.arrayOfUnit;
(App.Current as App).min1WeekAgo.arrayOfValue = getMin1WekAgoObj.arrayOfValue;
(App.Current as App).min1WeekAgo.arrayOfUnit = getMin1WekAgoObj.arrayOfUnit;
(App.Current as App).max1MonthAgo.arrayOfValue = getMax1MonthAgoObj.arrayOfValue;
(App.Current as App).max1MonthAgo.arrayOfUnit = getMax1MonthAgoObj.arrayOfUnit;
(App.Current as App).min1MonthAgo.arrayOfValue = getMin1MonthAgoObj.arrayOfValue;
(App.Current as App).min1MonthAgo.arrayOfUnit = getMin1MonthAgoObj.arrayOfUnit;
string[] startupData = new string[13];
startupData[0] = " " + (App.Current as App).max1DayAgo.arrayOfValue[0] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[0]; // maxTemp
startupData[1] = " " + (App.Current as App).max1DayAgo.arrayOfValue[1] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[1]; // maxWindSped
startupData[2] = " " + (App.Current as App).max1DayAgo.arrayOfValue[2] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[2]; // maxAirPressure
startupData[3] = " " + (App.Current as App).max1DayAgo.arrayOfValue[3] + " " + (App.Current as App).max1DayAgo.arrayOfUnit[3];// maxRainfall
startupData[4] = " " + (App.Current as App).min1DayAgo.arrayOfValue[0] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[0]; // minTemp
startupData[5] = " " + (App.Current as App).min1DayAgo.arrayOfValue[1] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[1];// minWindSped
startupData[6] = " " + (App.Current as App).min1DayAgo.arrayOfValue[2] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[2];// minAirPressure
startupData[7] = " " + (App.Current as App).min1DayAgo.arrayOfValue[3] + " " + (App.Current as App).min1DayAgo.arrayOfUnit[3];// minRainfall
// Main fields
// ServiceReference.WebServiceSoapClient webServiceObj = new ServiceReference.WebServiceSoapClient();
var getLatestTempObj = await webServiceObj.GetLatestDataAsync("umtTemp1");
var getLatestWindObj = await webServiceObj.GetLatestDataAsync("umtWindSpeed");
var getLatestwindDirObj = await webServiceObj.GetLatestDataAsync("umtAdjWinDir");
var getLatestairPressureObj = await webServiceObj.GetLatestDataAsync("umtAdjBaromPress");
startupData[8] = " " + getLatestTempObj.Value + " " + getLatestTempObj.Unit;//temperatureMainTxtBlock.Text
startupData[9] = " " + getLatestWindObj.Value + " " + getLatestWindObj.Unit;//temperatureMainTxtBlock.Text
startupData[10] = "" + getLatestwindDirObj.Value; //temperatureMainTxtBlock.Text
startupData[11] = " " + getLatestairPressureObj.Value + " " + getLatestairPressureObj.Unit;//temperatureMainTxtBlock.Text
startupData[12] = "Last update: " + getLatestwindDirObj.Timestamp;//temperatureMainTxtBlock.Text
//save the startup data to the global variables
(App.Current as App).NavigateData = startupData;
this.Frame.SetNavigationState(e.Parameter as string);
this.Frame.Navigate(typeof(MainPage));
}
}
An approach we have in some of our team based apps is prior to any call to return data of the net, the network status is checked. Example:
async Task RefreshFromWeb(...)
{
if (!App.HasInternetAccess)
{
await new Windows.UI.Popups.MessageDialog(Strings.NoInternetWarning).ShowAsync();
return;
}
//attempt access here
}
public static bool HasInternetAccess
{
get
{
var profile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
if (profile == null)
return false;
return profile.GetNetworkConnectivityLevel() ==
Windows.Networking.Connectivity.NetworkConnectivityLevel.InternetAccess;
}
}
We also took another approach at times which was very similar but uses await and returns true or false (the same could easily be done above, that approach above just gives the dialog)
public static async System.Threading.Tasks.Task HasInternet()
{
var profile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
var hasNetAccess = profile != null;
if (!hasNetAccess)
await new Windows.UI.Popups.MessageDialog(
content: InfoHub.AppHubViewModel.Strings.NoInternetWarning,
title: InfoHub.AppHubViewModel.Strings.NoInternetWarning).ShowAsync();
return hasNetAccess;
}
async void YourControlEvent_Click(object sender, ItemClickEventArgs e)
{
//if net access, do your stuff, otherwise ignore for now
if (await IsInternet())
{
//do net calls here
}
}
You need to implement some exception handling around this line:
ServiceReference.WebServiceSoapClient webServiceObj = new ServiceReference.WebServiceSoapClient();
and implement a fallback that will work in off-line mode, i.e. retrieve data from a cache.
You can use NetworkStatusChanged event in App.xaml.cs and then you can declare one static variable and use it to check whether Internet is available or not. If Internet is available do your desired operation otherwise show error message.
public static bool IsInternetAvailable;
void NetworkInformation_NetworkStatusChanged(object sender)
{
if (NetworkInformation.GetInternetConnectionProfile() != null)
App.IsInternetAvailable = true;
else
App.IsInternetAvailable = false;
}
Always use try catch blocks, when you have probability of exception.

How to read a textbox from a thread other than the one it was created on?

I am fairly new to C# and I have written several functioning programs, but all of them have been single thread applications. This is my first multi-threaded application and I am struggling to resolve this "Cross-thread operation not valid: Control 'cbLogType' accessed from a thread other than the one it was created on" error. My application searches Windows Event viewer for a user defined Event ID in a user defined Event Log Source(cbLogType). I am using a backgroundworker process to do all the work and I am using the worker.reportprogress to update a label, however, I receive the above error when debugging. I have tried several Invoke methods, but none seem to resolve my error. I have also tried removing the combobox and setting the Log Source directly in the code, which works to an extent, but still fails. I have included my code and any help would be greatly appreciated. I suspect that I might not be using the Invoke method correctly. Thanks in advance!
CODE:
private void bgWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
{
if (File.Exists(#"C:\Events.log"))
MessageBox.Show("File 'Events.log' already exists. All new data will be appended to the log file!", "Warning!");
string message = string.Empty;
string eventID = (tbEventID.Text);
string text;
EventLog eLog = new EventLog();
Invoke((MethodInvoker)delegate() { text = cbLogType.Text; });
eLog.Source = (this.cbLogType.Text); // I am receiving the error here
eLog.MachineName = ".";
int EventID = 0;
string strValue = string.Empty;
strValue = tbEventID.Text.Trim();
//string message = string.Empty;
EventID = Convert.ToInt32(strValue); // Convert string to integer
foreach (EventLogEntry entry in eLog.Entries)
{
int entryCount = 1;
if (cbDateFilter.Checked == true)
{
if (entry.TimeWritten > dtPicker1.Value && entry.TimeWritten < dtPicker2.Value)
if (entry.InstanceId == EventID)
message = "Event entry matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
using (StreamWriter writer = new StreamWriter(#"C:\Events.log", true))
writer.WriteLine("EventID: " + entry.InstanceId +
"\r\nDate Created: " + entry.TimeWritten +
"\r\nEntry Type: " + entry.EntryType +
"\r\nMachinename: " + entry.MachineName +
"\r\n" +
"\r\nMessage: " + entry.Message +
"\r\n");
if (entry.InstanceId != EventID)
message = "No event ids matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
}
else
{
if (cbDateFilter.Checked == false)
{
if (entry.InstanceId == EventID)
using (StreamWriter writer = new StreamWriter(#"C:\Events.log", true))
writer.WriteLine("EventID: " + entry.InstanceId +
"\r\nDate Created: " + entry.TimeWritten +
"\r\nEntry Type: " + entry.EntryType +
"\r\nMachinename: " + entry.MachineName +
"\r\n" +
"\r\nMessage: " + entry.Message +
"\r\n");
else if (entry.InstanceId != EventID)
message = "No event ids matching " + (tbEventID.Text) + " was found in " + (cbLogType.Text);
}
bgWorker1.ReportProgress((entryCount) * 10, message);
entryCount++;
}
}
}
}
private void bgWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblStat.Text = e.UserState.ToString();
}
You're accessing cbLogType in a non-UI thread.
Change to
eLog.Source = text;

Click item in listbox and View details in multiline textbox

I would like to click on an item in a listbox and display the attributes that were passed into that listbox to a multiline textbox.
Below is the code I have written on form initialisation
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
foreach (KeyValuePair<int, Individual> kvp in ReadFromFile.individuals)
{
listBox2.Items.Add("ID = " + kvp.Value.id + " Name = " + kvp.Value.name.givenName + " " + kvp.Value.name.surname + " DoB = " + kvp.Value.birth.date);
}
int testIndividual = 94;
string genderOut = "";
if (ReadFromFile.individuals[testIndividual].gender == "M")
{
genderOut = "MALE";
}
else if (ReadFromFile.individuals[testIndividual].gender == "F")
{
genderOut = "FEMALE";
}
try
{
textBox1.AppendText(
"Name = " + ReadFromFile.individuals[testIndividual].name.givenName + " "
+ ReadFromFile.individuals[testIndividual].name.surname
+ Environment.NewLine + "Gender = " + genderOut
+ Environment.NewLine + "Birth date = " + ReadFromFile.individuals[testIndividual].birth.date
+ Environment.NewLine + "Birth place = " + ReadFromFile.individuals[testIndividual].birth.place
+ Environment.NewLine + "Death date = " + ReadFromFile.individuals[testIndividual].death.date
+ Environment.NewLine + "Death place = " + ReadFromFile.individuals[testIndividual].death.place);
}
catch
{
MessageBox.Show("This individual doesnt exist");
}
}
}
I would like to add more so I can click on a listbox item and the details for that item will be shown in the textbox
I get the feeling I may have to override the ToString() method or regex it. Im still quite a novice programmer so go easy on me :) THANK YOU
You need to handle the SelectedIndexChanged event for your listbox.
One way to do this is to bring up Form1.cs[Design] and select the listbox. In the property grid (Alt+Enter) click the icon that looks like this:
Find the event SelectedIndexChanged and double click it. That will hook up an event handler for you in the auto generated Form1.cs.designer file.
Next, replace the code for your Form1 class with the following:
public partial class Form1 : Form
{
private Dictionary<int, Individual> _individuals;
public Form1()
{
InitializeComponent();
ReadFromFile.Read("sample.GED");
_individuals = ReadFromFile.individuals;
listBox1.DataSource = _individuals.Select(individual => individual.Value).ToList();
listBox1.DisplayMember = "name";
listBox1.ValueMember = "id";
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Clear();
var individual = listBox1.SelectedItem as Individual;
string genderOut = (individual.Gender == "M") ? "MALE" : "FEMALE";
var displayText
= String.Format("Name = {0} {1}\r\n" +
"Gender = {2}\r\n" +
"Birth date = {3}\r\n" +
"Birth place = {4}\r\n" +
"Death date = {5}\r\n" +
"Death place = {6}"
, individual.name.givenName
, individual.name.surname
, genderOut
, individual.birth.date
, individual.birth.place
, individual.death.date
, individual.death.place);
textBox1.AppendText(displayText);
}
}
A few notes about some of the things i've changed.
I've moved the code that was setting the textbox value into the SelectedIndexChanged event handler
I've refactored that code so that it's more readable by using the static String.Format method (all those Environment.NewLine repeats you had were messy).
I've setup the data for the list box using the DataSource property instead of your foreach loop.
Also, one thing you'll notice with this is that the list items in the listbox will not show the correct text. This is because you appear to be using some custom classes or structs for the name, birth and death of an Individual? To fix this, you need to add a new property to the Individual class like this:
public class Individual
{
// ... your code
public string DisplayName
{
get { return String.Format("{0} {1}), name.givenName, name.surname; }
}
// ... the rest of your code
}
Then you will need to change the line in my code above that looks like this:
listBox1.DisplayMember = "name";
to this:
listBox1.DisplayMember = "DisplayName";
Final note: You should probably be using "Upper Camel Case" for your property names. That means that they start with an upper case letter and then the first letter of each word is also upper case. For example, name.givenName should be Name.GivenName. This is a widely used convention.

Categories

Resources