Snippet samples

Hungry Bulldozer

Moderator
Регистрация
12.01.2011
Сообщения
3 441
Реакции
837
Баллы
113
In this thread we will post samples of c# snippets

1 Alert with 2 buttons: "Yes" and "No":
Result is put to project's variable
1 - "Yes" was pressed
0 - "No" was pressed
-1 - "Close"
JavaScript:
Развернуть Свернуть Копировать
// Show message
System.Windows.Forms.DialogResult result = System.Windows.Forms.MessageBox.Show("Press \"Yes\" or \"No\"", "Caption", System.Windows.Forms.MessageBoxButtons.YesNo);
// parse result
switch (result)
{
    // if "Yes"
    case System.Windows.Forms.DialogResult.Yes:
		// here you can put your code
		// or like this
		return 1;
		break;
    // если нет
    case System.Windows.Forms.DialogResult.No:
		// here you can put your code
		// or like this
        return 0;
        break;
    // if something else, for instance DialogResult.Cancel
    default:
		// here you can put your code
		// or like this
		return -1;
        break;
}
Link to the original post: http://zennolab.com/discussion/showthread.php?7860-Alert-C&p=45044&viewfull=1#post45044

2 Get last error:

This c# code has to be placed in bad end.

JavaScript:
Развернуть Свернуть Копировать
var error = project.GetLastError();
var tmp = "";
if(error != null)
    tmp = string.Format("ActionComment: {0}.\r\nActionGroupId: {1}.\r\nActionId: {2}", error.ActionComment, error.ActionGroupId, error.ActionId);
 
return tmp;

3 Complete all checkboxes on the page:

On the page all tags input:checkbox of active tag are filled with value from "CheckboxValue" ({-Variable.CheckboxValue-}).
"CheckboxValue" can be True or False.
JavaScript:
Развернуть Свернуть Копировать
// if page is not loaded yet, wait loading
if (instance.ActiveTab.IsBusy) instance.ActiveTab.WaitDownloading();
// find all checkbox on the page of active tab
HtmlElementCollection heCol = instance.ActiveTab.FindElementsByTags("input:checkbox");
// go through all checkboxes and fill them in
foreach(HtmlElement he in heCol.Elements)
{
    // fill checkbox
    he.SetValue(project.Variables["CheckboxValue"].Value, instance.EmulationLevel, false);
    // emulation delay
    instance.WaitFieldEmulationDelay();
}

4 Fill in all selects on the active tab page by last options:

It does work with classic select tags.
JavaScript:
Развернуть Свернуть Копировать
// if page is not loaded yet, wait loading
if (instance.ActiveTab.IsBusy) instance.ActiveTab.WaitDownloading();
// find all selects on the page
HtmlElementCollection heCol = instance.ActiveTab.FindElementsByTags("select");
// go through all selects and fill them in
foreach(HtmlElement he in heCol.Elements)
{
    // Find all options of select
    HtmlElementCollection children = he.FindChildrenByTags("option");
    // if there is any element
    if (children.Count > 0)
    {
        // get last
        HtmlElement c = children.Elements[children.Count-1];
        // if element is not empty
        if (!c.IsVoid)
        {
            // get element value
            string outerText = c.GetAttribute("outertext");
            // set value
            he.SetSelectedItems(outerText);
            // emulation delay
            instance.WaitFieldEmulationDelay();
        }
    }
}
 
Последнее редактирование:
Thanks HB. This will be very helpful for all "non-coder" Zenno users.
 
I would like to request a code snippet for the new project.GetLastError() function. I've tried the example code but couldn't get it working correctly.
 
  • Оценить
Реакции: Kepperbes
Thanks shade. I didn't know you had to use the bad end for it to work so that's why
I was having problems. Great to see my idea for this added in because it's really helpful!
 
Here's a couple that might come in handy....

============================================
LAUNCH WINDOWS PROGRAM

System.Diagnostics.Process.Start("PATH TO .EXE HERE");

================================================

From DarkDiver:

INPUT TEXT INTO FORM WHERE CURSOR IS

{
// lock object to avoid multithreaded problems
lock(SyncObjects.InputSyncer)
{
var x = project.Variables["fname"].Value;
// activate window
Emulator.ActiveWindow("NAME OF WINDOW (IE: UNTITLED - NOTEPAD");
// send keyboard event to the active window
System.Windows.Forms.SendKeys.SendWait(x);

}
}

====================================================

System.Threading.Thread.Sleep(2000); THIS IS PAUSE


=====================================================

SCREEN CAPTURE

{int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(200, 80); (SIZE OF IMAGE YOU WANT TO SAVE)
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(450, 140, 0, 0, new Size(200, 80)); (450 AND 140 ARE X AND Y OF CORNER OF ELEMENT)
bmpScreenShot.Save(@"C:\test3.jpg");}
 
  • Оценить
Реакции: Kepperbes
Count words in a variable text:

JavaScript:
Развернуть Свернуть Копировать
return project.Variables["text"].Value.Split( new char[] { ' ', ',', ';', '.', '!', '"', '(', ')', '?' },
    System.StringSplitOptions.RemoveEmptyEntries).Length;
 
hello bigcajones..
can give the exampale template for how to run it :)


Here's a couple that might come in handy....

============================================
LAUNCH WINDOWS PROGRAM

System.Diagnostics.Process.Start("PATH TO .EXE HERE");

================================================

From DarkDiver:

INPUT TEXT INTO FORM WHERE CURSOR IS

{
// lock object to avoid multithreaded problems
lock(SyncObjects.InputSyncer)
{
var x = project.Variables["fname"].Value;
// activate window
Emulator.ActiveWindow("NAME OF WINDOW (IE: UNTITLED - NOTEPAD");
// send keyboard event to the active window
System.Windows.Forms.SendKeys.SendWait(x);

}
}

====================================================

System.Threading.Thread.Sleep(2000); THIS IS PAUSE


=====================================================

SCREEN CAPTURE

{int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(200, 80); (SIZE OF IMAGE YOU WANT TO SAVE)
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(450, 140, 0, 0, new Size(200, 80)); (450 AND 140 ARE X AND Y OF CORNER OF ELEMENT)
bmpScreenShot.Save(@"C:\test3.jpg");}
 
Hi everyone, can someone help me with a snippet to open a window where I can input some text/numbers, and the input will be saved into a variable that can be used in zennoposter? I want to manually fill some fields. Thanks!
 
Hi everyone, can someone help me with a snippet to open a window where I can input some text/numbers, and the input will be saved into a variable that can be used in zennoposter? I want to manually fill some fields. Thanks!

If you want to fill fields manually then you can use same Alert code and enter fields in browser window.

Otherwise look over there:

http://zennolab.com/discussion/thre...rmami-oknami-windows-cherez-snippety-c.13416/
 
Thanks lokiys, it works, but how can I input the data with "Enter" instead of closing the window? I am using the following snippet

System.Windows.Forms.Form F = new System.Windows.Forms.Form();
F.Text = "nameofbox";

// create a textbox
System.Windows.Forms.TextBox textb = new System.Windows.Forms.TextBox ();
// specify the location
textb.Location = new System.Drawing.Point (50,50);
// You can specify the size textbox
textb.Width = 200;
// Add it to the form
F.Controls.Add (textb);


F.ShowDialog();
return textb.Text;
 
Thanks anyways lokiys, your reference helped me a lot. Is a lot better to input the text in a box than having to open each browser.
 
Thanks anyways lokiys, your reference helped me a lot. Is a lot better to input the text in a box than having to open each browser.


I re-read your post and still do not understand why you can not enter everything you need in Zennoposter browser instance ?

Use:

C#:
Развернуть Свернуть Копировать
System.Windows.Forms.MessageBox.Show("OK");

Where you need to do manual action, and when this alert box pops up then press SHOW in zennoposter and do your manual action, then press OK in alert box and DONE...

Cheers
 
  • Оценить
Реакции: Daikaiser

Кто просматривает тему: (Всего: 0, Пользователи: 0, Гости: 0)