BUG - Visual Studio losing my code in a WinForm prg file!!!

This forum is meant for questions and discussions about the X# language and tools
Post Reply
Anonymous

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by Anonymous »

This happens every time I edit a WinForm in X# FoxPro project.

I can tell you the steps to reproduce it:

1. Open a WinForm in the visual designer. Design a simple form with some UI Controls.
2. Press F7 and add some custom code to the PRG.
Save the Code changes in the PRG.
3. Click on the Form Designer tab.
4. Move one of the UI controls to a new position on the form canvas.
5. Save the Form from the Form Designer.
6. Press F7 to view the PRG code.
7. Be surprised when you see that your custom code has been stripped out of the PRG file.


It seems that the Form Designer will just regenerate the basic code for the form and wipe out your custome code.
User avatar
robert
Posts: 4498
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by robert »

Matt,
If your custom code contains UDCs such as the database commands, then this is a known issue and it is already solved in our internal build.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
FoxProMatt

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by FoxProMatt »

Here is my code:

Code: Select all

Using System
Using System.Collections.Generic
Using System.ComponentModel
Using System.Data
Using System.Drawing
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Using System.Windows.Forms

Begin Namespace WindowsFormsApplication2
    Public Partial Class Form1 ;
	    Inherit System.Windows.Forms.Form
    
	Public names As BindingList<String>
        Public Constructor()   Strict//Form1
            
            InitializeComponent()

            Use Employee
            Var employeeDataSource = DbDataSource()
            listBox1.DataSource = employeeDataSource
            listBox1.DisplayMember = "Last_name"
			
    	Private Method bindingSource1_CurrentChanged(sender As Object, e As System.EventArgs) As Void Strict
                Return
    
	Private Method Form1_Load(sender As Object, e As System.EventArgs) As Void Strict
                Return
    
	Private Method button1_Click(sender As Object, e As System.EventArgs) As Void Strict
    
	Private Method listBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) As Void Strict
            Return

End Class 

End Namespace

2020-05-30 08_21_27-WindowsFormsApplication2 (Running) - Microsoft Visual Studio Preview.png
2020-05-30 08_21_27-WindowsFormsApplication2 (Running) - Microsoft Visual Studio Preview.png (7.59 KiB) Viewed 696 times
User avatar
robert
Posts: 4498
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by robert »

Matt,
It is the USE command that causes the problem (fixed in next build).
If you replace it with DbUseArea(,,"Employee")
then it will work and the code will not be removed

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
FoxProMatt

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by FoxProMatt »

Robert- I need some other advice about the style of programming...

Given that the binding source is the entire employee DBF and the currently displayed item in the list box is the last name, how can I get the entire data row from the selected entry in the list box?

What I would want next is to know the ID value (from the ID field in the DBF) for the selected item in the list box.
User avatar
robert
Posts: 4498
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by robert »

Matt,

Listboxes are not designed to display more than one column.
To get the ID value back from the listbox you need to set the "ValueMember" of the listbox to the name of that column. You can also use "RecNo" as field name, since we have added a RecNo property to every row in the DbDataSource
If you want multiple columns you should check out the ListView control or the DataGridview control

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
FoxProMatt

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by FoxProMatt »

Robert - I've looked at other .Net C# Examples about BindingSource and ListBox control, and C# can give the entire object from the DataSource from the SelectedItem of the ListBox, if you Cast it to the correct Type.

Given this Class to represent the exact fields in the DBF:

Code: Select all

Public Class Employee

   Public First_Name As String
   Public Last_Name As String
   Public Employee_Id As Int

End Class
Then if I create a DbDataSource from the DBF and set that as the DataSource on the listbox, then this code should let me access any property from the DataSource record (which would com from each DBF row):

Code: Select all

Var id = ((Employee)listBox1.SelectedItem).Employee_Id
Var firstName = ((Employee)listBox1.SelectedItem).First_Name
User avatar
robert
Posts: 4498
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by robert »

Matt,
The C# example is not really comparable, since the data source for this example apparently is a list of Employee objects.
The DbDataSource() does not use this kind of objects, but uses a DbRecord, a generic class, that uses some 'advanced'
tricks to tell the listbox (or any other class that it binds to) to make it believe that it has properties with the names
of the fields in the DBF file that it exposes.
The only 'Real' properties this DbRecord class has are RecNo and Deleted
This class also has an 'indexer' property that takes a field name or field number and allows you to read/write these fields.

So what you can do is:
- cast the value of the selected item to DbRecord

Code: Select all

  VAR oRec := (XSharp.DbRecord) listBox1.SelectedItem
  var firstName := oRec["FirstName"]
  // or assuming that this is the first field in the DBF
  // var firstName := oRec[1]
I hope this helps.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
FoxProMatt

BUG - Visual Studio losing my code in a WinForm prg file!!!

Post by FoxProMatt »

This looks like a solution that will work perfectly for what I want to do.

Thanks!
Post Reply