C# after form loading element auto load problem - c#

In windows application, I want to show for example my pc name when I use toolStripStatusLabel and code like:
private void toolStripStatusLabel2_Click(object sender, EventArgs e)
{
string CurrentMachineName = Environment.MachineName;
toolStripStatusLabel2.Text = CurrentMachineName;
}
after I just build this application show that type not show pc name
but if I click to pc name that time show my pc name
after click show pc name
but I need to show when my from load after auto load my pc name
I am using C# with windows application.

Move you code to form_Load event instead of label_Click , e.g.
private void Form1_Load(object sender, EventArgs e)
{
toolStripStatusLabel2.Text = Environment.MachineName;
}
as it is obvious from _Click name - this event will be fired after specific control would be clicked
And _Load - on control initialization

Related

Separating NotifyIcon_Click and ItemMenu_Click events

I have an application which runs mostly within the taskbar, and its icon may be left clicked in order to open a Windows form.
The NotifyIcon also has a few MenuItems which can be accessed by right clicking, and each have their own click events to serve their functionality.
private void menuItem4_Click(object Sender, EventArgs e)
{
if (programEnabled == false)
{
programEnabled = true;
Console.WriteLine("Enabled!");
}
else
{
programEnabled = false;
Console.WriteLine("Disabled!");
}
}
//Icon clicked!
private void notifyIcon1_MouseClick(object Sender, EventArgs e)
{
//Reopen the form
Form f = new TemperForm();
}
Every time a MenuItem click event is activated however, NotifyIcon_Click also runs and I need that event for opening the Windows form. I don't want every time I click any MenuItem to also run the code that I wish to run when the icon is clicked.
How can I still use the NotifyIcon_Click event while not having it run every time I click a MenuItem?
I've been at this for hours and it's driving me up the wall!

Last textBox inputted value, Settings saving

Currently trying to save settings of last inputted textBox value (e.g. number).
Partially this is working code. However, that code allows to remember last value while closing/opening of Parameter_Form(subForm). In case of closing MainForm (application itself), the last textBox value doesn't retain. Why? The history hasn't record. Also I can't get why the cell 'value' is empty. Please see pic.
private void Parameter_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.textBoxLastValue = textBox1.Text;
Properties.Settings.Default.Save();
}
I've found following stuff. Please see attached pic.
Principally it's that number I've inputted in textBox.
There's no issue within app running and opening/closing subForm.
There's an issue after closing of MainForm.
If you load and save settings manually, you should make sure you load settings in form load event and also save it in form closing event:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Properties.Settings.Default.Test;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Test = textBox1.Text;
Properties.Settings.Default.Save();
}
If you are using data binding to settings, then you just need to save when closing.

Csharp Windows phone task complete

I'm making Windows universal app using Visual Studio 2013. I'm trying to accomplish something like in this article.
On the main page I have many pictures. After clicking on one of these pictures the user goes to another page, which has a check box.
After user checkes the check box, I have to change the image on main page.
I don't know if this code is correct, but I'm doing something like this for second page
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
points.Text = (Convert.ToInt32(points.Text) + 4).ToString();
var currentCheckBoxItem = sender as CheckBox;
if (currentCheckBoxItem.IsChecked == true)
{
otra.bedre1.Source = (new Uri("ms-appx:///Assets/complete.png", UriKind.Absolute));
// }
But this is code for mainpage where can choose task:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
bedre1.Source = ImageSource;
}
Any ideas?
You would need to store the name and Checkstatus as fields inside SQlite database so that its available even after the app is closed. So you would need to do some research on it.

set title of tab in web browser

I am trying to make a web browser with multiple tabs. But now, I have a problem with the DocumentTitle for the name of the tabs.
The problem here is that the code to name the tab is performed before it loads the page. I tried to find a way to perform it after, but it doesn't work.
For example:
private void stackoverflowToolStripMenuItem_Click(object sender, EventArgs e)
{
((WebBrowser) tabControl1.SelectedTab.Controls[0]) .Navigate("Http://www.stackoverflow.com/");
Browser_Navigated(null, null);
}
void Browser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
tabControl1.SelectedTab.Text = ((WebBrowser)tabControl1.SelectedTab.Controls[0]).DocumentTitle;
}
The WebBrowser class exposes a DocumentTitleChanged event that you can use to update the tab title.

Changing theme calls UserControl_Loaded event

I am not getting why behavior of WPF user control and Windows forms user control is different . I added window loaded event which just shows message box as :
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Main Window Loaded","WPF");
}
Also I created one user control and added loaded event as :
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("User Control Loaded.","WPF");
}
I have put this user control in main window.
When i launch this , I get both message box, User controls as well as windows.
Now , When i change my theme from Aero to any High contrast then user control's message box is shown again.
Why this is happening ? Why this is different from Windows form ? What should I do to avoid showing it multiple times ?
Wajeed
You could have a boolean field which stores the state of whether the dialog was shown yet or not. If you change the theme the UI-elements will reload so naturally the event will fire again.
if (!_diagWasShown)
{
_diagWasShown = true;
//Show dialogue
}
you can create bool variable, which will indicate if MessageBox was shown.
bool isUserMessageBoxShown = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (!isUserMessageBoxShown)
{
MessageBox.Show("User Control Loaded.","WPF");
isUserMessageBoxShown = true;
}
}

Categories

Resources