Hide/show Button at runtime - c#

I need your help with c# and some graphic issues: I'm developing a very simple application. There is a unique form called DeltaPregView, a controller for the form called DeltaPregController and a class that contains the Main of the project:
Main Class:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
namespace deltaPreg
{
static class Program
{
[MTAThread]
static void Main()
{
//create the view
DeltaPregView view = new DeltaPregView();
//link the view to the APP
Application.Run(view);
//initialize the controller of the APP
DeltaPregController controller = new DeltaPregController(view);
}
}
}
View for the class:
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace deltaPreg
{
public partial class DeltaPregView : Form
{
public DeltaPregView()
{
InitializeComponent();
}
public void init()
{
prova.Visible = false;
}
}
}
and the Controller for the view:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace deltaPreg
{
class DeltaPregController
{
#region variables
private DeltaPregView view;
#endregion
public DeltaPregController(DeltaPregView view)
{
this.view = view;
//start the reading process
start();
}
private void start()
{
view.init();
}
}
}
I would like to hide the button called "prova", but nothing changes in my program. I'm a newbie in the winforms management, thank you in advance.

The problem is that you print the form before you call the init function in DeltaPregView.
One way to solve this is by replacing those lines:
//link the view to the APP
Application.Run(view);
//initialize the controller of the APP
DeltaPregController controller = new DeltaPregController(view);
To:
//initialize the controller of the APP
DeltaPregController controller = new DeltaPregController(view);
//link the view to the APP
Application.Run(view);

Related

Program.cs file wont accept name of form that I've created

I've been creating an application for university coursework, using Visual Studio Forms, and I was trying to launch the form I've made to check it's connecting to the database behind it, but it won't accept the name of the form as legitimate. I have no idea why, as it is appearing in the Solution Explorer.
This is the code for the program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FoxhillCustomer
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmViewEditStaff());
}
}
}
This is the code for the frmViewEditStaff.cs:
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;
using System.Data.SqlClient;
namespace FoxhillViewEditStaff
{
public partial class frmViewEditStaff : Form
{
SqlDataAdapter daViewEditStaff;
DataSet dsFoxhillDentistryFinal = new DataSet();
SqlCommandBuilder cmdBViewEditStaff;
DataRow drViewEditStaff;
String connStr, sqlViewEditStaff;
int selectedTab = 0;
bool staffSelected = false;
int staffNoSelected = 0;
public frmViewEditStaff()
{
InitializeComponent();
}
private void frmViewEditStaff_Load(object sender, EventArgs e)
{
connStr = #"Data Source = .\sqlexpress; Initial Catalog = InTheDogHouse; Integrated Security = true";
sqlViewEditStaff = #"select * from Staff";
daViewEditStaff = new SqlDataAdapter(sqlViewEditStaff, connStr);
cmdBViewEditStaff = new SqlCommandBuilder(daViewEditStaff);
daViewEditStaff.FillSchema(dsFoxhillDentistryFinal, SchemaType.Source, "ViewEditStaff");
daViewEditStaff.Fill(dsFoxhillDentistryFinal, "ViewEditStaff");
dgvStaff.DataSource = dsFoxhillDentistryFinal.Tables["Staff"];
dgvStaff.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
tabStaff.SelectedIndex = 1;
tabStaff.SelectedIndex = 0;
}
}
}
Your form is in a namespace of FoxhillViewEditStaff. Your Program class is in a namespace of FoxhillCustomer, and you don't have a using directive for the FoxhillViewEditStaff namespace.
I'd strongly advise you to:
Use a single namespace for all the classes in your project
Rename all your forms to follow normal .NET naming conventions, to make the code clearer
You could just add a using directive, but it would be more sensible to just put the classes into the same namespace, IMO.

WCF with Duplex Communication

Whenever I am using Window Forms It works Fine But it always give error with console applicion.
Error- The socket connection was aborted. This could be caused by an
error processing your message or a receive timeout being exceeded by
the remote host, or an underlying network resource issue. Local socket
timeout was '00:01:00'.
Here is My Code
Contract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace ClassLibrary1
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IReportService" in both code and config file together.
[ServiceContract(CallbackContract=typeof(IReportServiceCallbak))]
public interface IReportService
{
[OperationContract(IsOneWay=true)]
void ProcessReport();
}
public interface IReportServiceCallbak
{
[OperationContract(IsOneWay=true)]
void Progress(int percentage);
}
}
Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
namespace ClassLibrary1
{
public class ReportService : IReportService
{
public void ProcessReport()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(50);
OperationContext.Current.GetCallbackChannel<IReportServiceCallbak>().Progress(i);
}
}
}
}
Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Threading;
namespace DuplexClientsss
{
class Program
{
static void Main(string[] args)
{
new Tests();
} }
class Tests : ReportService.IReportServiceCallback
{
ReportService.ReportServiceClient obj;
public Tests()
{
obj = new ReportService.ReportServiceClient(new InstanceContext(this));
obj.ProcessReport();
}
public void Progress(int percentage)
{
Console.WriteLine(percentage);
}
}
}
new Task
(
()=>
{
obj.ProcessReport();
}
).Start();

Winforms c# listbox not displaying contents of list

I have a winforms application that I am trying to program using MVC
In the designer.cs for the form I have:
this.listBox_regApps = new System.Windows.Forms.ListBox();
this.listBox_regApps.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.listBox_regApps.FormattingEnabled = true;
this.listBox_regApps.Location = new System.Drawing.Point(62, 115);
this.listBox_regApps.Name = "listBox_regApps";
this.listBox_regApps.Size = new System.Drawing.Size(351, 134);
this.listBox_regApps.TabIndex = 1;
Then later in the normal cs file I try to set the listbox data to be a list (apps). When I debug the list apps does indeed have the data, but it never appears in the form. Not sure why. I've tried adding .update and .refresh but neither of those worked either.
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;
namespace AppCompatDemo1
{
interface IAppView
{
void AddListener(IController controller);
};
public partial class AppCompatApp : Form, IAppView
{
IController controller;
public AppCompatApp()
{
InitializeComponent();
}
public void AddListener(IController controller)
{
this.controller = controller;
}
//Trying to set my listbox to display this list
public void SetApps(List<string> apps)
{
listBox_regApps.DataSource = apps;
}
}
public interface IController
{
void OnTestClick(string currentApp);
}
class AppController : IController
{
AppCompatApp view;
IAppModel model;
public AppController(IAppModel model, AppCompatApp view)
{
this.model = model;
this.view = view;
this.view.AddListener(this);
view.SetApps(model.getApps());
}
}
interface IAppModel
{
List<string> getApps();
}
class AppModel : IAppModel
{
List<string> apps;
public AppModel()
{
apps = new List<string>();
apps.Add("app1");
apps.Add("app2");
apps.Add("app3");
}
public List<string> getApps()
{
return apps;
}
}
}
Something like this.
foreach (string app in apps)
{
listBox_regApps.Items.Add(app);
}

NSubstitute Checking received calls don't work

Hey guys im new with the NSubstitute framework. I'm trying to test some of my classes, but when i use NSubstitute to check received calls it says received no matching calls.
I'm trying to test if the method Tick() is receiving update() from track class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATM_System.Event;
using ATM_System.EventDetection;
using ATM_System.Region;
using ATM_System.Track;
namespace ATM_System
{
public class ATM
{
private List<ITrack> _tracks;
private IRegion _region;
private List<IEventDetection> _eventdetects;
private List<IEvent> _events;
public ATM()
{
_tracks = new List<ITrack>();
_region = new Region.Region(100000,100000); //could be changed by user
_events = new List<IEvent>();
_eventdetects = new List<IEventDetection>();
}
public void Tick()
{
// update track positions
foreach (var track1 in _tracks)
{
track1.update();
}
//check for events
foreach (var detector in _eventdetects)
{
_events.AddRange(detector.DetectEvent(_tracks));
}
//handle events and output
foreach (var event1 in _events)
{
event1.HandleEvent();
event1.LogEvent();
}
}
public void IncomingTrack(ITrack track)
{
//add incoming track
_tracks.Add(track);
}
}
}
TEST FILE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ATM_System.Event;
using ATM_System.EventDetection;
using ATM_System.Track;
using NUnit.Framework;
using NSubstitute;
namespace ATM_System.Tests.Unit
{
[TestFixture]
class ATMUnitTests
{
private ATM _uut;
private ITrack _track;
private IEvent _event;
private IEventDetection _eventDetection;
[SetUp]
public void Setup()
{
_track = Substitute.For<ITrack>();
_event = Substitute.For<IEvent>();
_eventDetection = Substitute.For<IEventDetection>();
_uut = new ATM();
}
[Test]
public void Tick_UpdateTracks_TracksUpdated()
{
_uut.Tick();
_track.Received().update();
}
}
}
You forgot to include _track in notification receivers. It simply hasn't subscribed to event and as a result is not notified. To fix simply call your IncomingTrack method:
[Test]
public void Tick_UpdateTracks_TracksUpdated()
{
_uut.IncomingTrack(_track);
_uut.Tick();
_track.Received().update();
}

Tabs on Silver.UI not updating

I need to programitically add tabs to a Silver.UI toolbox after load, and after I add the tabs and call refresh, it still does not show them. But for some reason, when I put the code in load, it works! Here's my code:
//Load the objects into the desinger
DesignDeserializer deserializer = new DesignDeserializer();
deserializer.load();
public void createTab(string title)
{
ToolBoxTab tab = new ToolBoxTab(title, 1);
objectBox.AddTab(tab);
objectBox.RefreshTabs();
}
Here's my DesignDeserializer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Silver.UI;
namespace EmuGUI
{
class DesignDeserializer : main
{
public void load()
{
//Send to create the tabs...
createTab("foo");
}
}
}
It does not give me any errors...

Categories

Resources