C# winform check every 30 mins for log in session

In the Solution Explorer right-click the project and select Properties. Go to the “Settings” tab and create a “SettingInterval”. Make the type “int” for the SettingInterval.

public frmMain()
{
InitializeComponent();
timer1.Tick += Timer1_Tick;
timer1.Interval = 1000;
}
private void Timer1_Tick(object sender, EventArgs e)
{
TimeSpan ts = Expires – DateTime.Now;
if (ts.TotalSeconds < 10)
if (Warning)
{
Warning = false; // this must be before the MessageBox
btnLogIn.BackColor = Color.Orange;
MessageBox.Show(“You have 30 seconds left!”);
}

}

I have a button which will trigger the timer.

private void btnLogIn_Click(object sender, EventArgs e)
{
int NewInterval = 30; //30 min……AccessToken expired in 30 min
Properties.Settings.Default.SettingInterval = NewInterval;
Expires = DateTime.Now.AddMinutes(NewInterval);
Warning = true;
timer1.Start();

}

By simplemsexchange Posted in C#, VB.NET

Winform – Override textbox to show sample text and clear on click

Winform watermark text on textbox:

 

public string initTextClient = “Client”;
public bool editedClient;

public SelectMatterClient()
{
InitializeComponent();

txtClient.Text = initTextClient;
txtClient.ForeColor = Color.Gray;

}

 

private void txtClient_KeyPress(object sender, KeyPressEventArgs e)
{
editedClient = !char.IsControl(e.KeyChar);
}

private void txtClient_Enter(object sender, EventArgs e)
{
if (!editedClient)
{
txtClient.Clear();
txtClient.ForeColor = Color.Black;
}
}

private void txtClient_Leave(object sender, EventArgs e)
{
if (!editedClient)
{
txtClient.Text = initTextClient;
txtClient.ForeColor = Color.Gray;
}
}Capture

Get the column value of the selected row in wpf listview

<TabItem x:Name="HistoryTab" Header="History" Style="{StaticResource TabStyle}">
  <Grid>
    <ListView x:Name="HistoryTabLv" HorizontalAlignment="Left" Height="164" Width="275" VerticalAlignment="Top" SelectionChanged="HistoryTabLv_SelectionChanged" SelectionMode="Single">
      <ListView.View>
        <GridView>
          <GridViewColumn x:Name="TimeColumn" Header="Time" Width="85">
            <GridViewColumn.CellTemplate>
              <DataTemplate>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="-5,0,0,0">
                  <Image x:Name="Img" Height="12" Width="12" Source="{Binding Image}" Stretch="Uniform"/>
                  <TextBlock Text="{Binding Time}"/>
                </StackPanel>
              </DataTemplate>
            </GridViewColumn.CellTemplate>
          </GridViewColumn>
          <GridViewColumn x:Name="PhoneNumColumn" Header="Phone Number" Width="85" DisplayMemberBinding="{Binding PhoneNum}" />
          <GridViewColumn x:Name="DirectionColumn" Header="Direction" Width="95" DisplayMemberBinding="{Binding Direction}" />
        </GridView>
      </ListView.View>
    </ListView>
  </Grid>
</TabItem>
dynamic selectedItem = HistoryTabLv.SelectedItems[0];
var phoneNum = selectedItem.PhoneNum;


https://stackoverflow.com/questions/30274071/how-to-get-the-column-value-of-the-selected-row-in-wpf-listviewReff: 
By simplemsexchange Posted in C#

Create Simple Windows Service And Setup Project With Installation

Create Simple Window Service

Open Visual Studio 10 then go to File > New > Project. That will open a dialog box for a new project then select “Window Service” and click on the OK button.

After creating the project you can see the service design page as in the following and click on the link “click here to switch to code view”.

The Solution Explorer will then look like this:

The WinService class should contain the two functions OnStart() and OnStop() and contain the following code:

  1. public partial class WinService : ServiceBase
  2. {
  3.    public WinService()
  4.    {
  5.       InitializeComponent();
  6.    }
  7.    protected override void OnStart(string[] args)
  8.    {
  9.       //
  10.    }
  11.    protected override void OnStop()
  12.    {
  13.       //
  14.    }
  15. }

 

Add Configuration manager reference to the project.

conf

Now modify the above code with the followings:

 

public partial class WinService : ServiceBase
{
public WinService()
{
InitializeComponent();
}
private System.Timers.Timer timer = new System.Timers.Timer();
protected override void OnStart(string[] args)
{
timer.Interval = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings[“timeInterval”]); //Geting how often you want to run the service from config file.
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
timer.Stop();
try
{
MainProgram();
}
catch
{
}
timer.Start();
}
public void MainProgram()
{
//Your Custom Code what the service will do will be here
}
protected override void OnStop()
{
}
}

 

Go to Solution Explorer and right-click on WinService.cs then go to the view designer then look at the design view then right-click on the design page and click on “add installer” and it will look like this:

After adding the ProjectInstaller.cs you have 2 components in the design view of the ProjectInstaller.cs (serviceProcessInstaller1 and serviceInstaller1). You should then set up the properties as you need.

Click on serviceinstaller1 and go to the properties in the right pane and edit the service name Service1 to WinService as in the following:

Click on serviceProcessInstaller1 and go to the properties in the right pane and select Account and choose LocalService from the dropdown and save. See the following:

You have two assemblies under References as in the following highlighted (1) System.Configuration.Install and (2) System.ServiceProcess.

Create Setup Project

Build your service project, you need to set up the project to install the build/compiled project and run the installers to run the Windows service. Create a new project as a “Setup Project” and you need to add project output.

Create Setup Project for Window Service

Go to the Solution Explorer and right-click on the solution, go to Add > New Project.

Open a dialog box, go to left pane under Installed Templates > Other Project Types > Setup and Deployment > Visual Studio Installer and go to the right pane and select the project as a “Setup Project” and click on the OK button.

(If Visual Studio Installer is not available in menu, for VS 2010, please download this extention) 

Custom actions add to setup project

Go to the solution, right-click on the setup project then select View > Custom action.

The “Custom action” editor appears.

Right-click on “Custom action > Add Custom action”.

Open a dialog box “Select Item In Project” then double-click on “Application Folder” then click on the “Add Output” button. Open a dialog box, choose your project (Window service) and “Primary Output” from (Active) and click on the OK button.

Build your setup project and install the Windows service.

Install And Start The Service

Go to the Solution Explorer then select Build for both projects (Windows Service and Setup Project) then right-click on the Setup Project and click on the Install option and follow the setup wizard procedure. Finally your service is ready for being started and stopped. If you want to start the Windows server then go to Start > Administrator tools > Service > Find Your Service then right-click on the Service Name then select Start. Otherwise go to Start > Computer (My Computer) > Manage > Click on Service And Application from the left pane > Services.

Perform Specific Button click event when user press Enter key in Textbox

Put your form inside an asp.net panel control and set its DefaultButton attribute with your button ID. See the code below:

 

<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
         <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Send" />
             </ContentTemplate>
          </asp:UpdatePanel>
</asp:Panel>

Visual Studio 2013 Change the Default Location for Projects

For corporate office user my document is always redirected to a file server where creating exe file is restricted.

1. Create a folder in c drive called: vs 2013

2. Inside that folder create 2 folders

Projects

Templates

3. Inside Templates, create another 2 folders:

ItemTemplates

ProjectTemplates

4. Open visual studio and follow the steps

  • On the Tools menu, select Options.
  • From the Projects and Solutions folder, select General.
  • In the Visual Studio projects location text box, enter a location for files and projects.

 

vs2013

 

And you should be all set.

 

Configure visual studio to use youtube API

Install youtube api for your application first:

y1

y2

 

PM> Install-Package Google.Apis.YouTube.v3

 

Add the following code in your cs file:

 

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

 

Further details:

 

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth