Showing posts with label WFA. Show all posts
Showing posts with label WFA. Show all posts

Friday, August 27, 2021

[VB.net][Example] print message with commend prompt console in WFA

I found a script works and copy here, i worry i don't know where to found this again someday:


Option Compare Binary

Option Explicit On

Option Infer Off

Option Strict On


Imports System


Public Class Form1

   Private Declare Function AllocConsole Lib "Kernel32.dll" () As Integer

   Private Declare Function GetStdHandle Lib "Kernel32.dll" (ByVal nStdHandle As Integer) As Integer

   Private Declare Function WriteConsoleA Lib "Kernel32.dll" (ByVal hConsoleOutput As Integer, ByVal lpBuffer As String, ByVal nNumberOfCharsToWrite As Integer, lpNumberOfCharsWritten As Integer, lpReserved As Integer) As Integer


   Private Const STD_OUTPUT_HANDLE As Long = -11


   Private Sub Form1_DoubleClick(sender As Object, e As EventArgs) Handles MyBase.DoubleClick

      AllocConsole()

      Dim OutputHandle As Integer = GetStdHandle(STD_OUTPUT_HANDLE)

      Dim Text As String = "test"


      WriteConsoleA(OutputHandle, Text, Text.Length, 0, 0)

   End Sub

End Class


Beware the text highlighted with yellow text, if you use the keyword "WriteConsole" would got "EntryPointNotFoundException was unhanded" error


Reference:

https://www.vbforums.com/showthread.php?888189-Console-from-Winform-App-not-writing-lines

Wednesday, January 6, 2021

[VB.net][Resolved] property value is not valid

I want last column of dataGridView automatically resize according width of application.

White I set the autoSize property to Fill on last column, it pop up an error message:

Error message: Property value is not valid

check details

Column's AutoSize mode cannot be set to Fill when it is also a frozen column


Steps:

I fix this problem in design mode but not using code.

1) Select dataGridView in designmode, and click the right-arrow icon to open "DataGridView Task" menu.

2) Select "edit Columns..." item in menu and it would popup "Edit Column" window.

3) There should be some column items under "selected Columns" text (if you don't have, add some columns)

4) In edit column panel, set "frozen" property value to False in "layout" catalog. apply on all your columns in dataGridView.

5) For last column, set "AutoSizeMode" property value to "Fill" in "layout" catalog., others columns all set to "AllCells"

6) Click "OK"


Remark: If you want dataGridView able to autoresuze according you window form application size, Set Anchor in your property panel to "Top, Bootom, Left, Right"


Reference:

https://stackoverflow.com/questions/1025670/how-do-you-automatically-resize-columns-in-a-datagridview-control-and-allow-the

Tuesday, January 5, 2021

[VB.net][WFA][example] Get ToolStripMenu items

 

Private Sub ColourToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColourToolStripMenuItem.Click
  Dim count As Integer = sender.DropDownItems.Count
  For Each subItem In sender.DropDownItems
    Dim objAsConvertible = TryCast(subItem, ToolStripMenuItem)
    If Not objAsConvertible Is Nothing Then
      Dim tsm As ToolStripMenuItem = DirectCast(subItem, ToolStripMenuItem)
      Debug.WriteLine(CStr(tsm.Text)+":"+CStr(tsm.Name))
    End If
  Next
End Sub


[VB.net][WFA][example] Get ContextMenuStrip items

 

Private Sub ContextMenuStrip1_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip1.Opening
  For Each item As ToolStripItem In ContextMenuStrip1.items
    Dim objAsConvertible = TryCast(item, ToolStripMenuItem)
    If Not objAsConvertible Is Nothing Then 
      Dim cms As ToolStripMenuItem = DirectCast(item, ToolStripMenuItem)
      Debug.WriteLine(CStr(cms.Text)+":"+Cstr(cms.Name))
    End If
  Next
End Sub


Monday, January 4, 2021

[VB.net][WFA] Add a right click menu in your application

Step 1) 

Drag a ContextMenuStrip from "Menu & Toolbars" catalog from toolBox to your form in design view.


Step 2) 

Click your "ContextMenuStrip" control and than double click the "Type here", type the text your want to display there.


Step 3) 

Add a mouseUp event to your form.


Step 4) 

Add script to your event handler for showing your contextMenuStrip :

Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp

  If e.Button <> MouseButtons.Right Then Return

  ContextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y)

End Sub

Where text in pink is property name of your "contextMenuStrip".

Thursday, August 13, 2020

[VB][Resolved] name "OpenFileDialog" is not declared

 * Visual Studio 2008 with .Net framework version 3.5


Error code:

Dim result As DialogResult = OpenFileDialog.ShowDialog()

If result = DialogResult.OK Then

  'Do sth

End If


Firstly let imports System.Windows:

Imports System.Windows

and then try another syntax

Dim odf As FileDialog = New OpenFileDialog()

If odf.ShowDialog = DialogResult.OK Then

  'Do sth

End If


Reference:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.openfiledialog?view=netcore-3.1

Thursday, April 16, 2020

[C#][Example] Dynamic create LinkLabel and related event handler

place in constructor
Dictionary<String,String> pdfs = new Dictionary<String, String>(){
  {"Apple","http://www.example.com/apple.pdf"},
  {"Banana","http://www.example.com/banana.pdf"},
  {"Orange","http://www.example.com/orange.pdf"},
};
LinkLabel[] labels = new LinkLabel[pdfs.Length];
for(KeyValuePair<string, string> pair in pdfs){
  label[i] = new Label();
  label[i].Name = "label"+i;
  label[i].Location = new Point(27,i*30);
  label[i].TabStop = true;
  label[i].Text = pair.Key;
  label[i].Links[0].LinkData = pair.Value;
  this.Controls.Add(labels[i]);
}
And the Event Handler
private void linkedLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e){
  System.Diagnostics.Process.Start(e.Link.LinkData as string);
}
Referecne:
https://www.dotnetperls.com/dictionary

Wednesday, April 15, 2020

[C#][Resolved] Readonly TextBox with ScrollBar

I want a readonly textarea show a long list of news.
But there is no textarea but multiline textbox.
So what should do is create a new TextBox with multiline:
TextBox textBox = new TextBox();
textBox.Multiline = true;
textBox.WordWrap = true;
textBox.ReadOnly = true;
textBox.Text = "your long text";

However, the overflowed content are hidden, let set a vertical scrollbar for user to view the hidden part, insert this line after your insert statment:
textBox.ScrollBars = ScrollBars.Vertical;

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox.multiline?view=netframework-4.8

Tuesday, April 14, 2020

[C#][Resolved] c# textbox get text from resource

Firstly you need create an resource in "Resources.resx" file,
such as add Name as "title" and Value as "A Big Big Title" in your Resource.resx

Name      Value              Comment
========================================
title     A Big Big Title    

And than get it by "Properties.Resources.title". An example:

Label labelTitle = new Label();
labelTitle.Name = "labelTitle";
labelTitle.Size = new System.Drawing.Size(130,20);
labelTitle.TabIndex = 1;
labelTitle.Text = Properties.Resources.title;

And then you will found string "A Big Big Title" was shown in your design panel or at the result of your application.

Reference:
https://stackoverflow.com/questions/1508570/read-string-from-resx-file-in-c-sharp

Monday, March 16, 2020

[C#][Resolved] Error : The designer cannot process the code

Error Message:
The designer cannot process the code at line 259, please see the Task List for details. The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again.


Please check are you added bussinese logic in your Designer.cs file.
If yes, please move the your code to the .cs file

An example:
I want a for-loop to create many labels but I added this logic to file "form.Designer.cs", move the logic to "form.cs" solved.

Sunday, February 16, 2020

[C#][Example] Dynamic create labels

I put these code into .cs file constructor:
string[] pdfs = new string[]{"Apple","Banana","Mango","Cheery","Orange"};
Label[] labels = new Label[pdfs.Length];
for(int i=0; i<pdfs.Length; i++){
  label[i] = new Label();
  label[i].Name = "label"+i;
  label[i].Location = new Point(27,i*30);
  label[i].TabStop = true;
  label[i].Text = pdfs[i];
  this.Controls.Add(labels[i]);
}

Reference
https://stackoverflow.com/questions/15008871/how-to-create-many-labels-and-textboxes-dynamically-depending-on-the-value-of-an

Sunday, February 2, 2020

[C#][Resolved] Check LinkLable open a link with default browser

Add "System.Diagnostics.Process.Start("http://www.your-website.com");" in your event handler, as usual if you double click the linkLabl in your design view, you would be leaded to related method.

An example:
private void linklabelGuide_LinkClicked(object sender,LinkLabelLinkClickedEventArgs e){
  string url = "https:/www.google.com";
  System.Diagnostics.Process.Start(url);
}
If you run the application and click the linkLabel, default broswer of your operating system would be opened and load the link you specificed.

Reference:

https://stackoverflow.com/questions/7154256/linklabel-open-in-default-web-browser