I am trying to write a small application to read BarCode using Motorola MC5040 Symbol device. Clicking on a button on form should read BarCode.
I am having hard time finding any sample projects. I reference Symbol and Symbol.Barcode DLLs
Here is the code that is not working. Not sure how to control the side buttons on device either.
public partial class Form1 : Form
{
public static Symbol.Barcode.Reader SymbolReader = new Reader();
public static Symbol.Barcode.ReaderData SymbolReaderData = new ReaderData(ReaderDataTypes.Text, 100);
public static System.EventHandler SymbolEventHandler = null;
public Form1()
{
InitializeComponent();
InitScanner();
}
public void InitScanner()
{
SymbolEventHandler = new EventHandler(this.SymbolReader_ReadNotify);
SymbolReader.Actions.Enable();
}
public void SymbolReader_ReadNotify(object sender, EventArgs e)
{
SymbolReader.Actions.Enable();
Symbol.Barcode.ReaderData TheReaderData = SymbolReader.GetNextReaderData();
if (TheReaderData.Result == Symbol.Results.SUCCESS )
{
txtBarcode.Text = TheReaderData.Text.ToString();
SymbolReader_CycleScannerReader();
return;
}
SymbolReader_CycleScannerReader();
}
public void SymbolReader_CycleScannerReader()
{
SymbolReader.Actions.Read(SymbolReaderData);
}
private void button1_Click(object sender, EventArgs e)
{
SymbolReader_ReadNotify(sender, e);
}
}
}
Any pointers or correction will be great.
Here is a sample application using the Symbol.Barcode2 library
https://github.com/bigfont/2013-128CG-Vendord/blob/master/HelpfulStuff/CS_Barcode2Sample1/API.cs
if you initialize a Barcode2 object you can then use that object to capture scan data
var myBarcode2Obj = new Barcode2();
myBarcode2Obj.OnScan += //Your scan even here;
Related
So I'm making this small program for my assignment at university and I'm finding it hard to add to my list in my form. Here is my code:
public partial class WorkOutBeam : Form
{
Check checkLib;
public BindingList<ListBox> list;
public WorkOutBeam()
{
InitializeComponent();
}
public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName();
forceItem.Show();
}
public void AddToForceList(string name)
{
list.Items.Add(name);
}
}
NewForceName class below:
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName()
{
InitializeComponent();
}
private void OkButton_Click(object sender, EventArgs e)
{
if (NewForceNames.Text != "")
{
ReferToLibs();
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}
private void NewForceName_Load(object sender, EventArgs e)
{
}
}
So I say to my program, "give me a new force." When it does, it initializes a new form of "NewForceName." I type into a text box and click 'Ok', this starts a public method shown below:
The list is a binding list which refers to the listBox as a data source. However the program tells me that the Items part is inaccessible due to its protection but I don't know how to add it as public. I tried looking in the properties of my listBox but to no avail.
Give this a shot:
public partial class WorkOutBeam : Form
{
Check checkLib;
// public BindingList<ListBox> list; // get rid of this for now
public WorkOutBeam()
{
InitializeComponent();
}
/*public void StartForm(object sender, EventArgs e)
{
list = new BindingList<ListBox>();
listBox1.DataSource = list;
}*/
private void NewForce_Click(object sender, EventArgs e)
{
NewForceName forceItem = new NewForceName(this); // pass a reference to this
// instance of WorkoutBeam
forceItem.Show();
}
public void AddToForceList(string name)
{
// we should do some more things here, but let's keep it simple for now
listBox1.Items.Add(name);
}
}
And
public partial class NewForceName : Form
{
public WorkOutBeam workOutBeam;
public NewForceName( WorkoutBeam beam ) // we take a WorkoutBeam instance as CTOR param!
{
InitializeComponent();
workoutBeam = beam;
}
private void OkButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(NewForceNames.Text))
{
workOutBeam.AddToForceList(NewForceNames.Text);
Close();
}
}
// DO NOT create new WorkoutBeams every time. Use the original.
/*private void ReferToLibs()
{
workOutBeam = new WorkOutBeam();
}*/
}
Disclaimer: I did not address each and every problem in this code. This is just enough so that it should "work" as intended.
I've created a new form, in which I have a toolbox. When I press a button in that form, it should relay that information that has been entered by the user(toolboxbox value) to the main form, in which it should say that piece of information in a label.
Since the method to create that username from the toolbox is private, I cannot access it from any other way. Making it public does not seem to make a difference, neither does get,set (from the way I've been trying to atleast).
Picture that may help explaining it:
Code (in which to create user):
namespace WindowsFormsApplication3
{
public partial class Newuserform : Form
{
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
string uname = textboxUsername.ToString();
}
public void Unamecreate()
{
}
}
}
Form1 Code (To receive created user):
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Aboutform form2 = new Aboutform();
form2.Show();
}
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.Show();
}
}
}
you have a lot of options.
One way is to create an event and handle it in the main form.
public partial class Newuserform : Form
{
//the public property
public event EventHandler<string> UnameChanged;
public Newuserform()
{
InitializeComponent();
}
private void buttonCreateUser_Click(object sender, EventArgs e)
{
if (UnameChanged != null)
UnameChanged(textboxUsername.ToString()); //fire the event
}
}
Now, to "handle" the event, do the following in your main form:
private void newLocalUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Newuserform formnewuser = new Newuserform();
formnewuser.UnameChanged += Handler;
formnewuser.Show();
}
private void Handler (object sender, string Uname)
{
// do something wit the new Uname.
}
note: recreating the Newuserform will require to cleanup previous attached resources.
There are only (3) things I need to know in here.
Firstly, there is only (1) button (close_button) in this form. Here's my frm_main.cs code:
public partial class frm_main : Form
{
public_class pc = new public_class();
public frm_main()
{
InitializeComponent();
this.Load += new System.EventHandler(frm_main_Load);
}
private void close_button_Click(object sender, EventArgs e)
{
this.Close();
}
private void frm_main_Load(object sender, EventArgs e)
{
pc.screen_adjust(close_button);
}
}
And here's my public_class.cs code:
public partial class public_class
{
public int cl_b;
public void screen_adjust(Button b)
{
cl_b = frm_main.ActiveForm.Width;
frm_main.ActiveForm.Width = Screen.PrimaryScreen.Bounds.Width;
frm_main.ActiveForm.Height = Screen.PrimaryScreen.Bounds.Height;
b.Left += frm_main.ActiveForm.Width - cl_b;
}
}
The aim of this borderless program is to auto-stretch the form to the whole screen. Now, what I'd like to learn is:
Did I do a correct "VB.net module" in C# properly?
How do I call the methods in public_class.cs without using the 'public_class pc = new public_class();' and 'pc.screen_adjust(close_button);'?
In the public_class.cs, for example, if I want to change the close_button's text, how should I do it? I can't do frm_main.close_button afterall...
Thanks!
I have a problem with my code. Its working fine and there is no error but a logical one i think so. I have used a method PassValue(int id) to get value from another from. I have tested it and the forms are exchanging the values correctly but the problem comes when I use the value which i have received from other form as a "textbox.text" or a "label.text"
Here is my code:
namespace MyProgram
{
public partial class UserProfile : Form
{
public string empidstr;
public UserProfile()
{
InitializeComponent();
}
public void PassValue(int id)
{
string idstring = Convert.ToString(id);
// empidlabel.Text = idstring;
empidstr = idstring;
}
private void button2_Click(object sender, EventArgs e)
{
empidlabel.Text = empidstr;
}
private void UserProfile_Load(object sender, EventArgs e)
{
}
}}
I'm a newbie in c# and visual studio, but not programming in general.
I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work.
I think it's the most basic question newbies like me ask.
I have a form (Form3) with a text box and a button (I set it up is just for testing purposes).
I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank.
Here's my sample code:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
}
public class a
{
public static void b()
{
Form3 v = new Form3();
v.setCodes = "abc123";
}
}
}
Can someone lend me a hand solving this?
The problem is you are setting the value to a new instance of the form. Try something like this:
public partial class Form3 : Form {
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
private A a;
public Form3()
{
InitializeComponent();
a = new A(this);
}
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
public class A
{
private Form3 v;
public a(Form3 v)
{
this.v = v;
}
public void b()
{
v.setCodes = "abc123";
}
}
You're creating a brand new Form3() instance.
This does not affect the existing form.
You need to pass the form as a parameter to the method.
Try this:
public partial class Form3 : Form
{
/* Code from question unchanged until `button1_Click` */
private void button1_Click(object sender, EventArgs e)
{
a.b(this);
}
}
public class a
{
public static void b(Form3 form3)
{
form3.setCodes = "abc123";
}
}
This passes the current instance of the form to the other class so that it can update the setCodes property. Previously you were creating a new form instance rather than updating the current form.
Sending form instance to other other class
Form1 objForm1=new Form1();
obj.Validate(objForm1);
Easy way to access controls in another class by modifying Controls Private to Public in the Form(Designer.cs)