Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I Want to know that i have define a function in form
Eg:Dynamically Display (Running) Current Date Time
It display the current system time on that form
Is it possible to call the same function without creating a new function on each and every form?
please help
Solution 1: You dont need to write a function for this:
Label1.Text = DateTime.Now.ToString();
Solution 2: but if you want to access it using function create a static function
public static class Utility
{
public static string DisplayDateTime()
{
return DateTime.Now.ToString();
}
}
Call the above function wherever you want as below:
Label1.Text = Utility.DisplayDateTime();
Solution 3: if you want to change datetime for every second try this:
public static class Utility
{
public static string DisplayDateTime()
{
return DateTime.Now.ToString();
}
}
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=1000;//one second
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
Label1.Text = Utility.DisplayDateTime();
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I have this code:
<div class="col-lg-12 control-section toast-default-section">
<SfToast ID="toast_default" #ref="ToastObj" Title="Error" Content="#a" </SfToast>
</div>
#code{
a = "Test";
StringBuilder b = new StringBuilder();
b.Append("Hello ");
if (b.ToString() != "")
{
a = a+ b;
StateHasChanged();
await ToastObj.ShowAsync();
}
}
When this "ToastObj" open, I need click 2 times to refresh. I don't know why.
For example:
First Result: Test
Second Result: Test Hello
I need click two times to refresh.
For the future, it would be helpful to see all the code, including the SFToast component.
However, You are calling it correctly. All you have to do to Get the page to refresh is to have the code hit the "StateHasChanged()" function. I think the problem is that you have code sitting outside of any function. Consider the #code{} to act like a normal class file with a few diffrences. For example there is no "Constructor" but the first method called on creation will be the OnInitalized(). I suggest wrapping the code and making sure all variables are initialized
#code{
protected override void OnInitialized()
{
var a = "Test";
StringBuilder b = new StringBuilder();
b.Append("Hello ");
if (b.ToString() != "")
{
a = a+ b;
StateHasChanged();
await ToastObj.ShowAsync();
}
}
}
Try use InvokeAsync(StateHasChanged);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm new to coding and I have a simple problem that I cannot find the solution anywhere for.
All I would need is to pass information from one void to another.
I have tried to find a solution but many solutions go on for passing information from class to class and script to script, and that's not exactly what I need and none have worked.
The Code right now is something like this:
public static void Main(string[] args){
GetVoid.DoCheck();
}
public void GenerateInformation(){
DateTime StoredDateTime;
//Code that does something
StoredDateTime = //Date generated from the rest of the code not included
}
public void DoCheck(){
DateTime CheckedDateTime;
GenerateInformation();
CheckedDateTime = StoredDateTime;
//Rest of the code for that void
}
My end goal is for the date so simply be stored in the other DateTime but I get the error
The name 'StoredDateTime' does not exist in the current context.
As far as I know this is because it does not know what StoredDateTime is, and to fix this I would need to pass on the information from one void to the other.
But how would I do that?
void mean:
not valid or legally binding.
completely empty.
So, you cannot expect from void method to pass you result. You need to change your method signature and implement return.
public static DateTime GetDate()
{
var dateVal = DateTime.Now;
return dateVal;
}
then
public static void Main(string[] args){
var dateRetult = GetDate();
Console.WriteLine(dateResult);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
private void button4_Click(object sender, EventArgs e)
{
Program.keepRunnig = true;
this.Close();
Thread.Sleep(1000);
StreamWriter stw = new StreamWriter(#"E:\NabilDataBase\ChangeFormLang\ChangeFormLang\1.txt");
switch (Thread.CurrentThread.CurrentUICulture.IetfLanguageTag)
{
case "en-US": Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar-IQ");
// stw.Write("ar-IQ");
stw.Close();
break;
case "ar-AR": Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
// stw.Write("en-US");
stw.Close();
break;
}
this.Controls.Clear();
InitializeComponent();
}
I made a program multi language interface with thread class and added two language,my question is when I change the interface to another language that I added and close the form then I reopen it I see the interface returned to the default language, how can I save it the interface when closed.[enter image description here][1]
If you create a new Setting "Culture" you can save information at runtime like so:
Properties.Settings.Default.Culture= "ar-ar";
Properties.Settings.Default.Save();
And load it like so:
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Properties.Settings.Default.Culture);
How To: Write User Settings at Run Time with C#
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Ho do you get the current instance of a class?
The class have search method and cancel method.
Code example:
if (btnSearch.Text =="Search")
{
Searcher srch = new Searcher();
srch.Search();
btnSearch.Text = "Cancel";
return;
}
if (btnSearch.Text == "Cancel")
{
//call a method in the instance above for example
srch.Cancel();
}
I want to create the instance only when btnSearch.Text =="Search"; and when btnSearch.Text =="Cancel"; i want to call srch.Cancel();
////
Thanks to nmclean, problem solved, makes sense i had to declare the Search class at a higher scope to be able to access the current running instance.
Your srch variable must be declared at a higher scope than the function, otherwise it will not persist to the next time the function is called. Most likely this means it should be a field of the class:
class YourClass
{
private Searcher srch;
void YourMethod()
{
if (btnSearch.Text == "Search")
{
srch = new Searcher();
srch.Search();
btnSearch.Text = "Cancel";
return;
}
if (btnSearch.Text == "Cancel")
{
srch.Cancel();
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
am calling the function of a form from another form. It executes but do not really show up. so any suggestions??
My code goes like
In Form FrmA
private void Print()
{
FrmMenu ObjMain = new FrmMenu();
ObjMain.Show();
}
public void CreateButtons(string Action)
{
btn.Text=Action;
}
And When the Form FrmMenu gets open then another function gets executed in constructor of FrmMenu
Public FrmMenu()
{
FrmA f2 = new FrmA();
f2.CreateButtons("NEW");
}
But nothing happens ...!!! The function CreateButtons executes but do not show any changes
The text of button remain the same.
so please help me out.
EDITED
You must pass the instance of FrmA into the constructor of FrmMenu.
In FrmA:
private void Print()
{
FrmMenu ObjMain = new FrmMenu(this);
ObjMain.Show();
}
In FrmMenu:
public FrmMenu(FrmA f2)
{
f2.CreateButtons("NEW");
}