0% found this document useful (0 votes)
25 views

WinForms Reference

The document describes how to pass values from UiPath to WinForms applications using named pipes or sockets for interprocess communication. It also provides examples of how to bind WinForms controls to data and dynamically change font sizes at runtime using data binding and a custom converter.

Uploaded by

Vicky Vicky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

WinForms Reference

The document describes how to pass values from UiPath to WinForms applications using named pipes or sockets for interprocess communication. It also provides examples of how to bind WinForms controls to data and dynamically change font sizes at runtime using data binding and a custom converter.

Uploaded by

Vicky Vicky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

WinForms Reference

Pass the value from Uipath to WinForms:

To pass a value from UiPath to a WinForm, you can use interprocess communication (IPC)
techniques such as named pipes or sockets.

Here's an example of how to use named pipes to pass a value from UiPath to a WinForm:

1. Create a named pipe server in your WinForm application. This can be done using the
NamedPipeServerStream class in C#. Here's an example:

NamedPipeServerStream pipeServer = new NamedPipeServerStream("myPipeName");

pipeServer.WaitForConnection();

StreamReader reader = new StreamReader(pipeServer);

string value = reader.ReadLine();

pipeServer.Disconnect();

This code creates a named pipe server with the name "myPipeName", waits for a connection from a
client, reads a string value from the pipe, and then disconnects from the client

2. In your UiPath workflow, create a named pipe client to connect to the named pipe server in
your WinForm application. This can be done using the
System.IO.Pipes.NamedPipeClientStream class. Here's an example:

NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", "myPipeName",


PipeDirection.Out);

pipeClient.Connect();

StreamWriter writer = new StreamWriter(pipeClient);

writer.WriteLine("myValue");

writer.Flush();

pipeClient.WaitForPipeDrain();

pipeClient.Close();

This code creates a named pipe client that connects to the named pipe server with the name
"myPipeName" on the local machine, writes a string value "myValue" to the pipe, and then
disconnects from the server.
3. In your WinForm application, read the value passed from UiPath using the named pipe
server code shown in step 1

Note that you should ensure that the named pipe names used in your WinForm and UiPath
applications match, and that your WinForm application is running and listening for connections
before you attempt to pass the value from UiPath

Databinding:

// Set up data binding for the txtFirstName control

txtFirstName.DataBindings.Add(new Binding("Text", bindingSource1, "FirstName", true,


DataSourceUpdateMode.OnPropertyChanged));

(or)

// Assuming a TextBox control named "txtName" and a BindingSource named "bindingSource1"

txtName.DataBindings.Add(new Binding("Text", bindingSource1, "Name"));

Change Fonts size of Winform on Runtime using Data Binding:

To change the font size of a WinForm control on runtime using data binding, you can bind the Font
property of the control to a font object and use a converter to modify the font size dynamically. Here
are the steps:

1. Create a font object and set the initial font size:

Font initialFont = new Font("Arial", 10);

2. Create a data binding object and bind it to the control's Font property:

Binding binding = new Binding("Font", this, "MyFont");

binding.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;

txtName.DataBindings.Add(binding);

In this example, "MyFont" is a property in your form class that returns the font object you want to
use. The DataSourceUpdateMode property is set to OnPropertyChanged, so the font will be updated
whenever the MyFont property is changed.
3. Create a converter to modify the font size:

public class FontSizeConverter : IValueConverter

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

if (value is Font font && parameter is double size)

return new Font(font.FontFamily, (float)size, font.Style, font.Unit, font.GdiCharSet,


font.GdiVerticalFont);

return value;

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

throw new NotImplementedException();

This converter takes a font object and a double value representing the font size. It returns a new font
object with the specified size.

4. Set the MyFont property in your form class to update the font size:

private Font _myFont = initialFont;

public Font MyFont

get => _myFont;

set

{
_myFont = (Font)fontSizeConverter.Convert(value, typeof(Font), 16,
CultureInfo.InvariantCulture);

OnPropertyChanged(nameof(MyFont));

In this example, the font size is set to 16, but you can change it to any value you want. The
OnPropertyChanged method is called to notify the data binding system that the MyFont property
has changed.

When the MyFont property is updated, the converter is called to modify the font size, and the new
font object is used to update the Font property of the control. This will update the font size of the
control in real-time.

You might also like