Usefull snippet C# code - Digital Website

Go to content

Main menu:

Computer science > Programming > MonoDevelop

Monodevelop.
Usefull source examples C# for Linux.


  • Change textfont & color from a label:


//Place a label on the form called label1
//Text = Arial - Fontsize = 14
Pango.FontDescription l1 = Pango .FontDescription.FromString  ("Arial 14");
label1 .ModifyFont(l1);
l1 .Family ="Arial";
l1.Size = 14;

//The displayed color of the text is red

Gdk.Color col = new Gdk .Color ();
Gdk .Color .Parse ("red",ref col );

//Add the definied color to the label
label1.ModifyFg (StateType .Normal ,col );   //color will now be red!
label1 .Text = "Hello World....";


  • Change textcolor of a textbox: (textbox name = txtEnter; declare a color with a free chosen name c1)


Gdk .Color c1 = new Gdk.Color ();
Gdk.Color .Parse ("blue", ref c1 );

txtEnter .ModifyText (StateType .Normal ,c1);
//This make text colored in textbox


  • Put text into a textbox: (textbox name = txtEnter)


txtEnter .Buffer .Text = "This is some text...!";


  • Clear all the text into a textbox: (textbox name = txtEnter)


txtEnter .Buffer .Clear();


  • Put actual systemdate & time into a textbox: (textbox name = txtEnter)


txtEnter .Buffer .Text =DateTime.Now.ToString ();


  • Close - Terminate a programme:


Application .Quit ();


  • Expand or Collapse a block of source code:


#region Terminate programme
Application .Quit ();
#endregion

note: Above an example from code that is standing between region - endregion. All code between it, can be "collapsed" or "Expanded"!


  • Count the number of typed letters in a textbox: (declare intchars for store the value counted; place result into label => lblDisplay; the textbox has the name txtEnter)


#region When somebody type into the textboc calculate all characters
Int32  intchars;
intchars = txtEnter .Buffer .CharCount ;
//Place the value of counted characters into the label!
lblDisplay .Text = "There are " + intchars .ToString () + " characters at the textbox!";
#endregion


  • Count the numbers of characters when typing into textbox and show direct into labelbox: (textbox = txtEnter; label = lblDisplay)


protected void OnTxtEnterKeyReleaseEvent (object o, KeyReleaseEventArgs args)
{

#region When somebody type into the textboc calculate all characters
Int32  intchars;
intchars = txtEnter .Buffer .CharCount ;
//Place the value of counted characters into the label!
lblDisplay .Text = "There are " + intchars .ToString () + " characters at the textbox!";
#endregion

}


note: Double click on the settingsbox from your textbox under the tab: "Common Widget Signals" => "KeyReleaseEvent"

  • Display a timer on screen with continuous update interval an method to stop it:

   (textbox = txtTimeDisplay; textbox = txtInfo; button= btnTimerOff; button = btnTimerOn; button = btnExit)

using System;
using Gtk;

public partial class MainWindow: Gtk.Window
{

public Boolean blnKeepWorking;

public MainWindow (): base (Gtk.WindowType.Toplevel)
{

Build ();

}

protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{

Application.Quit ();
a.RetVal = true;

}

protected void OnBtnTimerOffClicked (object sender, EventArgs e)
{

blnKeepWorking =false ; //The timer will stop with working

}

protected void OnBtnTimerOnClicked (object sender, EventArgs e)
{

StartTimer ();

}

protected void OnBtnExitClicked (object sender, EventArgs e)
{

#region jump to CloseProgramme
CloseProgramme ();
#endregion

}

void CloseProgramme ()
{

#region Terminate programme
Application .Quit ();
#endregion

}

void StartTimer()
{

#region Activate timer
txtInfo .Buffer .Text = txtInfo .Buffer .Text + "Timer is working from: "+System .DateTime .Now .ToString () + "\n\r";
blnKeepWorking = true;
GLib .Timeout .Add (500, new GLib.TimeoutHandler (Update_Status ));

#endregion

}

bool Update_Status ()
{

#region Check boolean status, before to work
if (blnKeepWorking == true) //if public boolean blnKeepWorking = true => Start timer
{

txtTimeDisplay .Text = DateTime .Now .ToString ();
return true;

}
else                           //If public boolean blnKeepWorking = false => Stop timer
{

txtInfo .Buffer .Text = txtInfo .Buffer .Text + "Timer is stopped at: "+System .DateTime .Now .ToString () + "\n\r";
return false ;

}
#endregion

}

}


Note: The timer value is setted here in this example to '500ms'
GLib .Timeout .Add (500,....);

  • MessaDialog using (not advanced!)


     //We create a new MessageDialog with name welcome & it has only 1 accept button OK!

MessageDialog welcome = new MessageDialog (this, DialogFlags .Modal, MessageType.Info, ButtonsType .Ok, "The first window you make at Linux with MonoDevelop");
welcome .Title = "Hello Linux...";
welcome .Run ();
//Show the messagebox on screen!
welcome .Destroy ();   //After click OK button, destroy messagebox


note: We use here a simply messagebox with only 1 button on it => The OK button! We know that the user only can push this button. So, we can directly write .Destroy after clicking the OK button

  • MessageDialog using (more advanced, we can control with button the user did click on the message form!)


//We create a new MessageDialog with name welcome & it has only 2 accept button OK & CANCEL!
MessageDialog welcome = new MessageDialog (this, DialogFlags .Modal, MessageType.Info, ButtonsType.OkCancel, "The first window you make at Linux with MonoDevelop");
welcome .Title = "Hello Linux...";
ResponseType response = (ResponseType)welcome .Run ();   
//Declare ResponseType so we can check wich button the user did press
if (response == ResponseType .Ok)
{

//here we put code when user press OK button
welcome .Destroy (); //We want to destroy the message screen welcome

}
else
{

//Here you can put code when user press Cancel button.
System .Media .SystemSounds .Beep .Play (); //Play system beep sound
welcome .Destroy (); //Destroy welcome messagebox

}


note: We are working with 2 buttons (OK - CANCEL)...we control here wich button the user takes!

  • Lets play a systembeep


System .Media .SystemSounds .Beep .Play (); //Play system beep sound


We have also other options like:

System .Media .SystemSounds .Asterisk .Play();
System .Media .SystemSounds .Exclamation .Play();
System .Media .SystemSounds .Question .Play();
System .Media .SystemSounds .Hand .Play();


  • Change text manual from a  button (button called with name button1)


this.button1.label = "Click on me...";   //You will see on your screen that the text "Click on me.." is now on your button!


  • Terminate a programme instantly


   
System.Environment.Exit(0);  //using System; only at top programme => Konsole environmet.

    
note: The solution above we use when we have at top from our programme just an declaration of using System;
             But when you are working in a graphical environment, that you will be probally declared also: using Gtk;
             When the "using Gtk" is declared, you can use this method also to terminate programme.

    
Application.Quit();       //using Gtk; is declared at top programme => Graphical environment

   
note: When you did start your solution as an konsole application, but then you want to have some Gtk functions into it,
    then you need to go to: "Project" => "Edit References..."  and type into the search box gtk , now you will see you can
    activate the controls for using the namespaces Gtk!
    So, with other words, you were started the konsole applicaton and you don't want to use the method "System.Environment.Exit(0)"
    but you want to use "Application.Quit()", then this is the way how you can do it!


  • How to clear the console:


    
Console.clear();        //This will clear all actual text who is standing on it!






























































 

Back to content | Back to main menu